MySQL SELECT COUNT() 然后 ORDER BY 日期

发布于 2024-12-22 20:31:49 字数 1129 浏览 0 评论 0原文

我已经到处搜索,但似乎无法找到正确的方式来表达我的问题以获得我想要的结果(我确信这真的很容易,但我一直在用头撞击它年龄!)

我有以下查询:

SELECT co.title, co.content, co.id, bu.backupDate, bu.backupBy, COUNT(bu.id) AS totalBackups
FROM content co, wol_content_backup bu
WHERE co.car_model = '6-110'
AND co.subcategory = 'introduction'
ORDER BY bu.backupDate DESC

content 包含文章的内容,wol_content_backup 包含文章的备份。 bu.backupDate 是包含备份日期的 DATETIME 字段。

我想要的是返回上次备份的日期(bu.backupDate)(通常有几个,因为我的大多数管理员都训练有素,在修改文章之前按“备份”按钮!)以及数据库中的备份(COUNT(bu.id)AS TotalBackups)

但是,我的查询当前仅返回一个结果,这是第一个备份(不是最新的)我想要的只是从内容中选择具有以下内容的行:最近的相应备份日期。没有 COUNT(bu.id) 查询工作正常,所以我猜这就是问题所在,但不确定如何修复它......

任何帮助将非常感激!

圣诞节快乐!

编辑

感谢大家的帮助!我尝试了你所有的建议(并且一路上学到了很多新东西!)

你们都通过提醒我有关 MAX() 函数的方式让我走上了正确的道路,这最终导致了以下函数查询:

SELECT co.title, co.content, co.id, MAX(bu.backupDate) AS backupDate, bu.backupBy, COUNT(bu.id) AS totalBackups
FROM content co, wol_content_backup bu
WHERE co.car_model = '6-110'
AND co.subcategory = 'introduction'

它准确地输出我想要什么!

非常感谢您在圣诞节那天帮助陷入困境的程序员! :)

塞布

I've searched all over the place, but just can't seem to get the right way of phrasing my question to get the result I want (I'm sure it's really easy, but I've been smacking my head against it for ages!)

I have the following query:

SELECT co.title, co.content, co.id, bu.backupDate, bu.backupBy, COUNT(bu.id) AS totalBackups
FROM content co, wol_content_backup bu
WHERE co.car_model = '6-110'
AND co.subcategory = 'introduction'
ORDER BY bu.backupDate DESC

content contains an article's content, and wol_content_backup contains backups of the articles. bu.backupDate is a DATETIME field containing the date of the backup.

What I want is to return the date of the last backup(bu.backupDate) (there are usually several, since most of my admins are well trained and press the "backup" button before they modify the article!) and also the number of backups in the database (COUNT(bu.id) AS totalBackups)

However, my query is currently just returning one result, which is the first backup (not the most recent) All I want is for it to select the row from content which has the most recent corresponding backup date. Query works fine without the COUNT(bu.id) so Im guessing this is the problem, but not sure how to fix it...

Any help would be really appreciated!

Happy Christmas!

EDIT

Thanks for all your help guys! I tried all of your suggestions (and learned a huge amount of new stuff along the way!)

You all put me on the right track by reminding me about the MAX() function, which eventually led to the following functional query:

SELECT co.title, co.content, co.id, MAX(bu.backupDate) AS backupDate, bu.backupBy, COUNT(bu.id) AS totalBackups
FROM content co, wol_content_backup bu
WHERE co.car_model = '6-110'
AND co.subcategory = 'introduction'

which outputs exactly what I wanted!

Thanks so much for helping a coder in distress on Christmas day! :)

Seb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭白衣梦中忆 2024-12-29 20:31:49

我无法向您提供与我头脑中的语法无关的语法,但请考虑使用子查询来获取最近的备份日期并将其添加到主查询的 WHERE 子句中。然后您可以松开 ORDER 子句,因为您将得到一个结果(并且也松开 COUNT)。

I can't give you the syntax off the type of my head, but consider a subquery to get the most recent backup date and add this to your WHERE clause of the main query. Then you can loose the ORDER clause as you will get one result (and also loose the COUNT).

无法回应 2024-12-29 20:31:49

这应该会给你想要的。关键是创建一个派生表,为您提供正在处理的articleid 的最新备份。然后在外部查询中使用该信息来获取标题、内容和备份日期:(注释以非代码形式散布)

SELECT co.title, co.content, co.id, 
bu.backupBy,
mostrecent.MostRecentBackup,  
mostrecent.NumOfBackups
FROM content co inner join

这是派生表,用于获取上次备份以及包含该记录的记录的 ID然后

(select bu.id
       max(bu.backupdate) as MostRecentBackUP,
       count(bu.id) as NumOfBackups
from 
wol_content_backup bu
Group by bu.id
) mostrecent

你进入带有articleid的内容表

on co.id = mostrecent.articleid

,并返回到wol_content_backup表中的backupBy字段

inner join wol_content_backup buouter on
mostrecent.id = buouter.id

WHERE co.car_model = '6-110'
AND co.subcategory = 'introduction'
ORDER BY MostRecentBackup DESC

This should give you want you want. The key is to do a derived table that gets you the most recent backup for the articleid(s) you're dealing with. And then uses that information in the outer query to get your title, content, and backupby dates: (comments interspersed as non-code)

SELECT co.title, co.content, co.id, 
bu.backupBy,
mostrecent.MostRecentBackup,  
mostrecent.NumOfBackups
FROM content co inner join

This is the derived table that does the work of getting the last backup and the ID of the record that contains it

(select bu.id
       max(bu.backupdate) as MostRecentBackUP,
       count(bu.id) as NumOfBackups
from 
wol_content_backup bu
Group by bu.id
) mostrecent

Then you go to the content table with the articleid

on co.id = mostrecent.articleid

and go back to the wol_content_backup table for the backupBy field

inner join wol_content_backup buouter on
mostrecent.id = buouter.id

WHERE co.car_model = '6-110'
AND co.subcategory = 'introduction'
ORDER BY MostRecentBackup DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文