MSQL如何在NOT IN中使用多条语句

时间:2018-10-22 09:47:00

标签: mysql notin

我尝试执行此查询:

SELECT ISBN FROM table2 WHERE 
NOT IN ISBN=('8426429807','840149768X')
group by ISBN 
ORDER BY AVG(`Book-Rating`) DESC LIMIT 10

但是这个形状我出错了

  

1064-您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以使用正确的语法   靠近'IN ISBN =('8426429807','840149768X')

如何在Not In中使用多个isbn

2 个答案:

答案 0 :(得分:4)

Not In()的语法是:

expr NOT IN (value,...)

基本上,列/表达式应位于Not In之前,并且您不使用=运算符。应该是:

SELECT ISBN
FROM table2
WHERE ISBN NOT IN ('8426429807',
                   '840149768X')
GROUP BY ISBN
ORDER BY AVG(`Book-Rating`) DESC
LIMIT 10

编写同一查询的另一种方法是使用NOT (expr IN (value, ...))

SELECT ISBN
FROM table2
WHERE NOT (ISBN IN ('8426429807',
                    '840149768X'))
GROUP BY ISBN
ORDER BY AVG(`Book-Rating`) DESC
LIMIT 10

答案 1 :(得分:0)

您应该使用where column_name NOT IN ( col_value1, col_value2)

select ISBN from table2
where ISBN NOT IN ('8426429807','840149768X')
group by ISBN 
ORDER BY AVG(`Book-Rating`) DESC LIMIT 10