如何将此MySQL查询转换为Codeigniter查询?
SELECT *
FROM categories
LEFT JOIN user_category_subscriptions ON
user_category_subscriptions.category_id = categories.category_id
and user_category_subscriptions.user_id =1
答案 0 :(得分:2)
您需要将AND部分放在Codeigniter的join()
中$query = $this->db ->select('t1.*')
->join('user_category_subscriptions t2', 't1.category_id =t2.category_id AND t2.user_id =1','left')
->get('categories t1');
return ($query->num_rows())?$query->result():false;
在查询后使用此行,您可以仔细检查查询是否正确生成:
echo $this->db->last_query();die;
更多关于 CI 2.x Active Record Class以及 CI 3.x Query Builder Class
答案 1 :(得分:1)
根据文件here:
$query = $this->db->query('SELECT name, title, email FROM my_table');
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
echo 'Total Results: ' . $query->num_rows();
如果您想使用ORM,可以按照StackOverflow Link上提供的建议