0
更多的行?我想從表格中選擇activity_id,但不包括特定用戶的最新8個activity_id。當我試圖運行此查詢如何選擇子查詢
更多的行?我想從表格中選擇activity_id,但不包括特定用戶的最新8個activity_id。當我試圖運行此查詢如何選擇子查詢
NOT LIKE正期待一個表達式或一個值來比較而不是結果集。 變化也不像是NOT IN
試試這個:
SELECT * FROM `activity`
WHERE user_id = 1 AND activity_id NOT IN (
SELECT activity_id FROM activity WHERE user_id = 1
ORDER BY activity_id DESC LIMIT 8)
通過這樣解決的:
$sql2 = "DELETE t1.*
FROM activity t1
left join (select activity_id from activity where user_id = '".$row['user_id']."' ORDER BY activity_id DESC LIMIT 8) t2
on (t1.activity_id = t2.activity_id)
where t2.activity_id is null
and t1.user_id = '".$row['user_id']."'";
NOT沒有在子查詢的MySQL版本支持IN和限制。 – Stefan
我已經做出了自己的select語句......我如何將其更改爲delete語句? \t \t \t \t $ SQL1 =「選擇T1。*,t2.activity_id作爲activity_id_2 \t \t \t \t \t \t \t FROM動作T1 \t \t \t \t \t \t \t左加入(選擇USER_ID,ACTIVITY_ID從活動其中USER_ID ='「。$ row ['user_id']。」'ORDER BY user_id,activity_id DESC LIMIT 8)t2 \t \t \t \t上\t \t(t1.user_id = t2.user_id AND t1.activity_id = t2.activity_id) \t \t \t \t \t \t \t其中t2.activity_id爲空 \t \t \t \t \t \t \t和t1.user_id =「」 。$行[ 'user_ID的'] 「'」。 – Stefan
@Stefan。對於刪除語句,只需讓你的子查詢返回你想要刪除的activity_id值,然後:DELETE FROM activity where activity_id NOT IN(你的子查詢在這裏) – slash28cu