📄 distinct.result
字号:
drop table if exists t1,t2,t3;CREATE TABLE t1 (id int,facility char(20));CREATE TABLE t2 (facility char(20));INSERT INTO t1 VALUES (NULL,NULL);INSERT INTO t1 VALUES (-1,'');INSERT INTO t1 VALUES (0,'');INSERT INTO t1 VALUES (1,'/L');INSERT INTO t1 VALUES (2,'A01');INSERT INTO t1 VALUES (3,'ANC');INSERT INTO t1 VALUES (4,'F01');INSERT INTO t1 VALUES (5,'FBX');INSERT INTO t1 VALUES (6,'MT');INSERT INTO t1 VALUES (7,'P');INSERT INTO t1 VALUES (8,'RV');INSERT INTO t1 VALUES (9,'SRV');INSERT INTO t1 VALUES (10,'VMT');INSERT INTO t2 SELECT DISTINCT FACILITY FROM t1;select id from t1 group by id;idNULL-1012345678910select * from t1 order by id;id facilityNULL NULL-1 0 1 /L2 A013 ANC4 F015 FBX6 MT7 P8 RV9 SRV10 VMTselect id-5,facility from t1 order by "id-5";id-5 facilityNULL NULL-6 -5 -4 /L-3 A01-2 ANC-1 F010 FBX1 MT2 P3 RV4 SRV5 VMTselect id,concat(facility) from t1 group by id ;id concat(facility)NULL NULL-1 0 1 /L2 A013 ANC4 F015 FBX6 MT7 P8 RV9 SRV10 VMTselect id+0 as a,max(id),concat(facility) as b from t1 group by a order by b desc,a;a max(id) b10 10 VMT9 9 SRV8 8 RV7 7 P6 6 MT5 5 FBX4 4 F013 3 ANC2 2 A011 1 /L-1 -1 0 0 NULL NULL NULLselect id >= 0 and id <= 5 as grp,count(*) from t1 group by grp;grp count(*)NULL 10 61 6SELECT DISTINCT FACILITY FROM t1;FACILITYNULL/LA01ANCF01FBXMTPRVSRVVMTSELECT FACILITY FROM t2;FACILITYNULL/LA01ANCF01FBXMTPRVSRVVMTSELECT count(*) from t1,t2 where t1.facility=t2.facility;count(*)12select count(facility) from t1;count(facility)12select count(*) from t1;count(*)13select count(*) from t1 where facility IS NULL;count(*)1select count(*) from t1 where facility = NULL;count(*)0select count(*) from t1 where facility IS NOT NULL;count(*)12select count(*) from t1 where id IS NULL;count(*)1select count(*) from t1 where id IS NOT NULL;count(*)12drop table t1,t2;CREATE TABLE t1 (UserId int(11) DEFAULT '0' NOT NULL);INSERT INTO t1 VALUES (20);INSERT INTO t1 VALUES (27);SELECT UserId FROM t1 WHERE Userid=22;UserIdSELECT UserId FROM t1 WHERE UserId=22 group by Userid;UserIdSELECT DISTINCT UserId FROM t1 WHERE UserId=22 group by Userid;UserIdSELECT DISTINCT UserId FROM t1 WHERE UserId=22;UserIddrop table t1;CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned);INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1);CREATE TABLE t2 (a int(10) unsigned not null, key (A));INSERT INTO t2 VALUES (1),(2);CREATE TABLE t3 (a int(10) unsigned, key(A), b text);INSERT INTO t3 VALUES (1,'1'),(2,'2');SELECT DISTINCT t3.b FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;b1INSERT INTO t2 values (1),(2),(3);INSERT INTO t3 VALUES (1,'1'),(2,'2'),(1,'1'),(2,'2');explain SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 4 Using temporary1 SIMPLE t3 ref a a 5 test.t1.b 2 Using where; Using index1 SIMPLE t2 index a a 4 NULL 4 Using where; Using index; DistinctSELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;a1create temporary table t4 select * from t3;insert into t3 select * from t4;insert into t4 select * from t3;insert into t3 select * from t4;insert into t4 select * from t3;insert into t3 select * from t4;insert into t4 select * from t3;insert into t3 select * from t4;explain select distinct t1.a from t1,t3 where t1.a=t3.a;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 4 Using index; Using temporary1 SIMPLE t3 ref a a 5 test.t1.a 11 Using where; Using index; Distinctselect distinct t1.a from t1,t3 where t1.a=t3.a;a12select distinct 1 from t1,t3 where t1.a=t3.a;11explain SELECT distinct t1.a from t1;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using indexexplain SELECT distinct t1.a from t1 order by a desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using indexexplain SELECT t1.a from t1 group by a order by a desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using indexexplain SELECT distinct t1.a from t1 order by a desc limit 1;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using indexexplain SELECT distinct a from t3 order by a desc limit 2;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t3 index NULL a 5 NULL 10 Using indexexplain SELECT distinct a,b from t3 order by a+1;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t3 ALL NULL NULL NULL NULL 204 Using temporary; Using filesortexplain SELECT distinct a,b from t3 order by a limit 10;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t3 index NULL a 5 NULL 204 Using temporaryexplain SELECT a,b from t3 group by a,b order by a+1;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t3 ALL NULL NULL NULL NULL 204 Using temporary; Using filesortdrop table t1,t2,t3,t4;CREATE TABLE t1 (name varchar(255));INSERT INTO t1 VALUES ('aa'),('ab'),('ac'),('ad'),('ae');SELECT DISTINCT * FROM t1 LIMIT 2;nameaaabSELECT DISTINCT name FROM t1 LIMIT 2;nameaaabSELECT DISTINCT 1 FROM t1 LIMIT 2;11drop table t1;CREATE TABLE t1 (ID int(11) NOT NULL auto_increment,NAME varchar(75) DEFAULT '' NOT NULL,LINK_ID int(11) DEFAULT '0' NOT NULL,PRIMARY KEY (ID),KEY NAME (NAME),KEY LINK_ID (LINK_ID));INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0),(2,'Jack',0),(3,'Bill',0);CREATE TABLE t2 (ID int(11) NOT NULL auto_increment,NAME varchar(150) DEFAULT '' NOT NULL,PRIMARY KEY (ID),KEY NAME (NAME));SELECT DISTINCTt2.id AS key_link_id,t2.name AS linkFROM t1LEFT JOIN t2 ON t1.link_id=t2.idGROUP BY t1.idORDER BY link;key_link_id linkNULL NULLdrop table t1,t2;create table t1 (id int not null,name tinytext not null,unique (id)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -