如何在不使用group by语句的情况下选择不同的行 [英] How to select distinct rows without using group by statement

查看:80
本文介绍了如何在不使用group by语句的情况下选择不同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A B C
1 1 1 
1 1 1 
2 2 2 
2 2 2 
3 3 3 
3 3 3 
4 4 4 
4 4 4 
5 5 5 
5 5 5 
5 5 5 
6 6 6
6 6 6 

我只输出不重复的行,而不使用group by语句.我不能使用group by,因为它会使mysql挂起.所以它应该返回

I am to output only the distinct rows without using the group by statement. I cannot use group by because it makes mysql hang. So it should return

1 1 1
2 2 2 
3 3 3 
4 4 4 
5 5 5 
6 6 6 

我正在使用DISTINCT进行内部联接.这也不起作用:

I am using DISTINCT in for an inner join.This does not work either:

SELECT DISTINCT * FROM TABLEA inner join TABLEB on TABLEA.A = TABLEB.A 

推荐答案

SELECT DISTINCT A,B,C FROM TABLE;

根据mysql文档,DISTINCT指定从结果集中删除重复的行(

According to mysql documentation, DISTINCT specifies removal of duplicate rows from the result set (http://dev.mysql.com/doc/refman/5.0/en/select.html)

我在jsfiddle上创建了一个示例,它可以恕我直言

I created a sample on jsfiddle and it works IMHO

create table tablea (A int,B int,C int);
create table tableb (A int);

INSERT INTO tablea VALUES (1,1,1),(1,1,1),(2,2,2),(2,2,2),(3,3,3),(3,3,3),(4,4,4),(4,4,4),(5,5,5),(5,5,5),(5,5,5),(6,6,6),(6,6,6);
INSERT INTO tableb VALUES (1),(1),(1),(1),(1);

SELECT DISTINCT tablea.A,tablea.B,tablea.C FROM tablea INNER JOIN tableb ON tablea.A=tableb.A;

可以免费试用 SQLFiddle .

这篇关于如何在不使用group by语句的情况下选择不同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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