2017-09-01 70 views
0
SELECT *, 
IF(users_posts.uid IN (SELECT puid FROM post_ups WHERE post_ups.uid = UID LIMIT 400) AND users_posts.uid <> UID ,10,0) 
    FROM users_posts 

即時得到這個錯誤的MySQL還不支持限制和IN/ALL/ANY/SOME

該版本的MySQL還不支持「LIMIT & IN/ALL/ANY/SOME 子查詢」

如何使用子查詢上的限制,我可以將它轉換爲連接語句,是表現這個查詢好上百萬+這個重新編碼,理論上戈納運行?

UPDATE 這段代碼的結果是否與上面相同?

SELECT *,IF(ups.puid = t1.uid,10,0) 
    FROM users_posts t1 
    LEFT JOIN (SELECT puid FROM post_ups WHERE post_ups.uid = 135 ORDER BY `created_date` DESC LIMIT 400) AS ups ON ups.puid = t1.uid AND t1.uid <> 135 
+0

見https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very - 簡單-SQL查詢 – Strawberry

回答

1

您可以通過

`SELECT *, 
IF(users_posts.uid IN (select * from (SELECT puid FROM post_ups WHERE post_ups.uid = UID LIMIT 400) tmp_tbl) AND users_posts.uid <> UID ,10,0) 
    FROM users_posts` 

做到這一點或替代LEFT JOIN的使用INNER JOIN。

SELECT *,IF(ups.puid = t1.uid,10,0) 
FROM users_posts t1 INNER JOIN (SELECT puid FROM post_ups WHERE post_ups.uid = 135 ORDER BY `created_date` DESC LIMIT 400) AS ups ON ups.puid = t1.uid AND t1.uid <> 135