繁体   English   中英

php mysql嵌套查询“不在哪里”

[英]php mysql nested query “where not in”

我有两个表test_category和结果:

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

我想选择test_category上存在但在结果中不存在的一些列。

这是我试过的sql,但它不起作用:

select _id, score_type from test_category where _id in ('13') and not in(select score_type from results where user_id=349);

您错过了在AND之后提及列名称

尝试这个,

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

您的查询应该是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM