📄 distinct.result
字号:
);create table t2 (id int not null,idx int not null,unique (id, idx));create table t3 (id int not null,idx int not null,unique (id, idx));insert into t1 values (1,'yes'), (2,'no');insert into t2 values (1,1);insert into t3 values (1,1);EXPLAINSELECT DISTINCTt1.idfromt1straight_joint2straight_joint3straight_joint1 as j_lj_t2 left join t2 as t2_ljon j_lj_t2.id=t2_lj.idstraight_joint1 as j_lj_t3 left join t3 as t3_ljon j_lj_t3.id=t3_lj.idWHERE((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2))AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2));id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index id id 4 NULL 2 Using index; Using temporary1 SIMPLE t2 index id id 8 NULL 1 Using index; Distinct1 SIMPLE t3 index id id 8 NULL 1 Using index; Distinct1 SIMPLE j_lj_t2 index id id 4 NULL 2 Using where; Using index; Distinct1 SIMPLE t2_lj ref id id 4 test.j_lj_t2.id 1 Using where; Using index; Distinct1 SIMPLE j_lj_t3 index id id 4 NULL 2 Using where; Using index; Distinct1 SIMPLE t3_lj ref id id 4 test.j_lj_t3.id 1 Using where; Using index; DistinctSELECT DISTINCTt1.idfromt1straight_joint2straight_joint3straight_joint1 as j_lj_t2 left join t2 as t2_ljon j_lj_t2.id=t2_lj.idstraight_joint1 as j_lj_t3 left join t3 as t3_ljon j_lj_t3.id=t3_lj.idWHERE((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2))AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2));id2drop table t1,t2,t3;create table t1 (a int not null, b int not null, t time);insert into t1 values (1,1,"00:06:15"),(1,2,"00:06:15"),(1,2,"00:30:15"),(1,3,"00:06:15"),(1,3,"00:30:15");select a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;a sec_to_time(sum(time_to_sec(t)))1 00:06:151 00:36:301 00:36:30select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;a sec_to_time(sum(time_to_sec(t)))1 00:06:151 00:36:30create table t2 (a int not null primary key, b int);insert into t2 values (1,1),(2,2),(3,3);select t1.a,sec_to_time(sum(time_to_sec(t))) from t1 left join t2 on (t1.b=t2.a) group by t1.a,t2.b;a sec_to_time(sum(time_to_sec(t)))1 00:06:151 00:36:301 00:36:30select distinct t1.a,sec_to_time(sum(time_to_sec(t))) from t1 left join t2 on (t1.b=t2.a) group by t1.a,t2.b;a sec_to_time(sum(time_to_sec(t)))1 00:06:151 00:36:30drop table t1,t2;create table t1 (a int not null,b char(5), c text);insert into t1 (a) values (1),(2),(3),(4),(1),(2),(3),(4);select distinct a from t1 group by b,a having a > 2 order by a desc;a43select distinct a,c from t1 group by b,c,a having a > 2 order by a desc;a c4 NULL3 NULLdrop table t1;create table t1 (a char(1), key(a)) engine=myisam;insert into t1 values('1'),('1');select * from t1 where a >= '1';a11select distinct a from t1 order by a desc;a1select distinct a from t1 where a >= '1' order by a desc;a1drop table t1;CREATE TABLE t1 (email varchar(50), infoID BIGINT, dateentered DATETIME);CREATE TABLE t2 (infoID BIGINT, shipcode varchar(10));INSERT INTO t1 (email, infoID, dateentered) VALUES('test1@testdomain.com', 1, '2002-07-30 22:56:38'),('test1@testdomain.com', 1, '2002-07-27 22:58:16'),('test2@testdomain.com', 1, '2002-06-19 15:22:19'),('test2@testdomain.com', 2, '2002-06-18 14:23:47'),('test3@testdomain.com', 1, '2002-05-19 22:17:32');INSERT INTO t2(infoID, shipcode) VALUES(1, 'Z001'),(2, 'R002');SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID;email shipcodetest1@testdomain.com Z001test2@testdomain.com Z001test2@testdomain.com R002test3@testdomain.com Z001SELECT DISTINCTROW email FROM t1 ORDER BY dateentered DESC;emailtest1@testdomain.comtest2@testdomain.comtest3@testdomain.comSELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID ORDER BY dateentered DESC;email shipcodetest1@testdomain.com Z001test2@testdomain.com Z001test2@testdomain.com R002test3@testdomain.com Z001drop table t1,t2;CREATE TABLE t1 (privatemessageid int(10) unsigned NOT NULL auto_increment, folderid smallint(6) NOT NULL default '0', userid int(10) unsigned NOT NULL default '0', touserid int(10) unsigned NOT NULL default '0', fromuserid int(10) unsigned NOT NULL default '0', title varchar(250) NOT NULL default '', message mediumtext NOT NULL, dateline int(10) unsigned NOT NULL default '0', showsignature smallint(6) NOT NULL default '0', iconid smallint(5) unsigned NOT NULL default '0', messageread smallint(6) NOT NULL default '0', readtime int(10) unsigned NOT NULL default '0', receipt smallint(6) unsigned NOT NULL default '0', deleteprompt smallint(6) unsigned NOT NULL default '0', multiplerecipients smallint(6) unsigned NOT NULL default '0', PRIMARY KEY (privatemessageid), KEY userid (userid)) ENGINE=MyISAM;INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);CREATE TABLE t2 (userid int(10) unsigned NOT NULL auto_increment, usergroupid smallint(5) unsigned NOT NULL default '0', username varchar(50) NOT NULL default '', password varchar(50) NOT NULL default '', email varchar(50) NOT NULL default '', styleid smallint(5) unsigned NOT NULL default '0', parentemail varchar(50) NOT NULL default '', coppauser smallint(6) NOT NULL default '0', homepage varchar(100) NOT NULL default '', icq varchar(20) NOT NULL default '', aim varchar(20) NOT NULL default '', yahoo varchar(20) NOT NULL default '', signature mediumtext NOT NULL, adminemail smallint(6) NOT NULL default '0', showemail smallint(6) NOT NULL default '0', invisible smallint(6) NOT NULL default '0', usertitle varchar(250) NOT NULL default '', customtitle smallint(6) NOT NULL default '0', joindate int(10) unsigned NOT NULL default '0', cookieuser smallint(6) NOT NULL default '0', daysprune smallint(6) NOT NULL default '0', lastvisit int(10) unsigned NOT NULL default '0', lastactivity int(10) unsigned NOT NULL default '0', lastpost int(10) unsigned NOT NULL default '0', posts smallint(5) unsigned NOT NULL default '0', timezoneoffset varchar(4) NOT NULL default '', emailnotification smallint(6) NOT NULL default '0', buddylist mediumtext NOT NULL, ignorelist mediumtext NOT NULL, pmfolders mediumtext NOT NULL, receivepm smallint(6) NOT NULL default '0', emailonpm smallint(6) NOT NULL default '0', pmpopup smallint(6) NOT NULL default '0', avatarid smallint(6) NOT NULL default '0', avatarrevision int(6) unsigned NOT NULL default '0', options smallint(6) NOT NULL default '15', birthday date NOT NULL default '0000-00-00', maxposts smallint(6) NOT NULL default '-1', startofweek smallint(6) NOT NULL default '1', ipaddress varchar(20) NOT NULL default '', referrerid int(10) unsigned NOT NULL default '0', nosessionhash smallint(6) NOT NULL default '0', autorefresh smallint(6) NOT NULL default '-1', messagepopup tinyint(2) NOT NULL default '0', inforum smallint(5) unsigned NOT NULL default '0', ratenum smallint(5) unsigned NOT NULL default '0', ratetotal smallint(5) unsigned NOT NULL default '0', allowrate smallint(5) unsigned NOT NULL default '1', PRIMARY KEY (userid), KEY usergroupid (usergroupid), KEY username (username), KEY inforum (inforum)) ENGINE=MyISAM;INSERT INTO t2 VALUES (33,6,'Kevin','0','kevin@stileproject.com',1,'',0,'http://www.stileproject.com','','','','',1,1,0,'Administrator',0,996120694,1,-1,1030996168,1031027028,1030599436,36,'-6',0,'','','',1,0,1,0,0,15,'0000-00-00',-1,1,'64.0.0.0',0,1,-1,0,0,4,19,1);SELECT DISTINCT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t2.userid = t1.touserid);privatemessageid folderid userid touserid fromuserid title message dateline showsignature iconid messageread readtime receipt deleteprompt multiplerecipients userid usergroupid username password email styleid parentemail coppauser homepage icq aim yahoo signature adminemail showemail invisible usertitle customtitle joindate cookieuser daysprune lastvisit lastactivity lastpost posts timezoneoffset emailnotification buddylist ignorelist pmfolders receivepm emailonpm pmpopup avatarid avatarrevision options birthday maxposts startofweek ipaddress referrerid nosessionhash autorefresh messagepopup inforum ratenum ratetotal allowrate128 0 33 33 8 :D 996121863 1 0 2 996122850 2 0 0 33 6 Kevin 0 kevin@stileproject.com 1 0 http://www.stileproject.com 1 1 0 Administrator 0 996120694 1 -1 1030996168 1031027028 1030599436 36 -6 0 1 0 1 0 0 15 0000-00-00 -1 1 64.0.0.0 0 1 -1 0 0 4 19 1DROP TABLE t1,t2;CREATE TABLE t1 (a int primary key, b int, c int);INSERT t1 VALUES (1,2,3);CREATE TABLE t2 (a int primary key, b int, c int);INSERT t2 VALUES (3,4,5);SELECT DISTINCT t1.a, t2.b FROM t1, t2 WHERE t1.a=1 ORDER BY t2.c;a b1 4DROP TABLE t1,t2;CREATE table t1 ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL default '', PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 ;INSERT INTO t1 VALUES (1, 'aaaaa');INSERT INTO t1 VALUES (3, 'aaaaa');INSERT INTO t1 VALUES (2, 'eeeeeee');select distinct left(name,1) as name from t1;nameaedrop 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);INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (2,'Jack',0);INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (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 (html varchar(5) default NULL,rin int(11) default '0',rout int(11) default '0') ENGINE=MyISAM;INSERT INTO t1 VALUES ('1',1,0);SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;html prod1 0.0000drop table t1;CREATE TABLE t1 (a int);INSERT INTO t1 VALUES (1),(2),(3),(4),(5);SELECT DISTINCT a, 1 FROM t1;a 11 12 13 14 15 1SELECT DISTINCT 1, a FROM t1;1 a1 11 21 31 41 5CREATE TABLE t2 (a int, b int);INSERT INTO t2 VALUES (1,1),(2,2),(2,3),(2,4),(3,5);SELECT DISTINCT a, b, 2 FROM t2;a b 21 1 22 2 22 3 22 4 23 5 2SELECT DISTINCT 2, a, b FROM t2;2 a b2 1 12 2 22 2 32 2 42 3 5SELECT DISTINCT a, 2, b FROM t2;a 2 b1 2 12 2 22 2 32 2 43 2 5DROP TABLE t1,t2;create table t1 (id int, dsc varchar(50));insert into t1 values (1, "line number one"), (2, "line number two"), (3, "line number three");select distinct id, IFNULL(dsc, '-') from t1;id IFNULL(dsc, '-')1 line number one2 line number two3 line number threedrop table t1;CREATE TABLE t1 (ID int(11) NOT NULL auto_increment,x varchar(20) default NULL,y decimal(10,0) default NULL,PRIMARY KEY (ID),KEY (y)) ENGINE=MyISAM DEFAULT CHARSET=latin1;INSERT INTO t1 VALUES(1,'ba','-1'),(2,'ba','1150'),(306,'ba','-1'),(307,'ba','1150'),(611,'ba','-1'),(612,'ba','1150');select count(distinct x,y) from t1;count(distinct x,y)2select count(distinct concat(x,y)) from t1;count(distinct concat(x,y))2drop table t1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -