~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to mysql-test/r/join.result

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
drop table if exists t1,t2,t3;
 
2
CREATE TABLE t1 (S1 INT);
 
3
CREATE TABLE t2 (S1 INT);
 
4
INSERT INTO t1 VALUES (1);
 
5
INSERT INTO t2 VALUES (2);
 
6
SELECT * FROM t1 JOIN t2;
 
7
S1      S1
 
8
1       2
 
9
SELECT * FROM t1 INNER JOIN t2;
 
10
S1      S1
 
11
1       2
 
12
SELECT * from t1 JOIN t2 USING (S1);
 
13
S1
 
14
SELECT * FROM t1 INNER JOIN t2 USING (S1);
 
15
S1
 
16
SELECT * from t1 CROSS JOIN t2;
 
17
S1      S1
 
18
1       2
 
19
SELECT * from t1 LEFT JOIN t2 USING(S1);
 
20
S1
 
21
1
 
22
SELECT * from t1 LEFT JOIN t2 ON(t2.S1=2);
 
23
S1      S1
 
24
1       2
 
25
SELECT * from t1 RIGHT JOIN t2 USING(S1);
 
26
S1
 
27
2
 
28
SELECT * from t1 RIGHT JOIN t2 ON(t1.S1=1);
 
29
S1      S1
 
30
1       2
 
31
drop table t1,t2;
 
32
create table t1 (id int primary key);
 
33
create table t2 (id int);
 
34
insert into t1 values (75);
 
35
insert into t1 values (79);
 
36
insert into t1 values (78);
 
37
insert into t1 values (77);
 
38
replace into t1 values (76);
 
39
replace into t1 values (76);
 
40
insert into t1 values (104);
 
41
insert into t1 values (103);
 
42
insert into t1 values (102);
 
43
insert into t1 values (101);
 
44
insert into t1 values (105);
 
45
insert into t1 values (106);
 
46
insert into t1 values (107);
 
47
insert into t2 values (107),(75),(1000);
 
48
select t1.id, t2.id from t1, t2 where t2.id = t1.id;
 
49
id      id
 
50
107     107
 
51
75      75
 
52
select t1.id, count(t2.id) from t1,t2 where t2.id = t1.id group by t1.id;
 
53
id      count(t2.id)
 
54
75      1
 
55
107     1
 
56
select t1.id, count(t2.id) from t1,t2 where t2.id = t1.id group by t2.id;
 
57
id      count(t2.id)
 
58
75      1
 
59
107     1
 
60
select t1.id,t2.id from t2 left join t1 on t1.id>=74 and t1.id<=0 where t2.id=75 and t1.id is null;
 
61
id      id
 
62
NULL    75
 
63
explain select t1.id,t2.id from t2 left join t1 on t1.id>=74 and t1.id<=0 where t2.id=75 and t1.id is null;
 
64
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
65
1       SIMPLE  t1      const   PRIMARY NULL    NULL    NULL    1       Impossible ON condition
 
66
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using where
 
67
explain select t1.id, t2.id from t1, t2 where t2.id = t1.id and t1.id <0 and t1.id > 0;
 
68
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
69
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
70
drop table t1,t2;
 
71
CREATE TABLE t1 (
 
72
id int(11) NOT NULL auto_increment,
 
73
token varchar(100) DEFAULT '' NOT NULL,
 
74
count int(11) DEFAULT '0' NOT NULL,
 
75
qty int(11),
 
76
phone char(1) DEFAULT '' NOT NULL,
 
77
timestamp datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
 
78
PRIMARY KEY (id),
 
79
KEY token (token(15)),
 
80
KEY timestamp (timestamp),
 
81
UNIQUE token_2 (token(75),count,phone)
 
82
);
 
83
INSERT INTO t1 VALUES (21,'e45703b64de71482360de8fec94c3ade',3,7800,'n','1999-12-23 17:22:21');
 
84
INSERT INTO t1 VALUES (22,'e45703b64de71482360de8fec94c3ade',4,5000,'y','1999-12-23 17:22:21');
 
85
INSERT INTO t1 VALUES (18,'346d1cb63c89285b2351f0ca4de40eda',3,13200,'b','1999-12-23 11:58:04');
 
86
INSERT INTO t1 VALUES (17,'ca6ddeb689e1b48a04146b1b5b6f936a',4,15000,'b','1999-12-23 11:36:53');
 
87
INSERT INTO t1 VALUES (16,'ca6ddeb689e1b48a04146b1b5b6f936a',3,13200,'b','1999-12-23 11:36:53');
 
88
INSERT INTO t1 VALUES (26,'a71250b7ed780f6ef3185bfffe027983',5,1500,'b','1999-12-27 09:44:24');
 
89
INSERT INTO t1 VALUES (24,'4d75906f3c37ecff478a1eb56637aa09',3,5400,'y','1999-12-23 17:29:12');
 
90
INSERT INTO t1 VALUES (25,'4d75906f3c37ecff478a1eb56637aa09',4,6500,'y','1999-12-23 17:29:12');
 
91
INSERT INTO t1 VALUES (27,'a71250b7ed780f6ef3185bfffe027983',3,6200,'b','1999-12-27 09:44:24');
 
92
INSERT INTO t1 VALUES (28,'a71250b7ed780f6ef3185bfffe027983',3,5400,'y','1999-12-27 09:44:36');
 
93
INSERT INTO t1 VALUES (29,'a71250b7ed780f6ef3185bfffe027983',4,17700,'b','1999-12-27 09:45:05');
 
94
CREATE TABLE t2 (
 
95
id int(11) NOT NULL auto_increment,
 
96
category int(11) DEFAULT '0' NOT NULL,
 
97
county int(11) DEFAULT '0' NOT NULL,
 
98
state int(11) DEFAULT '0' NOT NULL,
 
99
phones int(11) DEFAULT '0' NOT NULL,
 
100
nophones int(11) DEFAULT '0' NOT NULL,
 
101
PRIMARY KEY (id),
 
102
KEY category (category,county,state)
 
103
);
 
104
INSERT INTO t2 VALUES (3,2,11,12,5400,7800);
 
105
INSERT INTO t2 VALUES (4,2,25,12,6500,11200);
 
106
INSERT INTO t2 VALUES (5,1,37,6,10000,12000);
 
107
select a.id, b.category as catid, b.state as stateid, b.county as countyid from t1 a, t2 b ignore index (primary) where (a.token ='a71250b7ed780f6ef3185bfffe027983') and (a.count = b.id);
 
108
id      catid   stateid countyid
 
109
27      2       12      11
 
110
28      2       12      11
 
111
29      2       12      25
 
112
26      1       6       37
 
113
select a.id, b.category as catid, b.state as stateid, b.county as
 
114
countyid from t1 a, t2 b where (a.token =
 
115
'a71250b7ed780f6ef3185bfffe027983') and (a.count = b.id) order by a.id;
 
116
id      catid   stateid countyid
 
117
26      1       6       37
 
118
27      2       12      11
 
119
28      2       12      11
 
120
29      2       12      25
 
121
drop table t1, t2;
 
122
create table t1 (a int primary key);
 
