1. 程式人生 > 其它 >Mysql子查詢中不能使用limit或者in

Mysql子查詢中不能使用limit或者in

技術標籤:MySQL

SQl語句

SELECT `a`.`article_id` FROM `article_tags` `a`  WHERE  a.tag_id in (SELECT id from tags WHERE `name`='tagName' and fmid in (5,6,7) LIMIT 100)   ORDER BY a.article_id desc LIMIT 1

報錯資訊

SQLSTATE[42000]: Syntax error or access violation: 1235 This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery

原因:

MYSQL子查詢不支援limit

解決辦法:

再使用一層迴圈

SELECT `a`.`article_id` FROM `article_tags` `a`  WHERE  a.tag_id in (SELECT id from (SELECT id from tags WHERE `name`='tagName' and fmid in (5,6,7) LIMIT 100) as tid)   ORDER BY a.article_id desc LIMIT 1

SELECT `a`.`article_id` FROM `article_tags` `a` WHERE a.tag_id in (SELECT id from (

SELECT id from tags WHERE `name`='tagName' and fmid in (5,6,7) LIMIT 100) as tid) ORDER BY a.article_id desc LIMIT 1

注意顏色表示部分,不要寫錯了哈