0

I have an experiment DB, and try to get all the participants that wish to be recontacted except for the ones that participated in one specific experiment. The query works, except that it only doesn't include the participants that took part only in this experiment, but does include participants that took part in that experiment and other experiments. Excuse my sloppy code!

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`;
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Yoav
  • 3
  • 1
  • 1
    If you like, consider following this simple two-step course of action: 1. If you have not already done so, provide proper DDLs (and/or an sqlfiddle) so that we can more easily replicate the problem. 2. If you have not already done so, provide a desired result set that corresponds with the information provided in step 1. – Strawberry May 15 '15 at 12:46
  • Other dude answered. Thanks for the comment tho, I'll do so in the future! :) – Yoav May 15 '15 at 13:04

1 Answers1

0

Im not 100% but i think you need to enclose some joins

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`;

since you have a OR , you need to enclose that with ().

Give it a try!

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55