~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysql-test/t/distinct.test

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#
48
48
# Problem with distinct without results
49
49
#
50
 
CREATE TABLE t1 (UserId int DEFAULT '0' NOT NULL);
 
50
CREATE TABLE t1 (UserId int(11) DEFAULT '0' NOT NULL);
51
51
INSERT INTO t1 VALUES (20);
52
52
INSERT INTO t1 VALUES (27);
53
53
 
61
61
# Test of distinct
62
62
#
63
63
 
64
 
CREATE TABLE t1 (a int not null primary key,b int);
 
64
CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned);
65
65
INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1);
66
 
CREATE TABLE t2 (a int not null, key (A));
 
66
CREATE TABLE t2 (a int(10) unsigned not null, key (A));
67
67
INSERT INTO t2 VALUES (1),(2);
68
 
CREATE TABLE t3 (a int, key(A), b text);
 
68
CREATE TABLE t3 (a int(10) unsigned, key(A), b text);
69
69
INSERT INTO t3 VALUES (1,'1'),(2,'2');
70
70
SELECT DISTINCT t3.b FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
71
71
INSERT INTO t2 values (1),(2),(3);
86
86
explain select distinct t1.a from t1,t3 where t1.a=t3.a;
87
87
#flush status;
88
88
select distinct t1.a from t1,t3 where t1.a=t3.a;
89
 
--replace_column 2 #
90
 
show status like 'Handler%';
91
 
flush status;
 
89
#show status like 'Handler%';
 
90
#flush status;
92
91
select distinct 1 from t1,t3 where t1.a=t3.a;
93
 
--replace_column 2 #
94
 
show status like 'Handler%';
 
92
#show status like 'Handler%';
95
93
 
96
94
explain SELECT distinct t1.a from t1;
97
95
explain SELECT distinct t1.a from t1 order by a desc;
112
110
drop table t1;
113
111
 
114
112
CREATE TABLE t1 (
115
 
  ID int NOT NULL auto_increment,
 
113
  ID int(11) NOT NULL auto_increment,
116
114
  NAME varchar(75) DEFAULT '' NOT NULL,
117
 
  LINK_ID int DEFAULT '0' NOT NULL,
 
115
  LINK_ID int(11) DEFAULT '0' NOT NULL,
118
116
  PRIMARY KEY (ID),
119
117
  KEY NAME (NAME),
120
118
  KEY LINK_ID (LINK_ID)
123
121
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0),(2,'Jack',0),(3,'Bill',0);
124
122
 
125
123
CREATE TABLE t2 (
126
 
  ID int NOT NULL auto_increment,
 
124
  ID int(11) NOT NULL auto_increment,
127
125
  NAME varchar(150) DEFAULT '' NOT NULL,
128
126
  PRIMARY KEY (ID),
129
127
  KEY NAME (NAME)
198
196
drop table t1,t2,t3;
199
197
 
200
198
#
 
199
# Test using DISTINCT on a function that contains a group function
 
200
# This also test the case when one doesn't use all fields in GROUP BY.
 
201
#
 
202
 
 
203
create table t1 (a int not null, b int not null, t time);
 
204
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");
 
205
select a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
 
206
select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
 
207
create table t2 (a int not null primary key, b int);
 
208
insert into t2 values (1,1),(2,2),(3,3);
 
209
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;
 
210
select 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;
 
211
drop table t1,t2;
 
212
 
 
213
#
201
214
# Test problem with DISTINCT and HAVING
202
215
#
203
216
create table t1 (a int not null,b char(5), c text);
210
223
# Test problem with DISTINCT and ORDER BY DESC
211
224
#
212
225
 
213
 
create table t1 (a char(1), key(a));
 
226
create table t1 (a char(1), key(a)) engine=myisam;
214
227
insert into t1 values('1'),('1');
215
228
select * from t1 where a >= '1'; 
216
229
select distinct a from t1 order by a desc;
235
248
      (1, 'Z001'),
236
249
      (2, 'R002');
237
250
 
238
 
--sorted_result
239
251
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID;
240
252
SELECT DISTINCTROW email FROM t1 ORDER BY dateentered DESC;
241
253
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID ORDER BY dateentered DESC;
245
257
# test with table.* in DISTINCT
246
258
#
247
259
 
248
 
CREATE TABLE t1 (privatemessageid int NOT NULL auto_increment,  folderid int NOT NULL default '0',  userid int NOT NULL default '0',  touserid int NOT NULL default '0',  fromuserid int NOT NULL default '0',  title varchar(250) NOT NULL default '',  message mediumtext NOT NULL,  dateline int NOT NULL default '0',  showsignature int NOT NULL default '0',  iconid int NOT NULL default '0',  messageread int NOT NULL default '0',  readtime int NOT NULL default '0',  receipt int NOT NULL default '0',  deleteprompt int NOT NULL default '0',  multiplerecipients int NOT NULL default '0',  PRIMARY KEY  (privatemessageid),  KEY userid (userid));
 
260
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;
249
261
INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);
250
 
