php 在 CodeIgniter 中加入查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15218315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
join query in CodeIgniter
提问by Edvinas Liutvaitis
I am using join query in CodeIgniter and can't get it to work. It only displays one table data, but not the other. I am new to CodeIgniter and can't figure this out. Pleas someone help me. Tnanks in advance.
我在 CodeIgniter 中使用连接查询,但无法让它工作。它只显示一个表数据,而不显示另一个。我是 CodeIgniter 的新手,无法弄清楚这一点。请有人帮助我。提前准备好。
view
看法
<?php foreach ($query as $row): ?>
<?php echo $row->answerA;?><br>
<?php echo $row->answerB;?><br>
<?php echo $row->answerC;?><br>
<?php echo $row->comment;?><br>
<?php echo $row->name; ?>
<?php endforeach; ?>
controller
控制器
function getall() {
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
// print_r($data['query']);
// die();
$this->load->view('result_view', $data);
}
model
模型
function result_getall() {
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'right outer');
$query = $this->db->get();
return $query->result();
}
EDIT
编辑
The result of
的结果
print_r($data['query']);
die();
is an array as below:
是一个数组,如下所示:
Array ( [0] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 83 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => [email protected] ) [1] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 84 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => [email protected] ) [2] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 85 [name] => Edvinas [second_name] => Liutvaitis [phone] => [email] => [email protected] ) [3] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 86 [name] => EdvinasQ [second_name] => LiutvaitisQ [phone] => 12345678 [email] => [email protected] ) [4] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 87 [name] => Edvinas [second_name] => Liutvaitis [phone] => 123456 [email] => [email protected] ) )
table structure
表结构
credentials
证书
cid(PRIMARY), name, second_name, phone, email
cid(PRIMARY)、姓名、second_name、电话、电子邮件
tblanswers
回复
answerid(PRIMARY), userid, questionid, answerA, answerB, answerC.
answerid(PRIMARY)、userid、questionid、answerA、answerB、answerC。
回答by Alessandro Minoccheri
Try with this:
试试这个:
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid');
Or
或者
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'inner');
And print the result to see if is what you want
并打印结果以查看是否是您想要的
回答by PinoyPal
What's the primary table you want to query? Well you can try this one, suggested that when using joins, you must specify an alias for each table:
您要查询的主表是什么?那么你可以试试这个,建议在使用join的时候,你必须为每个表指定一个别名:
function result_getall(){
// if you want to query your primary data from the table 'tblanswers',
$this->db->select('a.*,b.*'); <-- select what you might want to select
$this->db->from('tblanswers a');
$this->db->join('credentials b', 'b.cid = a.answerid', 'left');
$query = $this->db->get();
return $query->result();
// if you want to query your primary data from the table 'credentials',
$this->db->select('a.*,b.*'); <-- select what you might want to select
$this->db->from('credentials a');
$this->db->join('tblanswers b', 'b.answerid = a.cid', 'left');
$query = $this->db->get();
return $query->result();
}
回答by Coder NO i just try to Develop
Remove select() tag, as u need all terms.
删除 select() 标签,因为您需要所有术语。
U can modify code like
你可以修改代码,如
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'outer');
$query = $this->db->get('tblanswers');
return $query->result();
回答by Altaf Hussain
If both of your tables are having the related, then it should work. Why are you using right outer join ? Simply use the Inner Join or any other join and it will work. Kindly read about different types of JOIN here http://www.w3schools.com/sql/sql_join.aspand here http://dev.mysql.com/doc/refman/5.0/en/join.html
如果您的两个表都具有相关性,那么它应该可以工作。你为什么使用右外连接?只需使用内部连接或任何其他连接,它就会起作用。请在这里阅读不同类型的 JOIN http://www.w3schools.com/sql/sql_join.asp和这里http://dev.mysql.com/doc/refman/5.0/en/join.html
Hope this will help.
希望这会有所帮助。
Thank you
谢谢

