1

i have two tables test_category and results:

CREATE TABLE IF NOT EXISTS `test_category` (
  `_id` int(11) NOT NULL AUTO_INCREMENT,
  `score_type` varchar(50) NOT NULL,
  `sub_code` int(11) NOT NULL,
  `duration` varchar(10) NOT NULL,
  PRIMARY KEY (`_id`),
  KEY `sub_code` (`sub_code`)
)

CREATE TABLE IF NOT EXISTS `results` (
  `res_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `score_type` int(11) NOT NULL,
  `score` int(11) NOT NULL,
  `date_taken` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`res_id`),
  KEY `user_id` (`user_id`),
  KEY `score_type` (`score_type`)
)

i want to select some columns that exist on test_category but doesn't exist in results.

here is the sql i've tried but it does not work:

select _id, score_type from test_category where _id in ('13') and not in(select score_type from results where user_id=349);
4
  • 1
    I guess it should looks like : and score_type not in Commented May 29, 2014 at 5:52
  • @valex but datatype is differ in both tables. Commented May 29, 2014 at 5:55
  • @BrokenHeartღ You're right. The same name but different types ... Commented May 29, 2014 at 6:06
  • @valex that's why I have not mapped these columns for NOT IN. Commented May 29, 2014 at 6:13

2 Answers 2

2

You missed to mention the column name after AND

Try this,

SELECT _id, score_type FROM test_category WHERE _id IN
('13') AND _id NOT IN(SELECT score_type FROM results WHERE user_id=349);
Sign up to request clarification or add additional context in comments.

1 Comment

i see, so that's where i was wrong. your sql command works. thanks!
1

You query should be this:

select _id, score_type 
    FROM test_category 
      WHERE _id in ('13') 
       AND _id NOT IN (select score_type 
                         FROM results 
                           WHERE user_id=349);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.