4

I'am using the "GROUP_CONCAT" function with the "NOT IN" statement in my mysql query. But for unknown reason, it doesn't return the correct values:

Here is my query not working:

select firstname, lastname
from t_user
where (status_Id NOT IN(Select GROUP_CONCAT(id) from t_status where code = 'ACT'
or code = 'WACT'))

Returns 46 rows

Here is my query working:

select firstname, lastname
from t_user
where (status_Id NOT IN(1,4))

Returns 397 rows

The results of the of the GROUP_CONCAT subquery

 (Select GROUP_CONCAT(id) from t_status where code = 'ACT' or code = 'WACT') = 1,4.

It seems that the query take only care of the first item return by the GROUP_CONCAT subquery.

So I don't understand what's going on and why I do not have the same results in both cases.

Thanks in advance Gael

4

2 に答える 2

5

この場合、GROUP_CONCAT関数は文字列値を返すため、使用する必要はありません。ANDはand
1, 4とは大きく異なります。14

select  firstname, lastname
from    t_user
where   status_Id NOT IN 
        ( Select id 
          from t_status 
          where code = 'ACT' or code = 'WACT'
        )

クエリを正しくするより良い方法は、を使用することLEFT JOINです。

SELECT  a.firstname, a.lastname
FROM    t_user a
        LEFT JOIN t_status b
            ON a.t_status = b.id AND
                b.code IN ('ACT', 'WACT')
WHERE   b.id IS NULL
于 2013-03-21T08:40:16.663 に答える