如果NOT IN内的值为空,则mysql会抛出错误

时间:2015-03-05 05:55:54

标签: php mysql codeigniter codeigniter-2 notin

如果m.message_type_id IN()为空,则会给我一个错误,上面写着

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ) UNION ( ' at line 16

        SELECT m.id,m.log,message,images,videos,uo.user_id as to_id,owner_id,
        IF(m.owner_id = 45,'sent','received') as type
        FROM messages as m 
        LEFT JOIN events as e ON m.message_type_id = e.id
        LEFT JOIN user_organization as uo ON uo.id = e.org_id
        WHERE m.message_type_id IN ()
        AND m.type = 'event_invite_request_msg'

如果WHERE m.message_type_id IN(61)它给了我正确的答案。

解决方案是什么?

2 个答案:

答案 0 :(得分:1)

IN Clause希望你发送至少一个值,为什么你认为即使使用空参数也应该有效?

  

expr NOT IN(value,...)

<强> Syntax

在更广泛的层面上,几乎所有的比较都是这种情况,如果存在无效的值来比较,为什么你会期望比较不会导致错误

这对你有用吗?

SELECT myField FROM myTable WHERE MyField2 = 

不,它不会

答案 1 :(得分:0)

您必须为此使用条件

if(count($friendsArray[])){
        $qry = "SELECT m.id,m.log,message,images,videos,uo.user_id as to_id,owner_id,
        IF(m.owner_id = 45,'sent','received') as type
        FROM messages as m 
        LEFT JOIN events as e ON m.message_type_id = e.id
        LEFT JOIN user_organization as uo ON uo.id = e.org_id
        WHERE m.message_type_id IN ($ids)
        AND m.type = 'event_invite_request_msg'";
}else{
        $qry = "SELECT m.id,m.log,message,images,videos,uo.user_id as to_id,owner_id,
        IF(m.owner_id = 45,'sent','received') as type
        FROM messages as m 
        LEFT JOIN events as e ON m.message_type_id = e.id
        LEFT JOIN user_organization as uo ON uo.id = e.org_id
        WHERE m.type = 'event_invite_request_msg'";
}