php mysql嵌套查询 "where not in"

标签 php mysql sql

我有两个表 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);

关于php mysql嵌套查询 "where not in",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23926513/

相关文章:

php - JavaScript 和包含\r 和/或\n 字符的字符串数组

PHP/MySQL - 如果变量不存在则更新数组

mysql - 如何从 MySQL Workbench 中的模型导出 sql

php - 我无法在我的 sql 查询中添加 OR 运算符

javascript - 使用 PHP 和 AJAX 加载更多评论按钮

php - 正则表达式仅允许选择字母表字符

php - 在表中添加两个属性,然后使用 AS

PHP/MYSQL 具有多个字段和字段选择的 Distinct 运算符

c# - Entity Framework 在调用 Any 或 Count 后抛出 NotSupportedException

sql - 如何更改 magento 中的 base_url。因为我的网站在我的 magento 管理员上更改后消失了