CREATE TABLE t2 (userid int NOT NULL auto_increment,  usergroupid int NOT NULL default '0',  username varchar(50) NOT NULL default '',  password varchar(50) NOT NULL default '',  email varchar(50) NOT NULL default '',  styleid int NOT NULL default '0',  parentemail varchar(50) NOT NULL default '',  coppauser int 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 int NOT NULL default '0',  showemail int NOT NULL default '0',  invisible int NOT NULL default '0',  usertitle varchar(250) NOT NULL default '',  customtitle int NOT NULL default '0',  joindate int NOT NULL default '0',  cookieuser int NOT NULL default '0',  daysprune int NOT NULL default '0',  lastvisit int NOT NULL default '0',  lastactivity int NOT NULL default '0',  lastpost int NOT NULL default '0',  posts int NOT NULL default '0',  timezoneoffset varchar(4) NOT NULL default '',  emailnotification int NOT NULL default '0',  buddylist mediumtext NOT NULL,  ignorelist mediumtext NOT NULL,  pmfolders mediumtext NOT NULL,  receivepm int NOT NULL default '0',  emailonpm int NOT NULL default '0',  pmpopup int NOT NULL default '0',  avatarid int NOT NULL default '0',  avatarrevision int NOT NULL default '0',  options int NOT NULL default '15',  birthday date NULL,  maxposts int NOT NULL default '-1',  startofweek int NOT NULL default '1',  ipaddress varchar(20) NOT NULL default '',  referrerid int NOT NULL default '0',  nosessionhash int NOT NULL default '0',  autorefresh int NOT NULL default '-1',  messagepopup int NOT NULL default '0',  inforum int NOT NULL default '0',  ratenum int NOT NULL default '0',  ratetotal int NOT NULL default '0',  allowrate int NOT NULL default '1',  PRIMARY KEY  (userid),  KEY usergroupid (usergroupid),  KEY username (username),  KEY inforum (inforum));
251
 
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,NULL,-1,1,'64.0.0.0',0,1,-1,0,0,4,19,1);
 
262
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;
 
263
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);
252
264
SELECT DISTINCT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t2.userid = t1.touserid);
253
265
DROP TABLE t1,t2;
254
266
 
267
279
# Test of LEFT() with distinct
268
280
#
269
281
 
270
 
CREATE table t1 (  `id` int NOT NULL auto_increment,  `name` varchar(50) NOT NULL default '',  PRIMARY KEY  (`id`)) AUTO_INCREMENT=3 ;
 
