MySQL 不排除其他查询的查询结果
MySQL doesn't exclude query results from other query
我有一个实验数据库,并尝试获取所有希望重新联系的参与者,除了那些参与了一个特定实验的参与者。该查询有效,只是它不包括仅参与该实验的参与者,但包括参与该实验和其他实验的参与者。
请原谅我草率的代码!
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`;
我不是 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 ,你需要用 () 把它括起来。
试试吧!
我有一个实验数据库,并尝试获取所有希望重新联系的参与者,除了那些参与了一个特定实验的参与者。该查询有效,只是它不包括仅参与该实验的参与者,但包括参与该实验和其他实验的参与者。 请原谅我草率的代码!
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`;
我不是 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 ,你需要用 () 把它括起来。
试试吧!