123
insert into t1 values(1),(2);
 
124
select t1.a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a);
 
125
a
 
126
1
 
127
2
 
128
select t1.a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a);
 
129
ERROR HY000: Too many tables; MySQL can only use XX tables in a join
 
130
select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a);
 
131
a
 
132
1
 
133
2
 
134
select a from t1 as t1 left join t1 as t2 using (a) left join t1 as t3 using (a) left join t1 as t4 using (a) left join t1 as t5 using (a) left join t1 as t6 using (a) left join t1 as t7 using (a) left join t1 as t8 using (a) left join t1 as t9 using (a) left join t1 as t10 using (a) left join t1 as t11 using (a) left join t1 as t12 using (a) left join t1 as t13 using (a) left join t1 as t14 using (a) left join t1 as t15 using (a) left join t1 as t16 using (a) left join t1 as t17 using (a) left join t1 as t18 using (a) left join t1 as t19 using (a) left join t1 as t20 using (a) left join t1 as t21 using (a) left join t1 as t22 using (a) left join t1 as t23 using (a) left join t1 as t24 using (a) left join t1 as t25 using (a) left join t1 as t26 using (a) left join t1 as t27 using (a) left join t1 as t28 using (a) left join t1 as t29 using (a) left join t1 as t30 using (a) left join t1 as t31 using (a) left join t1 as t32 using (a) left join t1 as t33 using (a) left join t1 as t34 using (a) left join t1 as t35 using (a) left join t1 as t36 using (a) left join t1 as t37 using (a) left join t1 as t38 using (a) left join t1 as t39 using (a) left join t1 as t40 using (a) left join t1 as t41 using (a) left join t1 as t42 using (a) left join t1 as t43 using (a) left join t1 as t44 using (a) left join t1 as t45 using (a) left join t1 as t46 using (a) left join t1 as t47 using (a) left join t1 as t48 using (a) left join t1 as t49 using (a) left join t1 as t50 using (a) left join t1 as t51 using (a) left join t1 as t52 using (a) left join t1 as t53 using (a) left join t1 as t54 using (a) left join t1 as t55 using (a) left join t1 as t56 using (a) left join t1 as t57 using (a) left join t1 as t58 using (a) left join t1 as t59 using (a) left join t1 as t60 using (a) left join t1 as t61 using (a) left join t1 as t62 using (a) left join t1 as t63 using (a) left join t1 as t64 using (a) left join t1 as t65 using (a);
 
135
ERROR HY000: Too many tables; MySQL can only use XX tables in a join
 
136
drop table t1;
 
137
CREATE TABLE t1 (
 
138
a int(11) NOT NULL,
 
139
b int(11) NOT NULL,
 
140
PRIMARY KEY  (a,b)
 
141
) ENGINE=MyISAM;
 
