简体   繁体   English

PHP CodeIgniter 多个 LEFT OUTER 加入查询

[英]PHP CodeIgniter multiple LEFT OUTER join in on query

I'm using PHP CodeIgniter to query from my Database.我正在使用 PHP CodeIgniter 从我的数据库中查询。 I need to use multiple LEFT OUTER joins.我需要使用多个 LEFT OUTER 连接。 The join uses multiple conditions (I'm not sure it is supported as of the bug).连接使用多个条件(我不确定它是否受 bug 支持)。 My query results have the same value for both parameters I want to fetch:我的查询结果对于我要获取的两个参数具有相同的值:

Query:询问:

$this->db->select('anonymous.*,
    count(items_likes.id) as all_likes, count(items_comments.id) as all_comments');
    $this->db->from('anonymous');
    $this->db->where('anonymous.fid', $feed_id);

    $this->db->group_by('anonymous.id');

    $this->db->join('all_users', 'all_users.id = anonymous.uid', 'left outer');
    $this->db->join('images', 'images.ud = anonymous.uid AND images.fid = anonymous.fid', 'left outer');
    $this->db->join('items_likes', 'items_likes.uid = anonymous.uid AND items_likes.fid = anonymous.fid', 'left outer');

    $this->db->join('items_comments', 'items_comments.uid = anonymous.uid AND items_comments.fid = anonymous.fid', 'left outer');

    $list = $this->db->get()->result();

anonymous: is a table with unregistered users匿名:是未注册用户的表

fid: Is the feed id fid:是提要ID

images: Is a table of all the images that were uploaded to the feed图片:是上传到提要的所有图片的表格

items_likes: Is all the users' likes per image items_likes:是每张图片的所有用户点赞数

items_comments: Is all the users' comments per image items_comments:是每张图片的所有用户评论


The problem is that all_comments always return the same value as all_likes.问题是 all_comments 总是返回与 all_likes相同的值。 I'm not sure if the multiple LEFT-OUTER joins are the problem or maybe the fact that I'm using multiple conditions in every join statement ie我不确定多个 LEFT-OUTER 连接是否是问题所在,或者我在每个连接语句中都使用了多个条件,即

items_comments.uid = anonymous.uid AND items_comments.fid = anonymous.fid items_comments.uid = 匿名.uid 和 items_comments.fid = 匿名.fid

The log result:日志结果:

"all_likes":"12","all_comments":"12"

although I have 12 likes, the comments should be 3虽然我有 12 个赞,但评论应该是 3

If I write the query as follows the result is ok:如果我按如下方式编写查询,则结果可以:

$this->db->select('anonymous.*,
(SELECT count( items_likes.id ) FROM items_likes WHERE items_likes.uid = anonymous.uid AND items_likes.fid = anonymous.fid) as all_likes, 
(SELECT count( items_comments.id ) FROM items_comments WHERE items_comments.uid = anonymous.uid AND items_comments.fid = anonymous.fid) as all_comments');
$this->db->from('anonymous');
$this->db->where('anonymous.fid', $feed_id);

$this->db->group_by('anonymous.id');

$this->db->join('all_users', 'all_users.id = anonymous.uid', 'left outer');
$this->db->join('images', 'images.ud = anonymous.uid AND images.fid = anonymous.fid', 'left outer');
//$this->db->join('items_likes', 'items_likes.uid = anonymous.uid AND items_likes.fid = anonymous.fid', 'left outer');

//$this->db->join('items_comments', 'items_comments.uid = anonymous.uid AND items_comments.fid = anonymous.fid', 'left outer');

$list = $this->db->get()->result();

Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.左外连接从表 A 中生成一组完整的记录,匹配的记录(如果可用)在表 B 中。如果没有匹配,右侧将包含 null。

You need to add the records which contain NULL您需要添加包含 NULL 的记录

add this where clause to your query:将此 where 子句添加到您的查询中:

$this->db->->where('items_comments.uid IS NULL')    

more info on joins here更多关于加入的信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM