Mysql 多个不同的不起作用

发布于 2024-12-09 14:52:51 字数 681 浏览 0 评论 0原文

我已经尝试了与 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 技术交流群。

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

发布评论

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

评论(4

千年*琉璃梦 2024-12-16 14:52:51
SELECT yourtable.id, yourtable.building, yourtable.appartment_id, yourtable.status
FROM yourtable
INNER JOIN (
    SELECT MAX(id) AS id
    FROM yourtable
    GROUP BY building, appartment_id
) AS child ON yourtable.id = child.id
SELECT yourtable.id, yourtable.building, yourtable.appartment_id, yourtable.status
FROM yourtable
INNER JOIN (
    SELECT MAX(id) AS id
    FROM yourtable
    GROUP BY building, appartment_id
) AS child ON yourtable.id = child.id
彩虹直至黑白 2024-12-16 14:52:51

摆脱不同的并使用类似的东西:

GROUP BYbuilding,appartment_id

Get rid of distinct and use something like:

GROUP BY building , appartment_id

半透明的墙 2024-12-16 14:52:51

您正在寻找的称为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...

一抹微笑 2024-12-16 14:52:51
SELECT a.* 
FROM table_name a
INNER JOIN 
(SELECT MAX(id) as max_id
 FROM table_name 
 GROUP BY appartment_id) b
ON (b.max_id = a.id)
SELECT a.* 
FROM table_name a
INNER JOIN 
(SELECT MAX(id) as max_id
 FROM table_name 
 GROUP BY appartment_id) b
ON (b.max_id = a.id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文