Mysql 多个不同的不起作用
我已经尝试了与 MySQL 不同的多种方法,但我似乎无法让任何东西起作用...我有一个表是历史表。同一栋大楼内多次出现不同状态的公寓。我需要为每个公寓找到最新的公寓(ID 最高的公寓 ORDER BY id)
id building appartment_id status
208 1 2 2
209 1 3 2
210 1 4 2
211 1 5 2
212 1 6 2
213 1 7 2
214 1 2 1
215 1 2 3
但是我该怎么做?! :S 我已经尝试过了:
SELECT *, GROUP_CONCAT(appartment_id, building)
FROM `ib30_history`
group by appartment_id, building
order by id DESC
它似乎有效,但我不确定这是正确的方法,并且使用输出的代码似乎使数据运行起来很有趣,所以我不确定它是否真的有效!
I have tried this multiple distinct from MySQL and I cant seem to get anything to work... I have a table that is a history table. The appartments can be found many times from the same building with different status. I need to find the newest one for each appartment (the one with the highest id ORDER BY id)
id building appartment_id status
208 1 2 2
209 1 3 2
210 1 4 2
211 1 5 2
212 1 6 2
213 1 7 2
214 1 2 1
215 1 2 3
But how do I do that?! :S
I have tried this:
SELECT *, GROUP_CONCAT(appartment_id, building)
FROM `ib30_history`
group by appartment_id, building
order by id DESC
It seems to work but im not sure that is the right way of doing it and the code that uses the output seems to make funny things running through the data so im not sure it really works!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
摆脱不同的并使用类似的东西:
GROUP BYbuilding,appartment_id
Get rid of distinct and use something like:
GROUP BY building , appartment_id
您正在寻找的称为
GROUP BY
,MySQL 的文档对如何使用它有很多了解。当我输入此内容时,OP不包含查询,所以我无法给你一个例子......What you're looking for is called
GROUP BY
, and MySQL's documentation knows a lot about how it's to be used. At the timeI type this, the OP doesn't contain a query so I cannot give you an example...