Mysql子查询

时间:2012-09-24 12:52:22

标签: 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

2 个答案:

答案 0 :(得分:2)

我记得LIMIT子查询中尚不支持IN()。相反,您需要针对子查询LEFT JOIN并在子查询中查找NULL

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

答案 1 :(得分:0)

假设有点,但你的意思是这样的吗?

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