如何在MySQL中通过多列主键选择多行? [英] How to select multiple rows by multi-column primary key in MySQL?

查看:157
本文介绍了如何在MySQL中通过多列主键选择多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多列主键(城市/州/日期)和更多数据列的表.我正在寻找每个城市/州的最新数据.我该如何干净/有效地做到这一点?现在,我可以通过执行第一个查询来获取要尝试获取的所有行的列表来执行此操作,然后执行第二个查询以包含大量WHERE子句:

I have a table with a multi-column primary key (city/state/date) and many more columns of data. I'm looking to get the latest data for each city/state. How do I do that cleanly/efficiently? Right now I can do this by doing a first query to get the list of all the rows I'm trying to fetch, followed by a second query with a massive WHERE clause:

 SELECT state, city, max(date) from data GROUP BY city, state;

+-------+---------------------+------------+
| state | city                | MAX(date)  |
+-------+---------------------+------------+
| CA    | San Francisco       | 2013-09-01 |
| CA    | Los Angeles         | 2013-08-01 |
| NY    | New York            | 2013-10-01 |
| ...   | ... (many rows) ... | ...        |
+-------+---------------------+------------+


SELECT * FROM data WHERE 
    (state = "CA" AND city = "San Francisco" AND date='2013-09-01') OR 
    (state = "CA" AND city = "Los Angeles" AND date='2013-08-01') OR 
    (state = "NY" AND city = "New York" AND date='2013-10-01') OR 
    ...

这确实很丑陋且效率低下,如果第一个查询返回很多行,我的第二个查询可能太长.显然,如果我具有单列主键,则可以使用带有IN()的子选择,但这在这里实际上是不可能的.有什么建议吗?

This is really ugly and inefficient, and if the first query returns a lot of rows my second query might be too long. Clearly if I have a single-column primary key I could use a subselect with IN(), but that's not really possible here. Any suggestions?

更新:我尝试用子选择尝试Bill的建议,但是它没有使用任何键,而且会花很多时间.如果我将子选择限制为仅返回5行,则它将在0.64s内返回.如果我让它返回所有73个城市/州的组合,则将花费很长时间(查询仍在运行).

UPDATE: I tried Bill's suggestion with a subselect, but it's not using any keys and is taking forever. If I restrict the subselect to only return 5 rows it returns in 0.64s. If I let it return all 73 city/state combinations, it takes a very long time (query still running).

EXPLAIN SELECT * FROM data WHERE (city, state, date) IN (SELECT state, city, MAX(date) FROM data GROUP BY city, state)
+----+--------------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| id | select_type        | table | type  | possible_keys | key     | key_len | ref  | rows  | Extra       |
+----+--------------------+-------+-------+---------------+---------+---------+------+-------+-------------+
|  1 | PRIMARY            | data  | ALL   | NULL          | NULL    | NULL    | NULL | 13342 | Using where |
|  2 | DEPENDENT SUBQUERY | data  | index | NULL          | PRIMARY | 57      | NULL |  8058 | Using index |
+----+--------------------+-------+-------+---------------+---------+---------+------+-------+-------------+

推荐答案

我认为这应该为您解决问题:

I think this should do the trick for you:

select 
    * 
from 
    data t1
natural join 
    ( 
        select 
            city, 
            state, 
            max(date) as date
        from 
            data
        group by 
            city, 
            state
    ) t2;

这篇关于如何在MySQL中通过多列主键选择多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