Mysql

Mysql子查詢返回多於1行

  • March 18, 2020
SELECT * FROM wp_posts WHERE ID IN 
(
(SELECT courses  FROM wp_category WHERE CatID =401) OR 
(SELECT  meta_value FROM wp_postmeta WHERE  post_id IN (SELECT courses FROM wp_category WHERE CatID =401) AND meta_key ='post_id' )
) 
AND 
ID IN (SELECT post_id FROM wp_locations_courses WHERE location_id = 1120)

我在使用IN子查詢時遇到上述查詢的問題,這給了我錯誤Subquery returns more than 1 row,為什麼在我使用IN子句時會發生這種情況

       (SELECT courses  FROM wp_category WHERE CatID =401) 
       OR 
       (SELECT  meta_value FROM wp_postmeta WHERE  post_id IN (SELECT courses FROM wp_category WHERE CatID =401) AND meta_key ='post_id' )

這是一個條件,但你需要n價值觀。

這應該有效:

(
   ID IN 
       (
           (SELECT courses  FROM wp_category WHERE CatID =401) 
       )
   OR
   ID IN 
       (
           (SELECT  meta_value FROM wp_postmeta WHERE  post_id IN 
               (SELECT courses FROM wp_category WHERE CatID =401) AND meta_key ='post_id' )
       )
)

還要注意 MySQL 的子查詢。在您的情況下,子查詢。

看看這裡:https ://stackoverflow.com/questions/12356784/mysql-dependent-sub-query-with-not-in-in-the-where-clause-is-very-slow

引用自:https://dba.stackexchange.com/questions/24246