我读过很多关于NOT IN运算符的Q& A' If I use IN operator to filter NULL values and white spaces It is not working Why? 还有很多其他问题,但问题是当我们在运算符中不使用非主键时,它不会返回响应。
SELECT * FROM temp_customers WHERE temp_customers.fk_my_id NOT IN (SELECT fk_my_id FROM customers)
其中fk_my_id是非主键,可能是字符串。
有什么想法吗?
答案 0 :(得分:11)
在此使用group_concat
将fk_my_id
与逗号
SELECT * FROM temp_customers WHERE temp_customers.fk_my_id NOT IN
(SELECT GROUP_CONCAT(fk_my_id) FROM customers)
# when fk_my_id is INTEGER output (1,2)
SELECT * FROM temp_customers WHERE temp_customers.fk_my_id NOT IN
(SELECT GROUP_CONCAT("'",fk_my_id,"'") FROM customers)
# when fk_my_id is VARCHAR output ('1','2')
答案 1 :(得分:0)
我猜我在查询中使用了错误的列:(SELECT fk_my_id FROM customers)
。它应该是您的客户表的pkId
SELECT * FROM temp_customers
WHERE temp_customers.fk_my_id
NOT IN (SELECT pk_my_id FROM customers)