mysql distinct 失效,为什么我的MySQL DISTINCT不起作用?

mysql distinct 失效,为什么我的MySQL DISTINCT不起作用?Whythetwoquerybelowreturnduplicatemember_idandnotthethird?Ineedthesecondquerytoworkwithdistinct.AnytimeirunaGROUPBY,thisqueryisincrediblyslowandtheresultsetdoesn’tr…

mysql distinct 失效,为什么我的MySQL DISTINCT不起作用?

Why the two query below return duplicate member_id and not the third?

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 |

+———–+

my table sample

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;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/36919.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注