我在mysql查询中遇到了一个问题,我有两个表。
category_info
中文名称1最新新闻2告示板3标题news_info
日期1流动2013-03-04 2 1鱼类2013-03-04 3 2 Airtel印度2013-03-04 4 4 2 Marco Simoncelli 2013-03-05 5 3 title1 2013-03-22 6我想从表news_info中访问标题,该值具有最大的pid
我使用以下查询
SELECT a.*, b.* FROM category_info AS a RIGHT JOIN news_info AS b ON (a.cid = b.cid) GROUP BY a.cid它给了我独特的价值,但没有给我最大的价值。它给出了最小id值。
发布于 2013-03-23 14:16:03
这将给你对你问的问题的答案。但我不确定这是不是你真正想要的。
select distinct title
from news_info
where pid =
(select max(pid) from news_info)发布于 2013-03-23 14:32:07
以下是另一种方法:
select ni.*
from news_info ni
order by pid desc
limit 1在您的示例中,没有重复的pids,因此只有一个具有最大值。
发布于 2013-03-23 14:33:39
以下是另一种方法:
SELECT *
FROM news_info n
LEFT JOIN category_info c ON a.cid = b.cid
--
-- the maximum pid := there should not exist a higher pid value (for the same cid)
--
WHERE NOT EXISTS (
SELECT * FROM news_info x
WHERE x.cid = n.cid
AND x.pid > n.pid
);https://stackoverflow.com/questions/15586766
复制相似问题