Commit 2ef2e063 authored by unknown's avatar unknown

Merge mysql.com:/home/timka/mysql/src/5.0-virgin

into  mysql.com:/home/timka/mysql/src/5.0-2486


mysql-test/r/select.result:
  Auto merged
mysql-test/t/select.test:
  Auto merged
sql/sql_base.cc:
  Auto merged
sql/table.cc:
  Auto merged
sql/table.h:
  Auto merged
parents 04d7dad1 fe24add7
......@@ -349,14 +349,14 @@ drop table if exists t2 ;
--enable_warnings
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
let $1= 9 ;
while ($1)
{
......
......@@ -732,6 +732,11 @@ drop table t1;
set @a:=now();
CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=bdb;
insert into t1 (a) values(1),(2),(3);
select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a;
a
1
2
3
select a from t1 natural join t1 as t2 where b >= @a order by a;
a
1
......@@ -906,7 +911,7 @@ create temporary table tmp1
select branch_id, target_id, platform_id, product_id
from t1, t2, t3, t4 ;
create temporary table tmp2
select branch_id, target_id, platform_id, product_id
select tmp1.branch_id, tmp1.target_id, tmp1.platform_id, tmp1.product_id
from tmp1 left join t8
using (branch_id,target_id,platform_id,product_id)
where t8.archive_id is null ;
......
......@@ -750,6 +750,11 @@ drop table t1;
set @a:=now();
CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=innodb;
insert into t1 (a) values(1),(2),(3);
select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a;
a
1
2
3
select a from t1 natural join t1 as t2 where b >= @a order by a;
a
1
......
......@@ -121,6 +121,12 @@ id catid stateid countyid
drop table t1, t2;
create table t1 (a int primary key);
insert into t1 values(1),(2);
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);
a
1
2
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);
ERROR HY000: Too many tables; MySQL can only use XX tables in a join
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);
a
1
......@@ -145,6 +151,10 @@ DROP TABLE t1, t2;
CREATE TABLE t1 (d DATE NOT NULL);
CREATE TABLE t2 (d DATE NOT NULL);
INSERT INTO t1 (d) VALUES ('2001-08-01'),('0000-00-00');
SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE t2.d IS NULL;
d
2001-08-01
0000-00-00
SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE d IS NULL;
d
0000-00-00
......@@ -264,6 +274,12 @@ PRIMARY KEY (siteid,rate_code),
FULLTEXT KEY rate_code (rate_code)
) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('rivercats','cust',20);
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';
rate_code base_rate
cust 20
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';
rate_code base_rate
cust 20
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';
rate_code base_rate
cust 20
......@@ -326,7 +342,7 @@ select * from t3 right join t2 on (t3.i=t2.i) right join t1 on (t2.i=t1.i);
i i i
NULL NULL 1
2 2 2
select * from t1,t2 natural left join t3 order by 1,2;
select * from t1,t2 natural left join t3 order by t1.i,t2.i,t3.i;
i i
1 2
1 3
......@@ -338,36 +354,36 @@ i i i
1 3 NULL
2 2 2
2 3 NULL
select * from t2 natural left join t3,t1 order by t1.i;
i i
2 1
3 1
2 2
3 2
select t1.i,t2.i,t3.i from t2 natural left join t3,t1 order by t1.i,t2.i,t3.i;
i i i
1 2 2
1 3 NULL
2 2 2
2 3 NULL
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;
i i i
1 2 2
1 3 NULL
2 2 2
2 3 NULL
select * from t1,t2 natural right join t3 order by 1,2;
select * from t1,t2 natural right join t3 order by t1.i,t2.i,t3.i;
i i
1 2
1 4
2 2
1 2
2 4
2 2
select * from t1,t2 right join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i;
i i i
1 NULL 4
1 2 2
2 NULL 4
2 2 2
select * from t2 natural right join t3,t1 order by t1.i;
i i
2 1
4 1
2 2
4 2
select t1.i,t2.i,t3.i from t2 natural right join t3,t1 order by t1.i,t2.i,t3.i;
i i i
1 NULL 4
1 2 2
2 NULL 4
2 2 2
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;
i i i
1 NULL 4
......
......@@ -1153,13 +1153,13 @@ a b a1 b
4 2 1 2
4 2 2 2
5 3 NULL NULL
SELECT *
SELECT t2.a,t2.b,t3.a1,t3.b
FROM t2 NATURAL LEFT JOIN t3
WHERE t2.a = 4 OR (t2.a > 4 AND t3.a1 IS NULL);
b a c a1 c1
2 4 0 1 0
2 4 0 2 0
3 5 0 NULL NULL
a b a1 b
4 2 1 2
4 2 2 2
5 3 NULL NULL
DROP TABLE t0,t1,t2,t3,t4,t5,t6,t7,t8,t9;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (a int);
......
......@@ -36,16 +36,7 @@ grp a c id a c d
3 5 C 3 5 B 5
3 6 D 3 6 C 6
NULL NULL NULL 4 7 D 7
select * from t1 left join t2 using (a);
a grp c id c d
1 1 a 1 a 1
2 2 b NULL NULL NULL
3 2 c NULL NULL NULL
4 3 E 3 A 4
5 3 C 3 B 5
6 3 D 3 C 6
NULL NULL NULL NULL NULL
select t1.*,t2.* from t1 left join t2 on t1.a=t2.a;
select t1.*,t2.* from t1 left join t2 using (a);
grp a c id a c d
1 1 a 1 1 a 1
2 2 b NULL NULL NULL NULL
......@@ -54,34 +45,40 @@ grp a c id a c d
3 5 C 3 5 B 5
3 6 D 3 6 C 6
NULL NULL NULL NULL NULL NULL
select * from t1 left join t2 using (a,c);
a c grp id d
1 a 1 1 1
2 b 2 NULL NULL
3 c 2 NULL NULL
4 E 3 NULL NULL
5 C 3 NULL NULL
6 D 3 NULL NULL
NULL NULL NULL NULL
select * from t1 left join t2 using (c);
c grp a id a d
a 1 1 1 1 1
a 1 1 3 4 4
b 2 2 3 5 5
c 2 3 3 6 6
E 3 4 NULL NULL NULL
C 3 5 3 6 6
D 3 6 4 7 7
NULL NULL NULL NULL NULL
select * from t1 natural left outer join t2;
a c grp id d
1 a 1 1 1
2 b 2 NULL NULL
3 c 2 NULL NULL
4 E 3 NULL NULL
5 C 3 NULL NULL
6 D 3 NULL NULL
NULL NULL NULL NULL
select t1.*,t2.* from t1 left join t2 using (a) where t1.a=t2.a;
grp a c id a c d
1 1 a 1 1 a 1
3 4 E 3 4 A 4
3 5 C 3 5 B 5
3 6 D 3 6 C 6
select t1.*,t2.* from t1 left join t2 using (a,c);
grp a c id a c d
1 1 a 1 1 a 1
2 2 b NULL NULL NULL NULL
2 3 c NULL NULL NULL NULL
3 4 E NULL NULL NULL NULL
3 5 C NULL NULL NULL NULL
3 6 D NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL
select t1.*,t2.* from t1 left join t2 using (c);
grp a c id a c d
1 1 a 1 1 a 1
1 1 a 3 4 A 4
2 2 b 3 5 B 5
2 3 c 3 6 C 6
3 4 E NULL NULL NULL NULL
3 5 C 3 6 C 6
3 6 D 4 7 D 7
NULL NULL NULL NULL NULL NULL
select t1.*,t2.* from t1 natural left outer join t2;
grp a c id a c d
1 1 a 1 1 a 1
2 2 b NULL NULL NULL NULL
2 3 c NULL NULL NULL NULL
3 4 E NULL NULL NULL NULL
3 5 C NULL NULL NULL NULL
3 6 D NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL
select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id=3;
grp a c id a c d
3 4 E 3 4 A 4
......@@ -114,21 +111,21 @@ select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on
ERROR 42S22: Unknown column 't3.a' in 'on clause'
select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t2.a=t3.a);
ERROR 42S22: Unknown column 't3.a' in 'on clause'
select * from t1 inner join t2 using (a);
a grp c id c d
1 1 a 1 a 1
4 3 E 3 A 4
5 3 C 3 B 5
6 3 D 3 C 6
select t1.*,t2.* from t1 inner join t2 using (a);
grp a c id a c d
1 1 a 1 1 a 1
3 4 E 3 4 A 4
3 5 C 3 5 B 5
3 6 D 3 6 C 6
select t1.*,t2.* from t1 inner join t2 on (t1.a=t2.a);
grp a c id a c d
1 1 a 1 1 a 1
3 4 E 3 4 A 4
3 5 C 3 5 B 5
3 6 D 3 6 C 6
select * from t1 natural join t2;
a c grp id d
1 a 1 1 1
select t1.*,t2.* from t1 natural join t2;
grp a c id a c d
1 1 a 1 1 a 1
drop table t1,t2;
CREATE TABLE t1 (
usr_id INT unsigned NOT NULL,
......@@ -435,7 +432,7 @@ INSERT INTO t2 VALUES (3,'z');
SELECT t2.id2 FROM t2 LEFT OUTER JOIN t1 ON t1.id2 = t2.id2 WHERE id1 IS NULL;
id2
3
SELECT id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL;
SELECT t2.id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL;
id2
3
drop table t1,t2;
......@@ -653,6 +650,13 @@ select * from t1 natural left join t2 natural left join t3;
i
1
2
select * from t1 natural left join t2 where (t2.i is not null)=0;
i
1
select * from t1 natural left join t2 where (t2.i is not null) is not null;
i
1
2
select * from t1 natural left join t2 where (i is not null)=0;
i
select * from t1 natural left join t2 where (i is not null) is not null;
......@@ -931,6 +935,18 @@ create table t1 (a int, b varchar(20));
create table t2 (a int, c varchar(20));
insert into t1 values (1,"aaaaaaaaaa"),(2,"bbbbbbbbbb");
insert into t2 values (1,"cccccccccc"),(2,"dddddddddd");
select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by t1.a;
group_concat(t1.b,t2.c)
aaaaa
bbbbb
Warnings:
Warning 1260 2 line(s) were cut by GROUP_CONCAT()
select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by t1.a;
group_concat(t1.b,t2.c)
aaaaa
bbbbb
Warnings:
Warning 1260 2 line(s) were cut by GROUP_CONCAT()
select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by a;
group_concat(t1.b,t2.c)
aaaaa
......
......@@ -313,16 +313,12 @@ INSERT INTO t1 (order_id, product_id, product_type) VALUES
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
INSERT INTO t2 (order_id, product_id, product_type) VALUES
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
select t1.* from t1 left join t2
on (t1.order_id = t2.order_id and
t1.product_id = t2.product_id and
t1.product_type = t2.product_type)
where t2.order_id = NULL;
select t1.* from t1
left join t2 using(order_id, product_id, product_type)
where t2.order_id=NULL;
order_id product_id product_type
select t1.* from t1 left join t2
on (t1.order_id = t2.order_id and
t1.product_id = t2.product_id and
t1.product_type = t2.product_type)
select t1.* from t1
left join t2 using(order_id, product_id, product_type)
where t2.order_id is NULL;
order_id product_id product_type
3d7ce39b5d4b3e3d22aaafe9b633de51 1206029 3
......
......@@ -180,6 +180,15 @@ INSERT INTO t3 VALUES (1,'123 Park Place');
INSERT INTO t3 VALUES (2,'453 Boardwalk');
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 USING(c)
LEFT JOIN t3 ON t3.c = t1.c;
a b if(b = 1,i,if(b = 2,v,''))
1 1 50
2 1 25
3 2 123 Park Place
4 2 453 Boardwalk
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 ON t1.c = t2.c
LEFT JOIN t3 ON t3.c = t1.c;
a b if(b = 1,i,if(b = 2,v,''))
......@@ -189,6 +198,16 @@ a b if(b = 1,i,if(b = 2,v,''))
4 2 453 Boardwalk
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 USING(c)
LEFT JOIN t3 ON t3.c = t1.c
ORDER BY a;
a b if(b = 1,i,if(b = 2,v,''))
1 1 50
2 1 25
3 2 123 Park Place
4 2 453 Boardwalk
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 ON t1.c = t2.c
LEFT JOIN t3 ON t3.c = t1.c
ORDER BY a;
......@@ -523,9 +542,15 @@ KEY `pseudo` (`pseudo`)
);
INSERT INTO t1 (titre,auteur,dest) VALUES ('test','joce','bug');
INSERT INTO t2 (numeropost,pseudo) VALUES (1,'joce'),(1,'bug');
SELECT titre,t1.numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
SELECT titre,numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
SELECT titre,t1.numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
SELECT titre,numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
......
......@@ -519,16 +519,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -549,7 +549,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -591,7 +591,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -612,7 +612,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -654,7 +654,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -675,7 +675,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......
......@@ -519,16 +519,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -549,7 +549,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -591,7 +591,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -612,7 +612,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -654,7 +654,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -675,7 +675,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......
......@@ -520,16 +520,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -550,7 +550,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -592,7 +592,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -613,7 +613,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -655,7 +655,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -676,7 +676,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......
......@@ -562,16 +562,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -592,7 +592,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -634,7 +634,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -655,7 +655,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -697,7 +697,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -718,7 +718,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......@@ -3574,16 +3574,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -3604,7 +3604,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -3646,7 +3646,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -3667,7 +3667,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -3709,7 +3709,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -3730,7 +3730,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......
......@@ -519,16 +519,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -549,7 +549,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -591,7 +591,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -612,7 +612,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -654,7 +654,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -675,7 +675,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......
......@@ -519,16 +519,16 @@ a ? a
drop table if exists t2 ;
create table t2 as select * from t1 ;
set @query1= 'SELECT * FROM t2 join t1 on (t1.a=t2.a) order by t2.a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by a ' ;
set @query2= 'SELECT * FROM t2 natural join t1 order by t2.a ' ;
set @query3= 'SELECT * FROM t2 join t1 using(a) order by t2.a ' ;
set @query4= 'SELECT * FROM t2 left join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by a ' ;
set @query5= 'SELECT * FROM t2 natural left join t1 order by t2.a ' ;
set @query6= 'SELECT * FROM t2 left join t1 using(a) order by t2.a ' ;
set @query7= 'SELECT * FROM t2 right join t1 on(t1.a=t2.a) order by t2.a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by a ' ;
set @query8= 'SELECT * FROM t2 natural right join t1 order by t2.a ' ;
set @query9= 'SELECT * FROM t2 right join t1 using(a) order by t2.a ' ;
the join statement is:
SELECT * FROM t2 right join t1 using(a) order by a
SELECT * FROM t2 right join t1 using(a) order by t2.a
prepare stmt1 from @query9 ;
execute stmt1 ;
a b b
......@@ -549,7 +549,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural right join t1 order by a
SELECT * FROM t2 natural right join t1 order by t2.a
prepare stmt1 from @query8 ;
execute stmt1 ;
a b
......@@ -591,7 +591,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 left join t1 using(a) order by a
SELECT * FROM t2 left join t1 using(a) order by t2.a
prepare stmt1 from @query6 ;
execute stmt1 ;
a b b
......@@ -612,7 +612,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural left join t1 order by a
SELECT * FROM t2 natural left join t1 order by t2.a
prepare stmt1 from @query5 ;
execute stmt1 ;
a b
......@@ -654,7 +654,7 @@ a b a b
3 three 3 three
4 four 4 four
the join statement is:
SELECT * FROM t2 join t1 using(a) order by a
SELECT * FROM t2 join t1 using(a) order by t2.a
prepare stmt1 from @query3 ;
execute stmt1 ;
a b b
......@@ -675,7 +675,7 @@ a b b
3 three three
4 four four
the join statement is:
SELECT * FROM t2 natural join t1 order by a
SELECT * FROM t2 natural join t1 order by t2.a
prepare stmt1 from @query2 ;
execute stmt1 ;
a b
......
......@@ -1350,6 +1350,20 @@ fld1 fld1
250504 250505
250505 250505
insert into t2 (fld1, companynr) values (999999,99);
select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
companynr companyname
99 NULL
select count(*) from t2 left join t4 using (companynr) where t4.companynr is not null;
count(*)
1199
explain select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 1200
1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1 Using where; Not exists
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr is null;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL NULL NULL NULL NULL 12
1 SIMPLE t2 ALL NULL NULL NULL NULL 1200 Using where; Not exists
select companynr,companyname from t2 left join t4 using (companynr) where companynr is null;
companynr companyname
select count(*) from t2 left join t4 using (companynr) where companynr is not null;
......@@ -1362,6 +1376,18 @@ explain select companynr,companyname from t4 left join t2 using (companynr) wher
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
delete from t2 where fld1=999999;
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where
1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where
1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 and t4.companynr > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where
1 SIMPLE t4 eq_ref PRIMARY PRIMARY 1 test.t2.companynr 1
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where
......@@ -1374,6 +1400,18 @@ explain select companynr,companyname from t4 left join t2 using (companynr) wher
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr is null;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL NULL NULL NULL NULL 12
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0 or t4.companynr > 0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where ifnull(t2.companynr,1)>0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL NULL NULL NULL NULL 12
1 SIMPLE t2 ALL NULL NULL NULL NULL 1199 Using where
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr is null;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t4 ALL PRIMARY NULL NULL NULL 12 Using where
......
......@@ -1789,10 +1789,18 @@ CREATE TABLE t1 (id INT);
CREATE TABLE t2 (id INT);
INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (1);
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
id c
1 1
2 0
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
id c
1 1
2 0
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;
id c
1 1
2 0
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;
id c
1 1
......
......@@ -307,17 +307,33 @@ create table t1 ( id integer unsigned not null primary key );
create table t2 ( id integer unsigned not null primary key );
insert into t1 values (1), (2);
insert into t2 values (1);
select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id );
id_A id_B
1 1
2 NULL
select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id);
id_A id_B
1 1
2 NULL
create table t3 (id_A integer unsigned not null, id_B integer unsigned null );
insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id );
select * from t3;
id_A id_B
1 1
2 NULL
delete from t3;
insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id);
select * from t3;
id_A id_B
1 1
2 NULL
drop table t3;
create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id );
select * from t3;
id_A id_B
1 1
2 NULL
drop table t3;
create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id);
select * from t3;
id_A id_B
......
......@@ -400,6 +400,7 @@ drop table t1;
set @a:=now();
CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=bdb;
insert into t1 (a) values(1),(2),(3);
select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a;
select a from t1 natural join t1 as t2 where b >= @a order by a;
update t1 set a=5 where a=1;
select a from t1;
......@@ -571,7 +572,7 @@ create temporary table tmp1
select branch_id, target_id, platform_id, product_id
from t1, t2, t3, t4 ;
create temporary table tmp2
select branch_id, target_id, platform_id, product_id
select tmp1.branch_id, tmp1.target_id, tmp1.platform_id, tmp1.product_id
from tmp1 left join t8
using (branch_id,target_id,platform_id,product_id)
where t8.archive_id is null ;
......
......@@ -440,6 +440,7 @@ drop table t1;
set @a:=now();
CREATE TABLE t1 (a int not null, b timestamp not null, primary key (a)) engine=innodb;
insert into t1 (a) values(1),(2),(3);
select t1.a from t1 natural join t1 as t2 where t1.b >= @a order by t1.a;
select a from t1 natural join t1 as t2 where b >= @a order by a;
update t1 set a=5 where a=1;
select a from t1;
......
......@@ -111,6 +111,10 @@ drop table t1, t2;
create table t1 (a int primary key);
insert into t1 values(1),(2);
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);
--replace_result "31 tables" "XX tables" "61 tables" "XX tables"
--error 1116
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);
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);
--replace_result "31 tables" "XX tables" "61 tables" "XX tables"
--error 1116
......@@ -144,6 +148,7 @@ DROP TABLE t1, t2;
CREATE TABLE t1 (d DATE NOT NULL);
CREATE TABLE t2 (d DATE NOT NULL);
INSERT INTO t1 (d) VALUES ('2001-08-01'),('0000-00-00');
SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE t2.d IS NULL;
SELECT * FROM t1 LEFT JOIN t2 USING (d) WHERE d IS NULL;
SELECT * from t1 WHERE t1.d IS NULL;
SELECT * FROM t1 WHERE 1/0 IS NULL;
......@@ -266,6 +271,8 @@ CREATE TABLE t2 (
FULLTEXT KEY rate_code (rate_code)
) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('rivercats','cust',20);
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';
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';
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';
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';
drop table t1,t2;
......@@ -315,14 +322,14 @@ select * from t3 right join t2 on (t3.i=t2.i);
select * from t3 natural right join t2 natural right join t1;
select * from t3 right join t2 on (t3.i=t2.i) right join t1 on (t2.i=t1.i);
select * from t1,t2 natural left join t3 order by 1,2;
select * from t1,t2 natural left join t3 order by t1.i,t2.i,t3.i;
select * from t1,t2 left join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i;
select * from t2 natural left join t3,t1 order by t1.i;
select t1.i,t2.i,t3.i from t2 natural left join t3,t1 order by t1.i,t2.i,t3.i;
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;
select * from t1,t2 natural right join t3 order by 1,2;
select * from t1,t2 natural right join t3 order by t1.i,t2.i,t3.i;
select * from t1,t2 right join t3 on (t2.i=t3.i) order by t1.i,t2.i,t3.i;
select * from t2 natural right join t3,t1 order by t1.i;
select t1.i,t2.i,t3.i from t2 natural right join t3,t1 order by t1.i,t2.i,t3.i;
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;
drop table t1,t2,t3;
......
......@@ -678,7 +678,7 @@ SELECT t2.a,t2.b,t3.a1,t3.b
FROM t2 LEFT JOIN t3 ON t2.b=t3.b
WHERE t2.a = 4 OR (t2.a > 4 AND t3.a1 IS NULL);
SELECT *
SELECT t2.a,t2.b,t3.a1,t3.b
FROM t2 NATURAL LEFT JOIN t3
WHERE t2.a = 4 OR (t2.a > 4 AND t3.a1 IS NULL);
......
......@@ -19,11 +19,11 @@ select t1.*,t2.* from t1 JOIN t2 where t1.a=t2.a;
select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) order by t1.grp,t1.a,t2.c;
select t1.*,t2.* from { oj t2 left outer join t1 on (t1.a=t2.a) };
select t1.*,t2.* from t1 as t0,{ oj t2 left outer join t1 on (t1.a=t2.a) } WHERE t0.a=2;
select * from t1 left join t2 using (a);
select t1.*,t2.* from t1 left join t2 on t1.a=t2.a;
select * from t1 left join t2 using (a,c);
select * from t1 left join t2 using (c);
select * from t1 natural left outer join t2;
select t1.*,t2.* from t1 left join t2 using (a);
select t1.*,t2.* from t1 left join t2 using (a) where t1.a=t2.a;
select t1.*,t2.* from t1 left join t2 using (a,c);
select t1.*,t2.* from t1 left join t2 using (c);
select t1.*,t2.* from t1 natural left outer join t2;
select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id=3;
select t1.*,t2.* from t1 left join t2 on (t1.a=t2.a) where t2.id is null;
......@@ -44,9 +44,9 @@ select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on
select t1.*,t2.*,t3.a from t1 left join t2 on (t3.a=t2.a) left join t1 as t3 on (t2.a=t3.a);
# Test of inner join
select * from t1 inner join t2 using (a);
select t1.*,t2.* from t1 inner join t2 using (a);
select t1.*,t2.* from t1 inner join t2 on (t1.a=t2.a);
select * from t1 natural join t2;
select t1.*,t2.* from t1 natural join t2;
drop table t1,t2;
......@@ -325,7 +325,7 @@ INSERT INTO t2 VALUES (2,'y');
INSERT INTO t2 VALUES (3,'z');
SELECT t2.id2 FROM t2 LEFT OUTER JOIN t1 ON t1.id2 = t2.id2 WHERE id1 IS NULL;
SELECT id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL;
SELECT t2.id2 FROM t2 NATURAL LEFT OUTER JOIN t1 WHERE id1 IS NULL;
drop table t1,t2;
......@@ -430,6 +430,8 @@ insert into t1 values(1),(2);
insert into t2 values(2),(3);
insert into t3 values(2),(4);
select * from t1 natural left join t2 natural left join t3;
select * from t1 natural left join t2 where (t2.i is not null)=0;
select * from t1 natural left join t2 where (t2.i is not null) is not null;
select * from t1 natural left join t2 where (i is not null)=0;
select * from t1 natural left join t2 where (i is not null) is not null;
drop table t1,t2,t3;
......@@ -656,6 +658,8 @@ create table t1 (a int, b varchar(20));
create table t2 (a int, c varchar(20));
insert into t1 values (1,"aaaaaaaaaa"),(2,"bbbbbbbbbb");
insert into t2 values (1,"cccccccccc"),(2,"dddddddddd");
select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by t1.a;
select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by t1.a;
select group_concat(t1.b,t2.c) from t1 left join t2 using(a) group by a;
select group_concat(t1.b,t2.c) from t1 inner join t2 using(a) group by a;
drop table t1, t2;
......
......@@ -152,18 +152,12 @@ INSERT INTO t1 (order_id, product_id, product_type) VALUES
INSERT INTO t2 (order_id, product_id, product_type) VALUES
('9d9aad7764b5b2c53004348ef8d34500',2315652, 3);
select t1.* from t1 left join t2
on (t1.order_id = t2.order_id and
t1.product_id = t2.product_id and
t1.product_type = t2.product_type)
where t2.order_id = NULL;
select t1.* from t1 left join t2
on (t1.order_id = t2.order_id and
t1.product_id = t2.product_id and
t1.product_type = t2.product_type)
select t1.* from t1
left join t2 using(order_id, product_id, product_type)
where t2.order_id=NULL;
select t1.* from t1
left join t2 using(order_id, product_id, product_type)
where t2.order_id is NULL;
drop table t1,t2;
#
......
......@@ -151,11 +151,22 @@ INSERT INTO t2 VALUES (2,25);
INSERT INTO t3 VALUES (1,'123 Park Place');
INSERT INTO t3 VALUES (2,'453 Boardwalk');
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 USING(c)
LEFT JOIN t3 ON t3.c = t1.c;
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 ON t1.c = t2.c
LEFT JOIN t3 ON t3.c = t1.c;
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 USING(c)
LEFT JOIN t3 ON t3.c = t1.c
ORDER BY a;
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
FROM t1
LEFT JOIN t2 ON t1.c = t2.c
......@@ -339,7 +350,9 @@ CREATE TABLE t2 (
INSERT INTO t1 (titre,auteur,dest) VALUES ('test','joce','bug');
INSERT INTO t2 (numeropost,pseudo) VALUES (1,'joce'),(1,'bug');
SELECT titre,t1.numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
SELECT titre,numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
SELECT titre,t1.numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
SELECT titre,numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;
drop table t1,t2;
......
......@@ -1538,6 +1538,11 @@ select t2.fld1,t22.fld1 from t2,t2 t22 where t2.fld1 >= 250501 and t2.fld1 <= 25
#
insert into t2 (fld1, companynr) values (999999,99);
select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
select count(*) from t2 left join t4 using (companynr) where t4.companynr is not null;
explain select t2.companynr,companyname from t2 left join t4 using (companynr) where t4.companynr is null;
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr is null;
select companynr,companyname from t2 left join t4 using (companynr) where companynr is null;
select count(*) from t2 left join t4 using (companynr) where companynr is not null;
explain select companynr,companyname from t2 left join t4 using (companynr) where companynr is null;
......@@ -1547,10 +1552,18 @@ delete from t2 where fld1=999999;
#
# Test left join optimization
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0;
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0;
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 and t4.companynr > 0;
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0;
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0;
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 and companynr > 0;
# Following can't be optimized
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr is null;
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where t2.companynr > 0 or t2.companynr < 0 or t4.companynr > 0;
explain select t2.companynr,companyname from t4 left join t2 using (companynr) where ifnull(t2.companynr,1)>0;
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr is null;
explain select companynr,companyname from t4 left join t2 using (companynr) where companynr > 0 or companynr < 0 or companynr > 0;
explain select companynr,companyname from t4 left join t2 using (companynr) where ifnull(companynr,1)>0;
......
......@@ -1132,7 +1132,9 @@ CREATE TABLE t1 (id INT);
CREATE TABLE t2 (id INT);
INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (1);
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;
DROP TABLE t1,t2;
......
......@@ -157,11 +157,18 @@ create table t1 ( id integer unsigned not null primary key );
create table t2 ( id integer unsigned not null primary key );
insert into t1 values (1), (2);
insert into t2 values (1);
select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id );
select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id);
create table t3 (id_A integer unsigned not null, id_B integer unsigned null );
insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id );
select * from t3;
delete from t3;
insert into t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id);
select * from t3;
drop table t3;
create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 using ( id );
select * from t3;
drop table t3;
create table t3 select t1.id as id_A, t2.id as id_B from t1 left join t2 on (t1.id = t2.id);
select * from t3;
drop table t1,t2,t3;
......
......@@ -2609,8 +2609,6 @@ find_field_in_view(THD *thd, TABLE_LIST *table_list,
thd [in] thread handler
table_ref [in] table reference to search
name [in] name of field
table_name [in] optional table name that qualifies the field
db_name [in] optional database name that qualifies the field
length [in] length of name
ref [in/out] if 'name' is resolved to a view field, ref is
set to point to the found view field
......@@ -2621,6 +2619,12 @@ find_field_in_view(THD *thd, TABLE_LIST *table_list,
belongs - differs from 'table_list' only for
NATURAL/USING joins
DESCRIPTION
Search for a field among the result fields of a NATURAL/USING join.
Notice that this procedure is called only for non-qualified field
names. In the case of qualified fields, we search directly the base
tables of a natural join.
RETURN
NULL if the field was not found
WRONG_GRANT if no access rights to the found field
......@@ -2629,7 +2633,6 @@ find_field_in_view(THD *thd, TABLE_LIST *table_list,
static Field *
find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
const char *table_name, const char *db_name,
uint length, Item **ref, bool check_grants,
bool register_tree_change,
TABLE_LIST **actual_table)
......@@ -2651,27 +2654,6 @@ find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
if (!(nj_col= field_it++))
DBUG_RETURN(NULL);
if (table_name)
{
/*
Coalesced columns cannot be qualified unless this is the execute phase
of prepared statements. The reason is that they do not belong to any
table, but for PS the prepare phase already resolves and stores
items, so during the execution phase we resolve fully qualified items.
*/
if (!thd->current_arena->is_stmt_execute() && nj_col->is_coalesced)
continue;
if (table_name[0] &&
my_strcasecmp(table_alias_charset, nj_col->table_name(), table_name))
continue;
if (db_name && db_name[0])
{
const char *cur_db_name= nj_col->db_name();
if (cur_db_name && strcmp(db_name, cur_db_name))
continue;
}
}
if (!my_strcasecmp(system_charset_info, nj_col->name(), name))
break;
}
......@@ -2864,11 +2846,35 @@ find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
*actual_table= table_list;
}
else if (table_list->is_natural_join)
fld= find_field_in_natural_join(thd, table_list, name, table_name,
db_name, length, ref,
{
if (table_name && table_name[0])
{
/*
Qualified field; Search for it in the tables used by the natural join.
*/
List_iterator<TABLE_LIST> it(table_list->nested_join->join_list);
TABLE_LIST *table;
while ((table= it++))
{
if ((fld= find_field_in_table_ref(thd, table, name, item_name,
table_name, db_name, length, ref,
check_grants_table,
check_grants_view,
allow_rowid, cached_field_index_ptr,
register_tree_change, actual_table)))
DBUG_RETURN(fld);
}
DBUG_RETURN(0);
}
/*
Non-qualified field, search directly in the result columns of the
natural join.
*/
fld= find_field_in_natural_join(thd, table_list, name, length, ref,
/* TIMOUR_TODO: check this with Sanja */
check_grants_table || check_grants_view,
register_tree_change, actual_table);
}
else
{
if ((fld= find_field_in_table(thd, table_list->table, name, length,
......@@ -2937,9 +2943,11 @@ find_field_in_tables(THD *thd, Item_ident *item,
const char *name= item->field_name;
uint length=(uint) strlen(name);
char name_buff[NAME_LEN+1];
bool allow_rowid;
TABLE_LIST *cur_table= first_table;
TABLE_LIST *actual_table;
bool is_qualified= table_name && table_name[0];
bool allow_rowid= is_qualified ?
TRUE : (cur_table && !cur_table->next_local);
if (item->cached_table)
{
......@@ -3010,55 +3018,68 @@ find_field_in_tables(THD *thd, Item_ident *item,
if (last_table)
last_table= last_table->next_name_resolution_table;
/* The field we search for is qualified with a table name and optional db. */
if (table_name && table_name[0])
{
for (; cur_table != last_table ;
cur_table= cur_table->next_name_resolution_table)
{
Field *cur_field= find_field_in_table_ref(thd, cur_table, name,
item->name, table_name,
db, length, ref,
Field *cur_field= find_field_in_table_ref(thd, cur_table, name, item->name,
is_qualified ? table_name : NULL,
is_qualified ? db : NULL,
length, ref,
(cur_table->table &&
test(cur_table->table->grant.
want_privilege) &&
check_privileges),
(test(cur_table->grant.
want_privilege) &&
check_privileges),
1, &(item->cached_field_index),
want_privilege)
&& check_privileges),
allow_rowid,
&(item->cached_field_index),
register_tree_change,
&actual_table);
if (cur_field)
{
if (cur_field == WRONG_GRANT)
return (Field*) 0;
/*
Store the original table of the field, which may be different from
cur_table in the case of NATURAL/USING join.
*/
item->cached_table= (!actual_table->cacheable_table) ? 0 : actual_table;
item->cached_table= (!actual_table->cacheable_table || found) ?
0 : actual_table;
if (cur_field == WRONG_GRANT)
return (Field*) 0;
if (db || !thd->where)
DBUG_ASSERT(thd->where);
/*
If we found a fully qualified field we return it directly as it can't
have duplicates.
*/
if (is_qualified && db)
return cur_field;
if (found)
{
if (report_error == REPORT_ALL_ERRORS ||
report_error == IGNORE_EXCEPT_NON_UNIQUE)
my_error(ER_NON_UNIQ_ERROR, MYF(0),
item->full_name(),thd->where);
is_qualified ? item->full_name() : name, thd->where);
return (Field*) 0;
}
found= cur_field;
}
}
if (found)
return found;
/*
If there were no tables to search, we wouldn't go through the loop and
cur_table wouldn't be updated by the loop increment part.
If the field was qualified and there were no tables to search, issue
an error that an unknown table was given. The situation is detected
as follows: if there were no tables we wouldn't go through the loop
and cur_table wouldn't be updated by the loop increment part, so it
will be equal to the first table.
*/
if (cur_table == first_table && (report_error == REPORT_ALL_ERRORS ||
if (is_qualified && (cur_table == first_table) &&
(report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE))
{
char buff[NAME_LEN*2+1];
......@@ -3072,62 +3093,12 @@ find_field_in_tables(THD *thd, Item_ident *item,
else
if (report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE)
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(),thd->where);
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
else
return (Field*) not_found_field;
return (Field*) 0;
}
/* The field we search for is not qualified. */
allow_rowid= cur_table && !cur_table->next_local;
for (; cur_table != last_table ;
cur_table= cur_table->next_name_resolution_table)
{
Field *cur_field= find_field_in_table_ref(thd, cur_table, name, item->name,
NULL, NULL, length, ref,
(cur_table->table &&
test(cur_table->table->grant.
want_privilege) &&
check_privileges),
(test(cur_table->grant.
want_privilege)
&& check_privileges),
allow_rowid,
&(item->cached_field_index),
register_tree_change,
&actual_table);
if (cur_field)
{
if (cur_field == WRONG_GRANT)
return (Field*) 0;
/*
Store the original table of the field, which may be different from
cur_table in the case of NATURAL/USING join.
*/
item->cached_table= (!actual_table->cacheable_table || found) ?
0 : actual_table;
found= not_found_field;
if (found)
{
if (!thd->where) // Returns first found
break;
if (report_error == REPORT_ALL_ERRORS ||
report_error == IGNORE_EXCEPT_NON_UNIQUE)
my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
return (Field*) 0;
}
found= cur_field;
}
}
if (found)
DBUG_ASSERT(!found || found == not_found_field);
return found;
if (report_error == REPORT_ALL_ERRORS ||
report_error == REPORT_EXCEPT_NON_UNIQUE)
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
else
return (Field*) not_found_field;
return (Field*) 0;
}
......@@ -3532,10 +3503,6 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
Item_ident *item_ident_1, *item_ident_2;
Item_func_eq *eq_cond;
DBUG_PRINT("info", ("new equi-join condition: %s.%s = %s.%s",
table_ref_1->alias, field_1->field_name,
table_ref_2->alias, field_2->field_name));
if (!item_1 || !item_2)
goto err; // out of memory
......@@ -3582,7 +3549,6 @@ mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
eq_cond);
nj_col_1->is_common= nj_col_2->is_common= TRUE;
nj_col_1->is_coalesced= nj_col_2->is_coalesced= TRUE;
if (field_1)
{
......@@ -4331,29 +4297,28 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
}
found= FALSE;
for (TABLE_LIST *tables= context->first_name_resolution_table;
/*
If table names are qualified, then loop over all tables used in the query,
else treat natural joins as leaves and do not iterate over their underlying
tables.
*/
for (TABLE_LIST *tables= (table_name ? context->table_list :
context->first_name_resolution_table);
tables;
tables= tables->next_name_resolution_table)
tables= (table_name ? tables->next_local :
tables->next_name_resolution_table)
)
{
Field *field;
TABLE *table= tables->table;
DBUG_ASSERT(tables->is_leaf_for_name_resolution());
/*
If optional table and db names do not match the ones used to qualify
the field being expanded, skip this table reference. However if this is
a NATURAL/USING join, we can't simply skip the whole table reference,
because its columns may come from different tables/views. For NATURAL/
USING joins we perform this test for each column in the loop below.
*/
if (!tables->is_natural_join)
{
if (table_name && my_strcasecmp(table_alias_charset, table_name,
tables->alias) ||
(db_name && strcmp(tables->db,db_name)))
continue;
}
#ifndef NO_EMBEDDED_ACCESS_CHECKS
/* Ensure that we have access rights to all fields to be inserted. */
......@@ -4390,21 +4355,6 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
{
Item *item;
/*
If this is a column of a NATURAL/USING join, and the star was
qualified with a table (and database) name, check if the
column is not a coalesced one, and if not, that is belongs to
the same table.
*/
if (tables->is_natural_join && table_name)
{
if (field_iterator.is_coalesced() ||
my_strcasecmp(table_alias_charset, table_name,
field_iterator.table_name()) ||
(db_name && strcmp(db_name, field_iterator.db_name())))
continue;
}
if (!(item= field_iterator.create_item(thd)))
DBUG_RETURN(TRUE);
......@@ -4431,18 +4381,18 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
tables->is_natural_join);
DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
Item_field *fld= (Item_field*) item;
const char *table_name= field_iterator.table_name();
const char *field_table_name= field_iterator.table_name();
if (!tables->schema_table &&
!(fld->have_privileges=
(get_column_grant(thd, field_iterator.grant(),
field_iterator.db_name(),
table_name, fld->field_name) &
field_table_name, fld->field_name) &
VIEW_ANY_ACL)))
{
my_error(ER_COLUMNACCESS_DENIED_ERROR, MYF(0), "ANY",
thd->priv_user, thd->host_or_ip,
fld->field_name, table_name);
fld->field_name, field_table_name);
DBUG_RETURN(TRUE);
}
}
......
......@@ -2289,7 +2289,6 @@ Natural_join_column::Natural_join_column(Field_translator *field_param,
table_field= NULL;
table_ref= tab;
is_common= FALSE;
is_coalesced= FALSE;
}
......@@ -2301,7 +2300,6 @@ Natural_join_column::Natural_join_column(Field *field_param,
view_field= NULL;
table_ref= tab;
is_common= FALSE;
is_coalesced= FALSE;
}
......@@ -2638,13 +2636,6 @@ GRANT_INFO *Field_iterator_table_ref::grant()
}
bool Field_iterator_table_ref::is_coalesced()
{
if (table_ref->is_natural_join)
return natural_join_it.column_ref()->is_coalesced;
return FALSE;
}
/*
Create new or return existing column reference to a column of a
natural/using join.
......
......@@ -389,12 +389,6 @@ public:
we are looking at some column.
*/
bool is_common;
/*
A column is coalesced if it was common in some of several nested NATURAL/
USING joins. We have to know this, because according to ANSI, coalesced
columns cannot be qualified.
*/
bool is_coalesced;
public:
Natural_join_column(Field_translator *field_param, st_table_list *tab);
Natural_join_column(Field *field_param, st_table_list *tab);
......@@ -742,7 +736,6 @@ public:
const char *table_name();
const char *db_name();
GRANT_INFO *grant();
bool is_coalesced();
Item *create_item(THD *thd) { return field_it->create_item(thd); }
Field *field() { return field_it->field(); }
Natural_join_column *get_or_create_column_ref(THD *thd, bool *is_created);
......
......@@ -11801,8 +11801,8 @@ static void test_bug6046()
stmt= mysql_stmt_init(mysql);
stmt_text= "SELECT a FROM t1 NATURAL JOIN t1 as X1 "
"WHERE b > ? ORDER BY a";
stmt_text= "SELECT t1.a FROM t1 NATURAL JOIN t1 as X1 "
"WHERE t1.b > ? ORDER BY t1.a";
rc= mysql_stmt_prepare(stmt, stmt_text, strlen(stmt_text));
check_execute(stmt, rc);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment