我目前遇到一个问题,links
表中有一个名为 rating
的列,并且肯定存在 0 以外的值
code> 在列中,但 0
是为每个链接返回的唯一值。当我对该列进行简单的获取时,它会显示所有其他值,但当我执行 SQL 连接时不会显示。
我知道问题出在我的表连接上,但我不确定如何连接这些特定的表。
数据库表结构
评级
列是给我带来问题的列。
'链接'
id |标题 |网址 |用户 ID |列表 ID |评级|重量 |创建日期
'列表'
id |列表标题 |列表描述 |用户 ID |评级|意见 |创建日期
'link_ ratings'
id |用户 ID |链接 ID |额定|创建日期
型号:
public function get_latest(){
$this->db->limit(100);
$this->db->order_by('links.date_created', 'DESC');
$this->db->select('*');
$this->db->select('links.id as current_link_id');
$this->db->from('links');
$this->db->join('list', 'links.list_id = list.id');
$this->db->join('users', 'links.user_id = users.id');
$this->db->join('link_ratings', 'links.id = link_ratings.link_id','left');
$get_latest = $this->db->get();
return $get_latest;
}
感谢任何帮助。
最佳答案
你应该尝试这个:
function get_latest(){
$this->db->select('list.*, users.*, links.id as current_link_id');
$this->db->from('links');
$this->db->join('list', 'links.list_id = list.id');
$this->db->join('users', 'links.user_id = users.id');
$this->db->join('link_ratings', 'links.id = link_ratings.link_id','left');
$this->db->order_by('links.date_created', 'DESC');
$this->db->limit(100);
$get_latest = $this->db->get()->result_array(); #fetch all rows here
echo "<pre>";print_r( $get_latest );die; #print all rows and see if its fetching ratings corrctly or not.
echo $this->db->last_query();die; #check the query generated
return $get_latest;
}
关于php - 连接多个表,特定列返回 0? PHP/MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728147/