~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Merged in changes. 
Edited a the comment test case so deal with our version bump.

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);
110
110
drop table t1;
111
111
 
112
112
CREATE TABLE t1 (
113
 
  ID int NOT NULL auto_increment,
 
113
  ID int(11) NOT NULL auto_increment,
114
114
  NAME varchar(75) DEFAULT '' NOT NULL,
115
 
  LINK_ID int DEFAULT '0' NOT NULL,
 
115
  LINK_ID int(11) DEFAULT '0' NOT NULL,
116
116
  PRIMARY KEY (ID),
117
117
  KEY NAME (NAME),
118
118
  KEY LINK_ID (LINK_ID)
121
121
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0),(2,'Jack',0),(3,'Bill',0);
122
122
 
123
123
CREATE TABLE t2 (
124
 
  ID int NOT NULL auto_increment,
 
124
  ID int(11) NOT NULL auto_increment,
125
125
  NAME varchar(150) DEFAULT '' NOT NULL,
126
126
  PRIMARY KEY (ID),
127
127
  KEY NAME (NAME)
196
196
drop table t1,t2,t3;
197
197
 
198
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
#
199
214
# Test problem with DISTINCT and HAVING
200
215
#
201
216
create table t1 (a int not null,b char(5), c text);
208
223
# Test problem with DISTINCT and ORDER BY DESC
209
224
#
210
225
 
211
 
create table t1 (a char(1), key(a));
 
226
create table t1 (a char(1), key(a)) engine=myisam;
212
227
insert into t1 values('1'),('1');
213
228
select * from t1 where a >= '1'; 
214
229
select distinct a from t1 order by a desc;
242
257
# test with table.* in DISTINCT
243
258
#
244
259
 
245
 
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;
246
261
INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);
247
 
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));
248
 
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);
249
264
SELECT DISTINCT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t2.userid = t1.touserid);
250
265
DROP TABLE t1,t2;
251
266
 
264
279
# Test of LEFT() with distinct
265
280
#
266
281
 
267
 
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 ;
268
283
INSERT INTO t1 VALUES (1, 'aaaaa');
269
284
INSERT INTO t1 VALUES (3, 'aaaaa');
270
285
INSERT INTO t1 VALUES (2, 'eeeeeee');
276
291
#
277
292
 
278
293
CREATE TABLE t1 (
279
 
  ID int NOT NULL auto_increment,
 
294
  ID int(11) NOT NULL auto_increment,
280
295
  NAME varchar(75) DEFAULT '' NOT NULL,
281
 
  LINK_ID int DEFAULT '0' NOT NULL,
 
296
  LINK_ID int(11) DEFAULT '0' NOT NULL,
282
297
  PRIMARY KEY (ID),
283
298
  KEY NAME (NAME),
284
299
  KEY LINK_ID (LINK_ID)
289
304
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (3,'Bill',0);
290
305
 
291
306
CREATE TABLE t2 (
292
 
  ID int NOT NULL auto_increment,
 
307
  ID int(11) NOT NULL auto_increment,
293
308
  NAME varchar(150) DEFAULT '' NOT NULL,
294
309
  PRIMARY KEY (ID),
295
310
  KEY NAME (NAME)
310
325
 
311
326
CREATE TABLE t1 (
312
327
  html varchar(5) default NULL,
313
 
  rin int default '0',
314
 
  rout int default '0'
315
 
);
 
328
  rin int(11) default '0',
 
329
  rout int(11) default '0'
 
330
) ENGINE=MyISAM;
316
331
 
317
332
INSERT INTO t1 VALUES ('1',1,0);
318
333
SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;
389
404
# Bug #15745 ( COUNT(DISTINCT CONCAT(x,y)) returns wrong result)
390
405
#
391
406
CREATE TABLE t1 (
392
 
  ID int NOT NULL auto_increment,
 
407
  ID int(11) NOT NULL auto_increment,
393
408
  x varchar(20) default NULL,
394
409
  y decimal(10,0) default NULL,
395
410
  PRIMARY KEY  (ID),
396
411
  KEY (y)
397
 
);
 
412
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
398
413
 
399
414
INSERT INTO t1 VALUES
400
415
(1,'ba','-1'),
423
438
SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
424
439
 
425
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;
426
529
 
427
530
#
428
531
# Bug #25551: inconsistent behaviour in grouping NULL, depending on index type
454
557
#
455
558
# Bug #34928: Confusion by having Primary Key and Index
456
559
#
457
 
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,
458
561
                PRIMARY KEY(a,b,c,d,e),
459
562
                KEY(a,b,d,c)
460
563
);