IN查询到另一个IN查询怎么用不同的方式写?

时间:2017-02-23 10:39:14

标签: mysql mysqli

我希望这种类型的查询以正确的方式编写?

SELECT * FROM tb_user WHERE (
    id IN (SELECT user_id FROM tb_user_experience 
    WHERE
    company_name IN(SELECT company_name 
    FROM tb_company 
    WHERE tb_company.no_employee = '2'))

我知道这是错误的查询,但如何使用正确的mysql语法获得此逻辑的结果?

这是半查询我的完整查询在

之下
SELECT * FROM tb_user WHERE 
    steps_completed = 6 AND is_active='y' AND id != '1' AND 
    (country != '') AND (
    id IN (SELECT user_id FROM tb_user_experience WHERE 
    company_name IN(SELECT company_name FROM tb_company WHERE
    tb_company.no_employee = '2')))

1 个答案:

答案 0 :(得分:0)

查询也可以重写为:

SELECT u.* 
FROM tb_user u
INNER JOIN tb_user_experience e 
    ON  u.id = e.user_id
    AND u.steps_completed = 6 
    AND u.is_active = 'y' 
    AND u.id != '1'
    AND u.country != ''
INNER JOIN tb_company c
    ON  c.company_name = e.company_name
    AND c.no_employee = '2'