表格喜欢:
的结构id(int 11)
idn(varchar 10)
type (enum ('up','down'))
以下是表格新闻:
的结构id(int 11)
idn(varchar 10)
header (varchar 100)
date(datetime)
要显示新闻,我使用sql查询:
SELECT header FROM News n left join Likes l on l.idn=n.idn ORDER by n.date
但是现在我想选择标题新闻从表格新闻中按顺序排序desc count表格中的行喜欢type = up。
我该怎么做?
答案 0 :(得分:1)
这是一个查询,以获取每天最喜欢的标题。如果所有日期都需要TOP,请从n.date
ORDER BY
SELECT header FROM News as n
LEFT JOIN
(
Select idn,Count(*) as UpLikes FROM Likes WHERE type='up' GROUP BY idn
) as l
ON l.idn=n.idn
ORDER BY n.date,l.UpLikes DESC
答案 1 :(得分:0)
SELECT n.header, COUNT(i.`id`) as like_count
FROM Likes l LEFT JOIN News n ON n.idn = l.idn
WHERE l.`type` = 'up'
ORDER BY like_count DESC
答案 2 :(得分:0)
你为什么不用这个:
SELECT id, idn, header, LikeCount
FROM
(
SELECT id, idn, header, (SELECT COUNT(1) FROM [Likes]) AS [LikeCount]
FROM [News]
)
ORDER By [LikeCount]