MySQL不排除来自其他查询

时间:2015-05-15 12:42:28

标签: mysql compare notin

我有一个实验数据库,并尝试让所有希望重新接触的参与者除了参与某个特定实验的参与者之外。该查询有效,但它不包括仅参与此实验的参与者,但确实包括参与该实验和其他实验的参与者。 请原谅我的草率代码!

SELECT DISTINCT t1.`par_name`, t1.`email`, t1.`comment`
FROM `par` AS t1, `exp` AS t2, `pie` AS t3
WHERE t1.`par_id` = t3.`par_id` AND t3.`exp_id` = t2.`exp_id` AND 
      t1.`ReContact`!= 'N' AND t1.`email`!= " " AND t1.`MotherTounge` 
      = 'Deu' OR t1.`MotherTounge` = '' AND t1.`age` BETWEEN 18 AND 35 

AND t1.`par_name` NOT IN(SELECT t1.`par_name`
                         FROM `par` AS t1, `exp` AS t2, `pie` AS t3
                         WHERE t1.`par_id` = t3.`par_id` AND 
                               t3.`exp_id` = t2.`exp_id` AND t3.`exp_id` = '15ET001')
GROUP BY `par_name`;

1 个答案:

答案 0 :(得分:0)

我不是100%,但我认为你需要附上一些联接

SELECT DISTINCT t1.`par_name`, t1.`email`, t1.`comment`
FROM `par` AS t1, `exp` AS t2, `pie` AS t3
WHERE (t1.`par_id` = t3.`par_id`) AND (t3.`exp_id` = t2.`exp_id`) AND 
      (t1.`ReContact`!= 'N') AND (t1.`email`!= " ") AND 
// here is the change
(t1.`MotherTounge` = 'Deu' OR t1.`MotherTounge` = '') AND (t1.`age` BETWEEN 18 AND 35)

AND t1.`par_name` NOT IN(SELECT t1.`par_name`
                         FROM `par` AS t1, `exp` AS t2, `pie` AS t3
                         WHERE t1.`par_id` = t3.`par_id` AND 
                               t3.`exp_id` = t2.`exp_id` AND t3.`exp_id` = '15ET001')
GROUP BY `par_name`;

因为你有一个OR,你需要用()包含它。

试一试!