为什么我的MySQL DISTINCT不起作用? [英] Why my mysql DISTINCT doesn't work?
本文介绍了为什么我的MySQL DISTINCT不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么下面的两个查询返回重复的member_id而不是第三个?
Why the two query below return duplicate member_id and not the third?
我需要第二个查询才能与众不同.每当我运行 GROUP BY 时,此查询的速度都非常慢,并且结果集返回的值不会与distinct相同(该值是错误的).
I need the second query to work with distinct. Anytime i run a GROUP BY, this query is incredibly slow and the resultset doesn't return the same value as distinct (the value is wrong).
SELECT member_id, id
FROM ( SELECT * FROM table1 ORDER BY created_at desc ) as u
LIMIT 5
+-----------+--------+
| member_id | id |
+-----------+--------+
| 11333 | 313095 |
| 141831 | 313094 |
| 141831 | 313093 |
| 12013 | 313092 |
| 60821 | 313091 |
+-----------+--------+
SELECT distinct member_id, id
FROM ( SELECT * FROM table1 ORDER BY created_at desc ) as u
LIMIT 5
+-----------+--------+
| member_id | id |
+-----------+--------+
| 11333 | 313095 |
| 141831 | 313094 |
| 141831 | 313093 |
| 12013 | 313092 |
| 60821 | 313091 |
+-----------+--------+
SELECT distinct member_id
FROM ( SELECT * FROM table1 ORDER BY created_at desc ) as u
LIMIT 5
+-----------+
| member_id |
+-----------+
| 11333 |
| 141831 |
| 12013 |
| 60821 |
| 64980 |
+-----------+
我的餐桌样品
CREATE TABLE `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL,
`s_type_id` int(11) NOT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `s_FI_1` (`member_id`),
KEY `s_FI_2` (`s_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=313096 DEFAULT CHARSET=utf8;
推荐答案
它可以工作,它很脏(没有索引,没有键,临时表...),但是它可以工作,
it works, its dirty (no index, no key, temporary table...) but it works,
SELECT member_id,id
FROM ( SELECT member_id,id, created_at FROM table1 ORDER BY created_at desc ) as u
group by member_id ORDER BY created_at desc LIMIT 5;
这篇关于为什么我的MySQL DISTINCT不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文