142
INSERT INTO t1 VALUES (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(2,3);
 
143
CREATE TABLE t2 (
 
144
a int(11) default NULL
 
145
) ENGINE=MyISAM;
 
146
INSERT INTO t2 VALUES (2),(3);
 
147
SELECT t1.a,t2.a,b FROM t1,t2 WHERE t1.a=t2.a AND (t1.a=1 OR t1.a=2) AND b>=1 AND b<=3;
 
148
a       a       b
 
149
2       2       3
 
150
DROP TABLE t1, t2;
 
151
CREATE TABLE t1 (d DATE NOT NULL);
 
152
CREATE TABLE t2 (d DATE NOT NULL);
 
153
INSERT INTO t1 (d) VALUES ('2001-08-01'),('0000-00-00');
 
154
SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE t2.d IS NULL;
 
155
d
 
156
2001-08-01
 
157
0000-00-00
 
158
SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE d IS NULL;
 
159
d
 
160
0000-00-00
 
161
SELECT * from t1 WHERE t1.d IS NULL;
 
162
d
 
163
0000-00-00
 
164
SELECT * FROM t1 WHERE 1/0 IS NULL;
 
165
d
 
166
2001-08-01
 
167
0000-00-00
 
168
DROP TABLE t1,t2;
 
169
CREATE TABLE t1 (
 
170
Document_ID varchar(50) NOT NULL default '',
 
171
Contractor_ID varchar(6) NOT NULL default '',
 
172
Language_ID char(3) NOT NULL default '',
 
173
Expiration_Date datetime default NULL,
 
174
Publishing_Date datetime default NULL,
 
175
Title text,
 
176
Column_ID varchar(50) NOT NULL default '',
 
177
PRIMARY KEY  (Language_ID,Document_ID,Contractor_ID)
 
178
);
 
179
INSERT INTO t1 VALUES ('xep80','1','ger','2001-12-31 20:00:00','2001-11-12 10:58:00','Kartenbestellung - jetzt auch online','anle'),('','999998','',NULL,NULL,NULL,'');
 
180
CREATE TABLE t2 (
 
181
Contractor_ID char(6) NOT NULL default '',
 
182
Language_ID char(3) NOT NULL default '',
 
183
Document_ID char(50) NOT NULL default '',
 
184
CanRead char(1) default NULL,
 
185
Customer_ID int(11) NOT NULL default '0',
 
186
PRIMARY KEY  (Contractor_ID,Language_ID,Document_ID,Customer_ID)
 
187
);
 
188
INSERT INTO t2 VALUES ('5','ger','xep80','1',999999),('1','ger','xep80','1',999999);
 
189
CREATE TABLE t3 (
 
190
Language_ID char(3) NOT NULL default '',
 
191
Column_ID char(50) NOT NULL default '',
 
192
Contractor_ID char(6) NOT NULL default '',
 
193
CanRead char(1) default NULL,
 
194
Active char(1) default NULL,
 
195
PRIMARY KEY  (Language_ID,Column_ID,Contractor_ID)
 
196
);
 
197
INSERT INTO t3 VALUES ('ger','home','1','1','1'),('ger','Test','1','0','0'),('ger','derclu','1','0','0'),('ger','clubne','1','0','0'),('ger','philos','1','0','0'),('ger','clubko','1','0','0'),('ger','clubim','1','1','1'),('ger','progra','1','0','0'),('ger','progvo','1','0','0'),('ger','progsp','1','0','0'),('ger','progau','1','0','0'),('ger','progku','1','0','0'),('ger','progss','1','0','0'),('ger','nachl','1','0','0'),('ger','mitgli','1','0','0'),('ger','mitsu','1','0','0'),('ger','mitbus','1','0','0'),('ger','ergmar','1','1','1'),('ger','home','4','1','1'),('ger','derclu','4','1','1'),('ger','clubne','4','0','0'),('ger','philos','4','1','1'),('ger','clubko','4','1','1'),('ger','clubim','4','1','1'),('ger','progra','4','1','1'),('ger','progvo','4','1','1'),('ger','progsp','4','1','1'),('ger','progau','4','0','0'),('ger','progku','4','1','1'),('ger','progss','4','1','1'),('ger','nachl','4','1','1'),('ger','mitgli','4','0','0'),('ger','mitsu','4','0','0'),('ger','mitbus','4','0','0'),('ger','ergmar','4','1','1'),('ger','progra2','1','0','0'),('ger','archiv','4','1','1'),('ger','anmeld','4','1','1'),('ger','thema','4','1','1'),('ger','edito','4','1','1'),('ger','madis','4','1','1'),('ger','enma','4','1','1'),('ger','madis','1','1','1'),('ger','enma','1','1','1'),('ger','vorsch','4','0','0'),('ger','veranst','4','0','0'),('ger','anle','4','1','1'),('ger','redak','4','1','1'),('ger','nele','4','1','1'),('ger','aukt','4','1','1'),('ger','callcenter','4','1','1'),('ger','anle','1','0','0');
 
198
delete from t1 where Contractor_ID='999998';
 
199
insert into t1 (Contractor_ID) Values ('999998');
 
200
SELECT DISTINCT COUNT(t1.Title) FROM t1,
 
201
t2, t3 WHERE 
 
202
t1.Document_ID='xep80' AND t1.Contractor_ID='1' AND 
 
203
t1.Language_ID='ger' AND '2001-12-21 23:14:24' >= 
 
204
Publishing_Date AND '2001-12-21 23:14:24' <= Expiration_Date AND 
 
205
t1.Document_ID = t2.Document_ID AND 
 
206
t1.Language_ID = t2.Language_ID AND 
 
207
t1.Contractor_ID = t2.Contractor_ID AND ( 
 
208
t2.Customer_ID = '4'  OR 
 
209
t2.Customer_ID = '999999'  OR 
 
210
t2.Customer_ID = '1' )AND t2.CanRead 
 
211
= '1'  AND t1.Column_ID=t3.Column_ID AND 
 
212
t1.Language_ID=t3.Language_ID AND ( 
 
213
t3.Contractor_ID = '4'  OR 
 
214
t3.Contractor_ID = '999999'  OR 
 
215
t3.Contractor_ID = '1') AND 
 
216
t3.CanRead='1' AND t3.Active='1';
 
217
COUNT(t1.Title)
 
218
1
 
219
SELECT DISTINCT COUNT(t1.Title) FROM t1,
 
220
t2, t3 WHERE 
 
221
t1.Document_ID='xep80' AND t1.Contractor_ID='1' AND 
 
222
t1.Language_ID='ger' AND '2001-12-21 23:14:24' >= 
 
223
Publishing_Date AND '2001-12-21 23:14:24' <= Expiration_Date AND 
 
224
t1.Document_ID = t2.Document_ID AND 
 
225
t1.Language_ID = t2.Language_ID AND 
 
226
t1.Contractor_ID = t2.Contractor_ID AND ( 
 
227
t2.Customer_ID = '4'  OR 
 
228
t2.Customer_ID = '999999'  OR 
 
229
t2.Customer_ID = '1' )AND t2.CanRead 
 
230
= '1'  AND t1.Column_ID=t3.Column_ID AND 
 
231
t1.Language_ID=t3.Language_ID AND ( 
 
232
t3.Contractor_ID = '4'  OR 
 
233
t3.Contractor_ID = '999999'  OR 
 
234
t3.Contractor_ID = '1') AND 
 
235
t3.CanRead='1' AND t3.Active='1';
 
236
COUNT(t1.Title)
 
237
1
 
238
drop table t1,t2,t3;
 
239
CREATE TABLE t1 (
 
240
t1_id int(11) default NULL,
 
241
t2_id int(11) default NULL,
 
242
type enum('Cost','Percent') default NULL,
 
243
cost_unit enum('Cost','Unit') default NULL,
 
244
min_value double default NULL,
 
245
max_value double default NULL,
 
246
t3_id int(11) default NULL,
 
247
item_id int(11) default NULL
 
248
) ENGINE=MyISAM;
 
249
INSERT INTO t1 VALUES (12,5,'Percent','Cost',-1,0,-1,-1),(14,4,'Percent','Cost',-1,0,-1,-1),(18,5,'Percent','Cost',-1,0,-1,-1),(19,4,'Percent','Cost',-1,0,-1,-1),(20,5,'Percent','Cost',100,-1,22,291),(21,5,'Percent','Cost',100,-1,18,291),(22,1,'Percent','Cost',100,-1,6,291),(23,1,'Percent','Cost',100,-1,21,291),(24,1,'Percent','Cost',100,-1,9,291),(25,1,'Percent','Cost',100,-1,4,291),(26,1,'Percent','Cost',100,-1,20,291),(27,4,'Percent','Cost',100,-1,7,202),(28,1,'Percent','Cost',50,-1,-1,137),(29,2,'Percent','Cost',100,-1,4,354),(30,2,'Percent','Cost',100,-1,9,137),(93,2,'Cost','Cost',-1,10000000,-1,-1);
 
250
CREATE TABLE t2 (
 
251
id int(10) unsigned NOT NULL auto_increment,
 
252
name varchar(255) default NULL,
 
253
PRIMARY KEY  (id)
 
254
) ENGINE=MyISAM;
 
255
INSERT INTO t2 VALUES (1,'s1'),(2,'s2'),(3,'s3'),(4,'s4'),(5,'s5');
 
256
select t1.*, t2.*  from t1, t2 where t2.id=t1.t2_id limit 2;
 
257
t1_id   t2_id   type    cost_unit       min_value       max_value       t3_id   item_id id      name
 
258
22      1       Percent Cost    100     -1      6       291     1       s1
 
259
23      1       Percent Cost    100     -1      21      291     1       s1
 
260
drop table t1,t2;
 
261
CREATE TABLE t1 (
 
262
siteid varchar(25) NOT NULL default '',
 
263
emp_id varchar(30) NOT NULL default '',
 
264
rate_code varchar(10) default NULL,
 
265
UNIQUE KEY site_emp (siteid,emp_id),
 
266
KEY siteid (siteid)
 
267
) ENGINE=MyISAM;
 
268
INSERT INTO t1 VALUES ('rivercats','psmith','cust'), ('rivercats','KWalker','cust');
 
269
CREATE TABLE t2 (
 
270
siteid varchar(25) NOT NULL default '',
 
271
rate_code varchar(10) NOT NULL default '',
 
272
base_rate float NOT NULL default '0',
 
273
PRIMARY KEY  (siteid,rate_code)
 
274
) ENGINE=MyISAM;
 
275
INSERT INTO t2 VALUES ('rivercats','cust',20);
 
276
SELECT emp.rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE emp.emp_id = 'psmith' AND lr.siteid = 'rivercats';
 
277
rate_code       base_rate
 
278
cust    20
 
279
SELECT emp.rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE lr.siteid = 'rivercats' AND emp.emp_id = 'psmith';
 
280
rate_code       base_rate
 
281
cust    20
 
282
SELECT rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE emp.emp_id = 'psmith' AND siteid = 'rivercats';
 
283
rate_code       base_rate
 
284
cust    20
 
285
SELECT rate_code, lr.base_rate FROM t1 AS emp LEFT JOIN t2 AS lr USING (siteid, rate_code) WHERE siteid = 'rivercats' AND emp.emp_id = 'psmith';
 
286
rate_code       base_rate
 
287
cust    20
 
288
drop table t1,t2;
 
289
CREATE TABLE t1 (ID INTEGER NOT NULL PRIMARY KEY, Value1 VARCHAR(255));
 
290
CREATE TABLE t2 (ID INTEGER NOT NULL PRIMARY KEY, Value2 VARCHAR(255));
 
291
INSERT INTO t1 VALUES (1, 'A');
 
292
INSERT INTO t2 VALUES (1, 'B');
 
293
SELECT * FROM t1 NATURAL JOIN t2 WHERE 1 AND (Value1 = 'A' AND Value2 <> 'B');
 
294
ID      Value1  Value2
 
295
SELECT * FROM t1 NATURAL JOIN t2 WHERE 1 AND Value1 = 'A' AND Value2 <> 'B';
 
296
ID      Value1  Value2
 
297
SELECT * FROM t1 NATURAL JOIN t2 WHERE (Value1 = 'A' AND Value2 <> 'B') AND 1;
 
298
ID      Value1  Value2
 
299
drop table t1,t2;
 
300
CREATE TABLE t1 (a int);
 
301
CREATE TABLE t2 (b int);
 
302
CREATE TABLE t3 (c int);
 
303
SELECT * FROM t1 NATURAL JOIN t2 NATURAL JOIN t3;
 
304
a       b       c
 
305
DROP TABLE t1, t2, t3;
 
306
create table t1 (i int);
 
307
create table t2 (i int);
 
308
create table t3 (i int);
 
309
insert into t1 values(1),(2);
 
310
insert into t2 values(2),(3);
 
311
insert into t3 values (2),(4);
 
312
select * from t1 natural left join t2;
 
313
i
 
314
1
 
315
2
 
316
select * from t1 left join t2 on (t1.i=t2.i);
 
317
i       i
 
318
1       NULL
 
319
2       2
 
320
select * from t1 natural left join t2 natural left join t3;
 
321
i
 
322
1
 
323
2
 
324
select * from t1 left join t2 on (t1.i=t2.i) left join t3 on (t2.i=t3.i);
 
325
i       i       i
 
326
1       NULL    NULL
 
327
2       2       2
 
328
select * from t3 natural right join t2;
 
329
i
 
330
2
 
331
3
 
332
select * from t3 right join t2 on (t3.i=t2.i);
 
333
i       i
 
334
2       2
 
335
NULL    3
 
336
select * from t3 natural right join t2 natural right join t1;
 
337
i
 
338
1
 
339
2
 
340
select * from t3 right join t2 on (t3.i=t2.i) right join t1 on (t2.i=t1.i);
 
341
i       i       i
 
342
NULL    NULL    1
 
343
2       2       2
 
344
select * from t1,t2 natural left join t3 order by t1.i,t2.i,t3.i;
 
345
i       i
 
346
1       2
 
347
1       3
 
348
2       2
 
349
2       3
 
350
select * from t1,t2 left join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i;
 
351
i       i       i
 
352
1       2       2
 
353
1       3       NULL
 
354
2       2       2
 
355
2       3       NULL
 
356
select t1.i,t2.i,t3.i from t2 natural left join t3,t1 order by t1.i,t2.i,t3.i;
 
357
i       i       i
 
358
1       2       2
 
359
1       3       NULL
 
360
2       2       2
 
361
2       3       NULL
 
362
select t1.i,t2.i,t3.i from t2 left join t3 on (t2.i=t3.i),t1 order by t1.i,t2.i,t3.i;
 
363
i       i       i
 
364
1       2       2
 
365
1       3       NULL
 
366
2       2       2
 
367
2       3       NULL
 
368
select * from t1,t2 natural right join t3 order by t1.i,t2.i,t3.i;
 
369
i       i
 
370
1       4
 
371
1       2
 
372
2       4
 
373
2       2
 
374
select * from t1,t2 right join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i;
 
375
i       i       i
 
376
1       NULL    4
 
377
1       2       2
 
378
2       NULL    4
 
379
2       2       2
 
380
select t1.i,t2.i,t3.i from t2 natural right join t3,t1 order by t1.i,t2.i,t3.i;
 
381
i       i       i
 
382
1       NULL    4
 
383
1       2       2
 
384
2       NULL    4
 
385
2       2       2
 
386
select t1.i,t2.i,t3.i from t2 right join t3 on (t2.i=t3.i),t1 order by t1.i,t2.i,t3.i;
 
387
i       i       i
 
388
1       NULL    4
 
389
1       2       2
 
390
2       NULL    4
 
391
2       2       2
 
392
drop table t1,t2,t3;
 
393
CREATE TABLE t1 (a int, b int default 0, c int default 1);
 
394
INSERT INTO t1 (a) VALUES (1),(2),(3),(4),(5),(6),(7),(8);
 
395
INSERT INTO t1 (a) SELECT a + 8 FROM t1;
 
396
INSERT INTO t1 (a) SELECT a + 16 FROM t1;
 
397
CREATE TABLE t2 (a int, d int, e int default 0);
 
398
INSERT INTO t2 (a, d) VALUES (1,1),(2,2),(3,3),(4,4);
 
399
INSERT INTO t2 (a, d) SELECT a+4, a+4 FROM t2;
 
400
INSERT INTO t2 (a, d) SELECT a+8, a+8 FROM t2;
 
401
EXPLAIN
 
402
SELECT STRAIGHT_JOIN t2.e FROM t1,t2 WHERE t2.d=1 AND t1.b=t2.e
 
403
ORDER BY t1.b, t1.c;
 
404
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
405
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    32      Using temporary; Using filesort
 
406
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    16      Using where; Using join buffer
 
407
SELECT STRAIGHT_JOIN t2.e FROM t1,t2 WHERE t2.d=1 AND t1.b=t2.e
 
408
ORDER BY t1.b, t1.c;
 
409
e
 
410
0
 
411
0
 
412
0
 
413
0
 
414
0
 
415
0
 
416
0
 
417
0
 
418
0
 
419
0
 
420
0
 
421
0
 
422
0
 
423
0
 
424
0
 
425
0
 
426
0
 
427
0
 
428
0
 
429
0
 
430
0
 
431
0
 
432
0
 
433
0
 
434
0
 
435
0
 
436
0
 
437
0
 
438
0
 
439
0
 
440
0
 
441
0
 
442
DROP TABLE t1,t2;
 
443
create table t1 (c int, b int);
 
444
create table t2 (a int, b int);
 
445
create table t3 (b int, c int);
 
446
create table t4 (y int, c int);
 
447
create table t5 (y int, z int);
 
448
create table t6 (a int, c int);
 
449
insert into t1 values (10,1);
 
450
insert into t1 values (3 ,1);
 
451
insert into t1 values (3 ,2);
 
452
insert into t2 values (2, 1);
 
453
insert into t3 values (1, 3);
 
454
insert into t3 values (1,10);
 
455
insert into t4 values (11,3);
 
456
insert into t4 values (2, 3);
 
457
insert into t5 values (11,4);
 
458
insert into t6 values (2, 3);
 
459
select * from t1 natural join t2;
 
460
b       c       a
 
461
1       10      2
 
462
1       3       2
 
463
select * from t1 natural join t2;
 
464
b       c       a
 
465
1       10      2
 
466
1       3       2
 
467
select b as a, c as b, a as c from t1 natural join t2;
 
468
a       b       c
 
469
1       10      2
 
470
1       3       2
 
471
select a as c, c as b, b as a from t1 natural join t2;
 
472
c       b       a
 
473
2       10      1
 
474
2       3       1
 
475
select t1.c, t1.b, t2.a from t1 join (t2 join t4 on b + 1 = y) on t1.c = t4.c;
 
476
c       b       a
 
477
3       1       2
 
478
3       2       2
 
479
select t1.c as b, t1.b as a, t2.a as c
 
480
from t1 join (t2 join t4 on b + 1 = y) on t1.c = t4.c;
 
481
b       a       c
 
482
3       1       2
 
483
3       2       2
 
484
select * from t1 natural join (t2 natural join t3);
 
485
c       b       a
 
486
10      1       2
 
487
3       1       2
 
488
select * from (t1 natural join t2) natural join (t3 natural join t4);
 
489
b       c       a       y
 
490
1       3       2       11
 
491
1       3       2       2
 
492
select * from (t1 natural join t2) natural left join (t3 natural join t4);
 
493
b       c       a       y
 
494
1       10      2       NULL
 
495
1       3       2       11
 
496
1       3       2       2
 
497
select * from (t3 natural join t4) natural right join (t1 natural join t2);
 
498
b       c       a       y
 
499
1       10      2       NULL
 
500
1       3       2       11
 
501
1       3       2       2
 
502
select * from (t1 natural left join t2) natural left join (t3 natural left join t4);
 
503
b       c       a       y
 
504
1       10      2       NULL
 
505
1       3       2       11
 
506
1       3       2       2
 
507
2       3       NULL    NULL
 
508
select * from (t4 natural right join t3) natural right join (t2 natural right join t1);
 
509
b       c       a       y
 
510
1       10      2       NULL
 
511
1       3       2       11
 
512
1       3       2       2
 
513
2       3       NULL    NULL
 
514
select * from t1 natural join t2 natural join t3 natural join t4;
 
515
c       b       a       y
 
516
3       1       2       11
 
517
3       1       2       2
 
518
select * from ((t1 natural join t2) natural join t3) natural join t4;
 
519
c       b       a       y
 
520
3       1       2       11
 
521
3       1       2       2
 
522
select * from t1 natural join (t2 natural join (t3 natural join t4));
 
523
c       b       a       y
 
524
3       1       2       11
 
525
3       1       2       2
 
526
select * from t5 natural right join (t4 natural right join ((t2 natural right join t1) natural right join t3));
 
527
y       c       b       a       z
 
528
11      3       1       2       4
 
529
2       3       1       2       NULL
 
530
NULL    10      1       2       NULL
 
531
select * from (t1 natural join t2), (t3 natural join t4);
 
532
b       c       a       c       b       y
 
533
1       10      2       3       1       11
 
534
1       10      2       3       1       2
 
535
1       3       2       3       1       11
 
536
1       3       2       3       1       2
 
537
select * from t5 natural join ((t1 natural join t2), (t3 natural join t4));
 
538
y       z       b       c       a       c       b
 
539
11      4       1       10      2       3       1
 
540
11      4       1       3       2       3       1
 
541
select * from  ((t1 natural join t2),  (t3 natural join t4)) natural join t5;
 
542
y       b       c       a       c       b       z
 
543
11      1       10      2       3       1       4
 
544
11      1       3       2       3       1       4
 
545
select * from t5 natural join ((t1 natural join t2) cross join (t3 natural join t4));
 
546
y       z       b       c       a       c       b
 
547
11      4       1       10      2       3       1
 
548
11      4       1       3       2       3       1
 
549
select * from  ((t1 natural join t2) cross join (t3 natural join t4)) natural join t5;
 
550
y       b       c       a       c       b       z
 
551
11      1       10      2       3       1       4
 
552
11      1       3       2       3       1       4
 
553
select * from (t1 join t2 using (b)) join (t3 join t4 using (c)) using (c);
 
554
c       b       a       b       y
 
555
3       1       2       1       11
 
556
3       1       2       1       2
 
557
select * from (t1 join t2 using (b)) natural join (t3 join t4 using (c));
 
558
b       c       a       y
 
559
1       3       2       11
 
560
1       3       2       2
 
561
select a,b,c from (t1 natural join t2) natural join (t3 natural join t4)
 
562
where b + 1 = y or b + 10 = y group by b,c,a having min(b) < max(y) order by a;
 
563
a       b       c
 
564
2       1       3
 
565
select * from (t1 natural join t2) natural left join (t3 natural join t4)
 
566
where b + 1 = y or b + 10 = y group by b,c,a,y having min(b) < max(y) order by a, y;
 
567
b       c       a       y
 
568
1       3       2       2
 
569
1       3       2       11
 
570
select * from (t3 natural join t4) natural right join (t1 natural join t2)
 
571
where b + 1 = y or b + 10 = y group by b,c,a,y having min(b) < max(y) order by a, y;
 
572
b       c       a       y
 
573
1       3       2       2
 
574
1       3       2       11
 
575
select * from t1 natural join t2 where t1.c > t2.a;
 
576
b       c       a
 
577
1       10      2
 
578
1       3       2
 
579
select * from t1 natural join t2 where t1.b > t2.b;
 
580
b       c       a
 
581
select * from t1 natural left join (t4 natural join t5) where t5.z is not NULL;
 
582
c       b       y       z
 
583
3       1       11      4
 
584
3       2       11      4
 
585
select * from t1 join (t2 join t4 on b + 1 = y) on t1.c = t4.c;
 
586
c       b       a       b       y       c
 
587
3       1       2       1       2       3
 
588
3       2       2       1       2       3
 
589
select * from (t2 join t4 on b + 1 = y) join t1 on t1.c = t4.c;
 
590
a       b       y       c       c       b
 
591
2       1       2       3       3       1
 
592
2       1       2       3       3       2
 
593
select * from t1 natural join (t2 join t4 on b + 1 = y);
 
594
c       b       a       y
 
595
3       1       2       2
 
596
select * from (t1 cross join t2) join (t3 cross join t4) on (a < y and t2.b < t3.c);
 
597
c       b       a       b       b       c       y       c
 
598
10      1       2       1       1       3       11      3
 
599
10      1       2       1       1       10      11      3
 
600
3       1       2       1       1       3       11      3
 
601
3       1       2       1       1       10      11      3
 
602
3       2       2       1       1       3       11      3
 
603
3       2       2       1       1       10      11      3
 
604
select * from (t1, t2) join (t3, t4) on (a < y and t2.b < t3.c);
 
605
c       b       a       b       b       c       y       c
 
606
10      1       2       1       1       3       11      3
 
607
10      1       2       1       1       10      11      3
 
608
3       1       2       1       1       3       11      3
 
609
3       1       2       1       1       10      11      3
 
610
3       2       2       1       1       3       11      3
 
611
3       2       2       1       1       10      11      3
 
612
select * from (t1 natural join t2) join (t3 natural join t4) on a = y;
 
613
b       c       a       c       b       y
 
614
1       10      2       3       1       2
 
615
1       3       2       3       1       2
 
616
select * from ((t3 join (t1 join t2 on c > a) on t3.b < t2.a) join t4 on y > t1.c) join t5 on z = t1.b + 3;
 
617
b       c       c       b       a       b       y       c       y       z
 
618
1       3       10      1       2       1       11      3       11      4
 
619
1       10      10      1       2       1       11      3       11      4
 
620
1       3       3       1       2       1       11      3       11      4
 
621
1       10      3       1       2       1       11      3       11      4
 
622
select * from t1 natural join t2 where t1.b > 0;
 
623
b       c       a
 
624
1       10      2
 
625
1       3       2
 
626
select * from t1 natural join (t4 natural join t5) where t4.y > 7;
 
627
c       b       y       z
 
628
3       1       11      4
 
629
3       2       11      4
 
630
select * from (t4 natural join t5) natural join t1 where t4.y > 7;
 
631
c       y       z       b
 
632
3       11      4       1
 
633
3       11      4       2
 
634
select * from t1 natural left join (t4 natural join t5) where t4.y > 7;
 
635
c       b       y       z
 
636
3       1       11      4
 
637
3       2       11      4
 
638
select * from (t4 natural join t5) natural right join t1 where t4.y > 7;
 
639
c       b       y       z
 
640
3       1       11      4
 
641
3       2       11      4
 
642
select * from (t1 natural join t2) join (t3 natural join t4) on t1.b = t3.b;
 
643
b       c       a       c       b       y
 
644
1       10      2       3       1       11
 
645
1       10      2       3       1       2
 
646
1       3       2       3       1       11
 
647
1       3       2       3       1       2
 
648
select t1.*, t2.* from t1 natural join t2;
 
649
c       b       a       b
 
650
10      1       2       1
 
651
3       1       2       1
 
652
select t1.*, t2.*, t3.*, t4.* from (t1 natural join t2) natural join (t3 natural join t4);
 
653
c       b       a       b       b       c       y       c
 
654
3       1       2       1       1       3       11      3
 
655
3       1       2       1       1       3       2       3
 
656
select * from (select * from t1 natural join t2) as t12
 
657
natural join
 
658
(select * from t3 natural join t4) as t34;
 
659
b       c       a       y
 
660
1       3       2       11
 
661
1       3       2       2
 
662
select * from (select * from t1 natural join t2) as t12
 
663
natural left join
 
664
(select * from t3 natural join t4) as t34;
 
665
b       c       a       y
 
666
1       10      2       NULL
 
667
1       3       2       11
 
668
1       3       2       2
 
669
select * from (select * from t3 natural join t4) as t34
 
670
natural right join
 
671
(select * from t1 natural join t2) as t12;
 
672
b       c       a       y
 
673
1       10      2       NULL
 
674
1       3       2       11
 
675
1       3       2       2
 
676
select * from t1 natural join (t3 cross join t4);
 
677
ERROR 23000: Column 'c' in from clause is ambiguous
 
678
select * from (t3 cross join t4) natural join t1;
 
679
ERROR 23000: Column 'c' in from clause is ambiguous
 
680
select * from t1 join (t2, t3) using (b);
 
681
ERROR 23000: Column 'b' in from clause is ambiguous
 
682
select * from ((t1 natural join t2), (t3 natural join t4)) natural join t6;
 
683
ERROR 23000: Column 'c' in from clause is ambiguous
 
684
select * from ((t1 natural join t2), (t3 natural join t4)) natural join t6;
 
685
ERROR 23000: Column 'c' in from clause is ambiguous
 
686
select * from t6 natural join ((t1 natural join t2),  (t3 natural join t4));
 
687
ERROR 23000: Column 'c' in from clause is ambiguous
 
688
select * from (t1 join t2 on t1.b=t2.b) natural join (t3 natural join t4);
 
689
ERROR 23000: Column 'b' in from clause is ambiguous
 
690
select * from  (t3 natural join t4) natural join (t1 join t2 on t1.b=t2.b);
 
691
ERROR 23000: Column 'b' in from clause is ambiguous
 
692
select * from (t3 join (t4 natural join t5) on (b < z))
 
693
natural join
 
694
(t1 natural join t2);
 
695
ERROR 23000: Column 'c' in from clause is ambiguous
 
696
select * from (t1 natural join t2) natural join (t3 join (t4 natural join t5) on (b < z));
 
697
ERROR 23000: Column 'c' in from clause is ambiguous
 
698
select * from information_schema.statistics join information_schema.columns
 
699
using(table_name,column_name) where table_name='user';
 
700
TABLE_NAME      user
 
701
COLUMN_NAME     Host
 
702
TABLE_CATALOG   NULL
 
703
TABLE_SCHEMA    mysql
 
704
NON_UNIQUE      0
 
705
INDEX_SCHEMA    mysql
 
706
INDEX_NAME      PRIMARY
 
707
SEQ_IN_INDEX    1
 
708
COLLATION       A
 
709
CARDINALITY     NULL
 
710
SUB_PART        NULL
 
711
PACKED  NULL
 
712
NULLABLE        
 
713
INDEX_TYPE      BTREE
 
714
COMMENT 
 
715
INDEX_COMMENT   
 
716
TABLE_CATALOG   NULL
 
717
TABLE_SCHEMA    mysql
 
718
ORDINAL_POSITION        1
 
719
COLUMN_DEFAULT  NULL
 
720
IS_NULLABLE     NO
 
721
DATA_TYPE       char
 
722
CHARACTER_MAXIMUM_LENGTH        60
 
723
CHARACTER_OCTET_LENGTH  240
 
724
NUMERIC_PRECISION       NULL
 
725
NUMERIC_SCALE   NULL
 
726
CHARACTER_SET_NAME      utf8
 
727
COLLATION_NAME  utf8_bin
 
728
COLUMN_TYPE     char(60)
 
729
COLUMN_KEY      PRI
 
730
EXTRA   
 
731
PRIVILEGES      #
 
732
COLUMN_COMMENT  
 
733
STORAGE Default
 
734
FORMAT  Default
 
735
TABLE_NAME      user
 
736
COLUMN_NAME     User
 
737
TABLE_CATALOG   NULL
 
738
TABLE_SCHEMA    mysql
 
739
NON_UNIQUE      0
 
740
INDEX_SCHEMA    mysql
 
741
INDEX_NAME      PRIMARY
 
742
SEQ_IN_INDEX    2
 
743
COLLATION       A
 
744
CARDINALITY     3
 
745
SUB_PART        NULL
 
746
PACKED  NULL
 
747
NULLABLE        
 
748
INDEX_TYPE      BTREE
 
749
COMMENT 
 
750
INDEX_COMMENT   
 
751
TABLE_CATALOG   NULL
 
752
TABLE_SCHEMA    mysql
 
753
ORDINAL_POSITION        2
 
754
COLUMN_DEFAULT  NULL
 
755
IS_NULLABLE     NO
 
756
DATA_TYPE       char
 
757
CHARACTER_MAXIMUM_LENGTH        16
 
758
CHARACTER_OCTET_LENGTH  64
 
759
NUMERIC_PRECISION       NULL
 
760
NUMERIC_SCALE   NULL
 
761
CHARACTER_SET_NAME      utf8
 
762
COLLATION_NAME  utf8_bin
 
763
COLUMN_TYPE     char(16)
 
764
COLUMN_KEY      PRI
 
765
EXTRA   
 
766
PRIVILEGES      #
 
767
COLUMN_COMMENT  
 
768
STORAGE Default
 
769
FORMAT  Default
 
770
drop table t1;
 
771
drop table t2;
 
772
drop table t3;
 
773
drop table t4;
 
774
drop table t5;
 
775
drop table t6;
 
776
create table t1 (a1 int, a2 int);
 
777
create table t2 (a1 int, b int);
 
778
create table t3 (c1 int, c2 int);
 
779
create table t4 (c2 int);
 
780
insert into t1 values (1,1);
 
781
insert into t2 values (1,1);
 
782
insert into t3 values (1,1);
 
783
insert into t4 values (1);
 
784
select * from t1 join t2 using (a1) join t3 on b=c1 join t4 using (c2);
 
785
c2      a1      a2      b       c1
 
786
1       1       1       1       1
 
787
select * from t3 join (t1 join t2 using (a1)) on b=c1 join t4 using (c2);
 
788
c2      c1      a1      a2      b
 
789
1       1       1       1       1
 
790
select a2 from t1 join t2 using (a1) join t3 on b=c1 join t4 using (c2);
 
791
a2
 
792
1
 
793
select a2 from t3 join (t1 join t2 using (a1)) on b=c1 join t4 using (c2);
 
794
a2
 
795
1
 
796
select a2 from ((t1 join t2 using (a1)) join t3 on b=c1) join t4 using (c2);
 
797
a2
 
798
1
 
799
select a2 from ((t1 natural join t2) join t3 on b=c1) natural join t4;
 
800
a2
 
801
1
 
802
drop table t1,t2,t3,t4;
 
803
create table t1 (c int, b int);
 
804
create table t2 (a int, b int);
 
805
create table t3 (b int, c int);
 
806
create table t4 (y int, c int);
 
807
create table t5 (y int, z int);
 
808
insert into t1 values (3,2);
 
809
insert into t2 values (1,2);
 
810
insert into t3 values (2,3);
 
811
insert into t4 values (1,3);
 
812
insert into t5 values (1,4);
 
813
select * from ((t3 natural join (t1 natural join t2)) natural join t4)
 
814
natural join t5;
 
815
y       c       b       a       z
 
816
1       3       2       1       4
 
817
drop table t1, t2, t3, t4, t5;
 
818
create table t1 (a int, b int);
 
819
insert into t1 values 
 
820
(NULL, 1),
 
821
(NULL, 2),
 
822
(NULL, 3),
 
823
(NULL, 4);
 
824
create table t2 (a int not null, primary key(a));
 
825
insert into t2 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
826
create table t3 (a int not null, primary key(a));
 
827
insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
828
flush status;
 
829
select * from t1, t2, t3 where t3.a=t1.a and t2.a=t1.b;
 
830
a       b       a       a
 
831
explain select * from t1, t2, t3 where t3.a=t1.a and t2.a=t1.b;
 
832
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
833
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    4       
 
834
1       SIMPLE  t2      eq_ref  PRIMARY PRIMARY 4       test.t1.b       1       Using index
 
835
1       SIMPLE  t3      eq_ref  PRIMARY PRIMARY 4       test.t1.a       1       Using index
 
836
We expect rnd_next=5, and read_key must be 0 because of short-cutting:
 
837
show status like 'Handler_read%';
 
838
Variable_name   Value
 
839
Handler_read_first      0
 
840
Handler_read_key        0
 
841
Handler_read_next       0
 
842
Handler_read_prev       0
 
843
Handler_read_rnd        0
 
844
Handler_read_rnd_next   5
 
845
drop table t1, t2, t3;
 
846
create table t1 (a int);
 
847
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
848
create table t2 (a int, b int, filler char(100), key(a), key(b));
 
849
create table t3 (a int, b int, filler char(100), key(a), key(b));
 
850
insert into t2 
 
851
select @a:= A.a + 10*(B.a + 10*C.a), @a, 'filler' from t1 A, t1 B, t1 C;
 
852
insert into t3 select * from t2 where a < 800;
 
853
explain select * from t2,t3 where t2.a < 200 and t2.b=t3.b;
 
854
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
855
1       SIMPLE  t2      ALL     a,b     NULL    NULL    NULL    1000    Using where
 
856
1       SIMPLE  t3      ref     b       b       5       test.t2.b       1       
 
857
drop table t1, t2, t3;
 
858
create table t1 (a int);
 
859
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
860
create table t2 (a int, b int, primary key(a));
 
861
insert into t2 select @v:=A.a+10*B.a, @v  from t1 A, t1 B;
 
862
explain select * from t1;
 
863
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
864
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    10      
 
865
show status like '%cost%';
 
866
Variable_name   Value
 
867
Last_query_cost 4.016090
 
868
select 'The cost of accessing t1 (dont care if it changes' '^';
 
869
The cost of accessing t1 (dont care if it changes
 
870
The cost of accessing t1 (dont care if it changes^
 
871
select 'vv: Following query must use ALL(t1), eq_ref(A), eq_ref(B): vv' Z;
 
872
Z
 
873
vv: Following query must use ALL(t1), eq_ref(A), eq_ref(B): vv
 
874
explain select * from t1, t2 A, t2 B where A.a = t1.a and B.a=A.b;
 
875
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
876
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    10      
 
877
1       SIMPLE  A       eq_ref  PRIMARY PRIMARY 4       test.t1.a       1       
 
878
1       SIMPLE  B       eq_ref  PRIMARY PRIMARY 4       test.A.b        1       
 
879
show status like '%cost%';
 
880
Variable_name   Value
 
881
Last_query_cost 24.016090
 
882
select '^^: The above should be ~= 20 + cost(select * from t1). Value less than 20 is an error' Z;
 
883
Z
 
884
^^: The above should be ~= 20 + cost(select * from t1). Value less than 20 is an error
 
885
drop table t1, t2;
 
886
CREATE TABLE t1 (a INT PRIMARY KEY, b INT);
 
887
CREATE TABLE t2 (c INT PRIMARY KEY, d INT);
 
888
INSERT INTO t1 VALUES(1,NULL),(2,NULL),(3,NULL),(4,NULL);
 
889
INSERT INTO t1 SELECT a + 4, b FROM t1;
 
890
INSERT INTO t1 SELECT a + 8, b FROM t1;
 
891
INSERT INTO t1 SELECT a + 16, b FROM t1;
 
892
INSERT INTO t1 SELECT a + 32, b FROM t1;
 
893
INSERT INTO t1 SELECT a + 64, b FROM t1;
 
894
INSERT INTO t2 SELECT a, b FROM t1;
 
895
EXPLAIN SELECT * FROM t1 JOIN t2 ON b=c ORDER BY a LIMIT 2;
 
896
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
897
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    2       
 
898
1       SIMPLE  t2      eq_ref  PRIMARY PRIMARY 4       test.t1.b       1       
 
899
EXPLAIN SELECT * FROM t1 JOIN t2 ON a=c ORDER BY a LIMIT 2;
 
900
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
901
1       SIMPLE  t1      index   PRIMARY PRIMARY 4       NULL    2       
 
902
1       SIMPLE  t2      eq_ref  PRIMARY PRIMARY 4       test.t1.a       1       
 
903
SELECT * FROM t1 JOIN t2 ON b=c ORDER BY a LIMIT 2;
 
904
a       b       c       d
 
905
SELECT * FROM t1 JOIN t2 ON a=c ORDER BY a LIMIT 2;
 
906
a       b       c       d
 
907
1       NULL    1       NULL
 
908
2       NULL    2       NULL
 
909
EXPLAIN SELECT * FROM t1 JOIN t2 ON b=c ORDER BY a;
 
910
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
911
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    128     Using filesort
 
912
1       SIMPLE  t2      eq_ref  PRIMARY PRIMARY 4       test.t1.b       1       
 
913
EXPLAIN SELECT * FROM t1 JOIN t2 ON a=c ORDER BY a;
 
914
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
915
1       SIMPLE  t1      ALL     PRIMARY NULL    NULL    NULL    128     Using filesort
 
916
1       SIMPLE  t2      eq_ref  PRIMARY PRIMARY 4       test.t1.a       1       
 
917
SELECT * FROM t1 JOIN t2 ON b=c ORDER BY a;
 
918
a       b       c       d
 
919
SELECT * FROM t1 JOIN t2 ON a=c ORDER BY a;
 
920
a       b       c       d
 
921
1       NULL    1       NULL
 
922
2       NULL    2       NULL
 
923
3       NULL    3       NULL
 
924
4       NULL    4       NULL
 
925
5       NULL    5       NULL
 
926
6       NULL    6       NULL
 
927
7       NULL    7       NULL
 
928
8       NULL    8       NULL
 
929
9       NULL    9       NULL
 
930
10      NULL    10      NULL
 
931
11      NULL    11      NULL
 
932
12      NULL    12      NULL
 
933
13      NULL    13      NULL
 
934
14      NULL    14      NULL
 
935
15      NULL    15      NULL
 
936
16      NULL    16      NULL
 
937
17      NULL    17      NULL
 
938
18      NULL    18      NULL
 
939
19      NULL    19      NULL
 
940
20      NULL    20      NULL
 
941
21      NULL    21      NULL
 
942
22      NULL    22      NULL
 
943
23      NULL    23      NULL
 
944
24      NULL    24      NULL
 
945
25      NULL    25      NULL
 
946
26      NULL    26      NULL
 
947
27      NULL    27      NULL
 
948
28      NULL    28      NULL
 
949
29      NULL    29      NULL
 
950
30      NULL    30      NULL
 
951
31      NULL    31      NULL
 
952
32      NULL    32      NULL
 
953
33      NULL    33      NULL
 
954
34      NULL    34      NULL
 
955
35      NULL    35      NULL
 
956
36      NULL    36      NULL
 
957
37      NULL    37      NULL
 
958
38      NULL    38      NULL
 
959
39      NULL    39      NULL
 
960
40      NULL    40      NULL
 
961
41      NULL    41      NULL
 
962
42      NULL    42      NULL
 
963
43      NULL    43      NULL
 
964
44      NULL    44      NULL
 
965
45      NULL    45      NULL
 
966
46      NULL    46      NULL
 
967
47      NULL    47      NULL
 
968
48      NULL    48      NULL
 
969
49      NULL    49      NULL
 
970
50      NULL    50      NULL
 
971
51      NULL    51      NULL
 
972
52      NULL    52      NULL
 
973
53      NULL    53      NULL
 
974
54      NULL    54      NULL
 
975
55      NULL    55      NULL
 
976
56      NULL    56      NULL
 
977
57      NULL    57      NULL
 
978
58      NULL    58      NULL
 
979
59      NULL    59      NULL
 
980
60      NULL    60      NULL
 
981
61      NULL    61      NULL
 
982
62      NULL    62      NULL
 
983
63      NULL    63      NULL
 
984
64      NULL    64      NULL
 
985
65      NULL    65      NULL
 
986
66      NULL    66      NULL
 
987
67      NULL    67      NULL
 
988
68      NULL    68      NULL
 
989
69      NULL    69      NULL
 
990
70      NULL    70      NULL
 
991
71      NULL    71      NULL
 
992
72      NULL    72      NULL
 
993
73      NULL    73      NULL
 
994
74      NULL    74      NULL
 
995
75      NULL    75      NULL
 
996
76      NULL    76      NULL
 
997
77      NULL    77      NULL
 
998
78      NULL    78      NULL
 
999
79      NULL    79      NULL
 
1000
80      NULL    80      NULL
 
1001
81      NULL    81      NULL
 
1002
82      NULL    82      NULL
 
1003
83      NULL    83      NULL
 
1004
84      NULL    84      NULL
 
1005
85      NULL    85      NULL
 
1006
86      NULL    86      NULL
 
1007
87      NULL    87      NULL
 
1008
88      NULL    88      NULL
 
1009
89      NULL    89      NULL
 
1010
90      NULL    90      NULL
 
1011
91      NULL    91      NULL
 
1012
92      NULL    92      NULL
 
1013
93      NULL    93      NULL
 
1014
94      NULL    94      NULL
 
1015
95      NULL    95      NULL
 
1016
96      NULL    96      NULL
 
1017
97      NULL    97      NULL
 
1018
98      NULL    98      NULL
 
1019
99      NULL    99      NULL
 
1020
100     NULL    100     NULL
 
1021
101     NULL    101     NULL
 
1022
102     NULL    102     NULL
 
1023
103     NULL    103     NULL
 
1024
104     NULL    104     NULL
 
1025
105     NULL    105     NULL
 
1026
106     NULL    106     NULL
 
1027
107     NULL    107     NULL
 
1028
108     NULL    108     NULL
 
1029
109     NULL    109     NULL
 
1030
110     NULL    110     NULL
 
1031
111     NULL    111     NULL
 
1032
112     NULL    112     NULL
 
1033
113     NULL    113     NULL
 
1034
114     NULL    114     NULL
 
1035
115     NULL    115     NULL
 
1036
116     NULL    116     NULL
 
1037
117     NULL    117     NULL
 
1038
118     NULL    118     NULL
 
1039
119     NULL    119     NULL
 
1040
120     NULL    120     NULL
 
1041
121     NULL    121     NULL
 
1042
122     NULL    122     NULL
 
1043
123     NULL    123     NULL
 
1044
124     NULL    124     NULL
 
1045
125     NULL    125     NULL
 
1046
126     NULL    126     NULL
 
1047
127     NULL    127     NULL
 
1048
128     NULL    128     NULL
 
1049
DROP TABLE IF EXISTS t1,t2;
 
1050
End of 5.0 tests.