282
CREATE table t1 (  `id` int(11) NOT NULL auto_increment,  `name` varchar(50) NOT NULL default '',  PRIMARY KEY  (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 ;
271
283
INSERT INTO t1 VALUES (1, 'aaaaa');
272
284
INSERT INTO t1 VALUES (3, 'aaaaa');
273
285
INSERT INTO t1 VALUES (2, 'eeeeeee');
279
291
#
280
292
 
281
293
CREATE TABLE t1 (
282
 
  ID int NOT NULL auto_increment,
 
294
  ID int(11) NOT NULL auto_increment,
283
295
  NAME varchar(75) DEFAULT '' NOT NULL,
284
 
  LINK_ID int DEFAULT '0' NOT NULL,
 
296
  LINK_ID int(11) DEFAULT '0' NOT NULL,
285
297
  PRIMARY KEY (ID),
286
298
  KEY NAME (NAME),
287
299
  KEY LINK_ID (LINK_ID)
292
304
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (3,'Bill',0);
293
305
 
294
306
CREATE TABLE t2 (
295
 
  ID int NOT NULL auto_increment,
 
307
  ID int(11) NOT NULL auto_increment,
296
308
  NAME varchar(150) DEFAULT '' NOT NULL,
297
309
  PRIMARY KEY (ID),
298
310
  KEY NAME (NAME)
313
325
 
314
326
CREATE TABLE t1 (
315
327
  html varchar(5) default NULL,
316
 
  rin int default '0',
317
 
  rout int default '0'
318
 
);
 
328
  rin int(11) default '0',
 
329
  rout int(11) default '0'
 
330
) ENGINE=MyISAM;
319
331
 
320
332
INSERT INTO t1 VALUES ('1',1,0);
321
333
SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;
346
358
EXPLAIN SELECT DISTINCT a FROM t1;
347
359
EXPLAIN SELECT DISTINCT a,b FROM t1;
348
360
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2;
349
 
--sorted_result
350
361
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2
351
362
  WHERE t1_1.a = t1_2.a;
352
363
EXPLAIN SELECT a FROM t1 GROUP BY a;
393
404
# Bug #15745 ( COUNT(DISTINCT CONCAT(x,y)) returns wrong result)
394
405
#
395
406
CREATE TABLE t1 (
396
 
  ID int NOT NULL auto_increment,
 
407
  ID int(11) NOT NULL auto_increment,
397
408
  x varchar(20) default NULL,
398
409
  y decimal(10,0) default NULL,
399
410
  PRIMARY KEY  (ID),
400
411
  KEY (y)
401
 
);
 
412
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
402
413
 
403
414
INSERT INTO t1 VALUES
404
415
(1,'ba','-1'),
427
438
SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
428
439
 
429
440
DROP TABLE t1;
 
441
# The test case for bug#20836 should be re-enabled when bug#16861 is resolved
 
442
# The results for the test should be the same as in 4.1.
 
443
#
 
444
#Bug #20836: Selecting into variables results in wrong results being returned
 
445
#
 
446
#--disable_warnings
 
447
#DROP TABLE IF EXISTS t1;
 
448
#--enable_warnings
 
449
#
 
450
#CREATE TABLE t1 (id INT NOT NULL, fruit_id INT NOT NULL, fruit_name varchar(20)
 
451
#default NULL);
 
452
#
 
453
#INSERT INTO t1 VALUES (1,1,'ORANGE');
 
454
#INSERT INTO t1 VALUES (2,2,'APPLE');
 
455
#INSERT INTO t1 VALUES (3,2,'APPLE');
 
456
#INSERT INTO t1 VALUES (4,3,'PEAR');
 
457
#
 
458
#SELECT DISTINCT fruit_id, fruit_name INTO @v1, @v2 FROM t1 WHERE fruit_name = 
 
459
#'APPLE';
 
460
#SELECT @v1, @v2;
 
461
#
 
462
#SELECT DISTINCT fruit_id, fruit_name INTO @v3, @v4 FROM t1 GROUP BY fruit_id, 
 
463
#fruit_name HAVING fruit_name = 'APPLE';
 
464
#SELECT @v3, @v4;
 
465
#
 
466
#SELECT DISTINCT @v5:= fruit_id, @v6:= fruit_name INTO @v7, @v8 FROM t1 WHERE 
 
467
#fruit_name = 'APPLE';
 
468
#SELECT @v5, @v6, @v7, @v8;
 
469
#
 
470
#SELECT DISTINCT @v5 + fruit_id, CONCAT(@v6, fruit_name) INTO @v9, @v10 FROM t1 
 
471
#WHERE fruit_name = 'APPLE';
 
472
#SELECT @v5, @v6, @v7, @v8, @v9, @v10;
 
473
#
 
474
#SELECT DISTINCT @v11:= @v5 + fruit_id, @v12:= CONCAT(@v6, fruit_name) INTO 
 
475
#@v13, @v14 FROM t1 WHERE fruit_name = 'APPLE';
 
476
#SELECT @v11, @v12, @v13, @v14;
 
477
#
 
478
#SELECT DISTINCT @v13, @v14 INTO @v15, @v16 FROM t1 WHERE fruit_name = 'APPLE';
 
479
#SELECT @v15, @v16;
 
480
#
 
481
#SELECT DISTINCT 2 + 2, 'Bob' INTO @v17, @v18 FROM t1 WHERE fruit_name = 
 
482
#'APPLE';
 
483
#SELECT @v17, @v18;
 
484
#
 
485
#--disable_warnings
 
486
#DROP TABLE IF EXISTS t2;
 
487
#--enable_warnings
 
488
#
 
489
#CREATE TABLE t2 (fruit_id INT NOT NULL, fruit_name varchar(20)
 
490
#default NULL);
 
491
#
 
492
#SELECT DISTINCT fruit_id, fruit_name INTO OUTFILE 
 
493
#'../tmp/data1.tmp' FROM t1 WHERE fruit_name = 'APPLE';
 
494
#LOAD DATA INFILE '../tmp/data1.tmp' INTO TABLE t2;
 
495
#--exec rm $MYSQL_TEST_DIR/var/tmp/data1.tmp
 
496
#
 
497
#SELECT DISTINCT @v19:= fruit_id, @v20:= fruit_name INTO OUTFILE 
 
498
#'../tmp/data2.tmp' FROM t1 WHERE fruit_name = 'APPLE';
 
499
#LOAD DATA INFILE '../tmp/data2.tmp' INTO TABLE t2;
 
500
#--exec rm $MYSQL_TEST_DIR/var/tmp/data2.tmp
 
501
#
 
502
#SELECT @v19, @v20;
 
503
#SELECT * FROM t2;
 
504
#
 
505
#DROP TABLE t1;
 
506
#DROP TABLE t2;
 
507
 
 
508
#
 
509
# Bug #15881: cast problems
 
510
#
 
511
CREATE TABLE t1 (a CHAR(1)); INSERT INTO t1 VALUES('A'), (0);
 
512
SELECT a FROM t1 WHERE a=0;
 
513
SELECT DISTINCT a FROM t1 WHERE a=0;
 
514
DROP TABLE t1;
 
515
CREATE TABLE t1 (a DATE);
 
516
INSERT INTO t1 VALUES ('1972-07-29'), ('1972-02-06');
 
517
EXPLAIN SELECT (SELECT DISTINCT a FROM t1 WHERE a = '2002-08-03');
 
518
EXPLAIN SELECT (SELECT DISTINCT ADDDATE(a,1) FROM t1
 
519
                WHERE ADDDATE(a,1) = '2002-08-03');
 
520
CREATE TABLE t2 (a CHAR(5) CHARACTER SET latin1 COLLATE latin1_general_ci);
 
521
INSERT INTO t2 VALUES (0xf6);
 
522
INSERT INTO t2 VALUES ('oe');
 
523
 
 
524
SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2) dt;
 
525
SELECT COUNT(*) FROM 
 
526
  (SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt;
 
527
 
 
528
DROP TABLE t1, t2;
430
529
 
431
530
#
432
531
# Bug #25551: inconsistent behaviour in grouping NULL, depending on index type
458
557
#
459
558
# Bug #34928: Confusion by having Primary Key and Index
460
559
#
461
 
CREATE TABLE t1(a INT, b INT, c INT, d INT DEFAULT 0, e INT DEFAULT 0,
 
560
CREATE TABLE t1(a INT, b INT, c INT, d INT, e INT,
462
561
                PRIMARY KEY(a,b,c,d,e),
463
562
                KEY(a,b,d,c)
464
563
);