不在哪里以及哪里出现错误

时间:2018-06-22 03:53:48

标签: php mysqli codeigniter-3

我尝试多次移动WHERE子句,但仍然出现错误。我在WHERE NOT IN AND WHERE子句中做错了吗?在添加 WHERE ha_rooms.deleted!= 1 子句之前,代码运行良好。我也尝试使用已删除的<> 1,但仍然显示相同的错误

$query2 = $this->db->query("SELECT * FROM ha_rooms
    WHERE ha_rooms.deleted != 1 JOIN ha_user_room_merged
    WHERE ha_rooms.room_id NOT IN (SELECT ha_user_room_merged.room_id
    FROM ha_user_room_merged WHERE ha_user_room_merged.deleted = 0)
    group by ha_rooms.room_id");

错误是这个


  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本以使用正确的语法   在'JOIN ha_user_room_merged附近WHERE ha_rooms.room_id不在(选择   ha_user_room_merg'在第1行

3 个答案:

答案 0 :(得分:1)

坦率地说,您的查询无论如何都无法正确完成,但是如果您没有其他逻辑,它可以使您清楚地知道要执行的操作。因此,我建议您为SQL Syntax, Ordering, use of GROUP BY, WHERE sub-query etc做Google。顺便说一句,BTW是您查询的正确版本,请检查它是否对您有用

SELECT hr.* 
FROM ha_rooms hr
JOIN ha_user_room_merged hurm On hurm.room_id = hr.room_id
WHERE hurm.deleted = 0 
AND hr.deleted <> 1

答案 1 :(得分:0)

请尝试。

select * from ha_rooms as a inner join ha_user_room_merged b on a.ha_rooms.id = b.room_id
where b.deleted <> 0

输出

ha_rooms.room_id    ha_user_room_merged.room_id   deleted
1                    1                             1
2                    2                             1
3                    3                             1
4                    4                             1

谢谢:)

答案 2 :(得分:0)

使用下面的codeigniter查询。您的加入没有开启

$all_ids = 0;
        $this->db->select('group_concat(room_id) as ids');
        $this->db->where('deleted', 0);
        $ids = $this->db->get('ha_user_room_merged')->row_array();
        if($ids) {
            $all_ids = explode(',', $ids['ids']);
        }

        $this->db->where('hr.deleted !=', 1);
        $this->db->where_not_in('hr.room_id', $all_ids);
        $this->db->join('ha_user_room_merged hrm', 'hrm.room_id = hr.room_id');
        $this->db->group_by('hr.room_id');
        $this->db->get('ha_rooms hr')->result_array();