1
我需要幫助來編寫子查詢。Mysql子查詢
我有一個包含精選文章和非精選文章的類別的文章表。
我想挑選4個最新的精選文章,這些精選文章不屬於前6個最新的精選文章,無論其類別如何。這是我做了什麼
select title
from node
where nid NOT IN(select nid from node order by date limit 6)
order by date
limit 4
我需要幫助來編寫子查詢。Mysql子查詢
我有一個包含精選文章和非精選文章的類別的文章表。
我想挑選4個最新的精選文章,這些精選文章不屬於前6個最新的精選文章,無論其類別如何。這是我做了什麼
select title
from node
where nid NOT IN(select nid from node order by date limit 6)
order by date
limit 4
假設了一下這裏,但你的意思是這樣的嗎?
select title
from node
where nid NOT IN(select nid from node where featured = 1 order by date desc limit 6)
and featured = 1
and category = ...
order by date desc
limit 4
我記得,LIMIT
尚不支持在IN()
子查詢。相反,您需要針對子查詢LEFT JOIN
並在子查詢中查找NULL
s:
SELECT title
FROM
node
LEFT JOIN (SELECT nid FROM node ORDER BY date LIMIT 6) nids ON node.nid = nids.nid
WHERE
nids.nid IS NULL
AND node.category = 'some category'
ORDER BY date
LIMIT 4