215 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			215 lines
		
	
	
		
			6.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| drop table if exists t1,t2;
 | |
| create table t1 (pk int primary key, grp int, a int, c char(10) not null);
 | |
| insert into t1 values (1, 1,1,'a');
 | |
| insert into t1 values (2, 2,2,'b');
 | |
| insert into t1 values (3, 2,3,'c');
 | |
| insert into t1 values (4, 3,4,'E');
 | |
| insert into t1 values (5, 3,5,'C');
 | |
| insert into t1 values (6, 3,6,'D');
 | |
| select a,c,sum(a) from t1 group by a order by a;
 | |
| a	c	sum(a)
 | |
| 1	a	1
 | |
| 2	b	2
 | |
| 3	c	3
 | |
| 4	E	4
 | |
| 5	C	5
 | |
| 6	D	6
 | |
| select a,c,sum(a) from t1 where a > 10 group by a order by a;
 | |
| a	c	sum(a)
 | |
| select sum(a) from t1 where a > 10;
 | |
| sum(a)
 | |
| NULL
 | |
| select count(distinct a),count(distinct grp) from t1;
 | |
| count(distinct a)	count(distinct grp)
 | |
| 6	3
 | |
| insert into t1 values (7, null,null,'');
 | |
| select count(distinct a),count(distinct grp) from t1;
 | |
| count(distinct a)	count(distinct grp)
 | |
| 6	3
 | |
| create table t2 (pk int primary key, grp int, a int, c char(10));
 | |
| select grp,max(a)+max(grp),max(c) from t1 group by grp order by grp;
 | |
| grp	max(a)+max(grp)	max(c)
 | |
| NULL	NULL	
 | |
| 1	2	a
 | |
| 2	5	c
 | |
| 3	9	E
 | |
| select * from t2;
 | |
| pk	grp	a	c
 | |
| drop table t1,t2;
 | |
| drop table if exists t1;
 | |
| CREATE TABLE t1 (id int ,value1 decimal(10,2),c int primary key);
 | |
| INSERT INTO t1 VALUES (1,0.00,1),(1,1.00,2), (1,2.00,3), (2,10.00,4), (2,11.00,5), (2,12.00,6);
 | |
| CREATE TABLE t2 (id int primary key,name char(20));
 | |
| INSERT INTO t2 VALUES (1,'Set One'),(2,'Set Two');
 | |
| select id, avg(value1)  from t1 group by id;
 | |
| id	avg(value1)
 | |
| 1	1.000000
 | |
| 2	11.000000
 | |
| select name, avg(value1) from t1, t2 where t1.id = t2.id group by t1.id;
 | |
| name	avg(value1)
 | |
| Set One	1.000000
 | |
| Set Two	11.000000
 | |
| drop table t1,t2;
 | |
| create table t1 (pk int primary key, id int not null);
 | |
| create table t2 (pk int primary key, id int not null,rating int null);
 | |
| insert into t1 values(1,1),(2,2),(3,3);
 | |
| insert into t2 values(1, 1, 3),(2, 2, NULL),(3, 2, NULL),(4, 3, 2),(5, 3, NULL);
 | |
| select t1.id, avg(rating) from t1 left join t2 on ( t1.id = t2.id ) group by t1.id order by t1.id;
 | |
| id	avg(rating)
 | |
| 1	3.0000
 | |
| 2	NULL
 | |
| 3	2.0000
 | |
| drop table t1,t2;
 | |
| create table t1 (a int primary key, c char(10), b char(128));
 | |
| INSERT INTO t1 VALUES (1,'1','1');
 | |
| INSERT INTO t1 VALUES (2,'2','2');
 | |
| INSERT INTO t1 VALUES (4,'4','4');
 | |
| select count(*) from t1;
 | |
| count(*)
 | |
| 3
 | |
| select count(*) from t1 where a = 1;
 | |
| count(*)
 | |
| 1
 | |
| select count(*) from t1 where a = 100;
 | |
| count(*)
 | |
| 0
 | |
| select count(*) from t1 where a >= 10;
 | |
| count(*)
 | |
| 0
 | |
| select count(a) from t1 where a = 1;
 | |
| count(a)
 | |
| 1
 | |
| select count(a) from t1 where a = 100;
 | |
| count(a)
 | |
| 0
 | |
| select count(a) from t1 where a >= 10;
 | |
| count(a)
 | |
| 0
 | |
| select count(b) from t1 where b >= 2;
 | |
| count(b)
 | |
| 2
 | |
| select count(b) from t1 where b >= 10;
 | |
| count(b)
 | |
| 0
 | |
| select count(c) from t1 where c = 10;
 | |
| count(c)
 | |
| 0
 | |
| drop table t1;
 | |
| CREATE TABLE t1 (d datetime default now(), i int primary key);
 | |
| INSERT INTO t1(i) VALUES (1);
 | |
| SELECT COUNT(i), i, COUNT(i)*i FROM t1 GROUP BY i;
 | |
| COUNT(i)	i	COUNT(i)*i
 | |
| 1	1	1
 | |
| SELECT COUNT(i), (i+0), COUNT(i)*(i+0) FROM t1 GROUP BY i;
 | |
| COUNT(i)	(i+0)	COUNT(i)*(i+0)
 | |
| 1	1	1
 | |
| DROP TABLE t1;
 | |
| create table t1 (
 | |
| pk int primary key,
 | |
| num float,
 | |
| my_user char(20)
 | |
| );
 | |
| insert into t1 values (1, 10.3,'nem'),(2, 20.53,'monty'),(3, 30.23,'sinisa');
 | |
| insert into t1 values (4, 30.13,'nem'),(5, 20.98,'monty'),(6, 10.45,'sinisa');
 | |
| insert into t1 values (7, 5.2,'nem'),(8, 8.64,'monty'),(9, 11.12,'sinisa');
 | |
| select sum(num) from t1;
 | |
| sum(num)
 | |
| 147.57999897003174
 | |
| select sum(num) from t1 group by my_user order by my_user;
 | |
| sum(num)
 | |
| 50.15000057220459
 | |
| 45.6299991607666
 | |
| 51.79999923706055
 | |
| drop table t1;
 | |
| drop table if exists t1,t2,t3;
 | |
| create table t1 (pk int primary key, a1 int, a2 char(3));
 | |
| insert into t1 values(1, 10,'aaa'), (2, 10,null), (3, 10,'bbb'), (4, 20,'zzz');
 | |
| create table t2(pk int primary key, a1 char(3), a2 int, a3 float);
 | |
| create table t3(pk int primary key, a1 char(3), a2 int, a3 float);
 | |
| select * from t1;
 | |
| pk	a1	a2
 | |
| 1	10	aaa
 | |
| 2	10	NULL
 | |
| 3	10	bbb
 | |
| 4	20	zzz
 | |
| select min(a2) from t1;
 | |
| min(a2)
 | |
| aaa
 | |
| select max(t1.a1), max(t2.a2) from t1, t2;
 | |
| max(t1.a1)	max(t2.a2)
 | |
| NULL	NULL
 | |
| insert into t2 values(1, 'AAA', 10, 0.5);
 | |
| insert into t2 values(2, 'BBB', 20, 1.0);
 | |
| select t1.a1, t1.a2, t2.a1, t2.a2 from t1 left outer join t3 on t1.a1=10;
 | |
| ERROR 42S22: Unknown column 't2.a1' in 'field list'
 | |
| select max(t1.a2) from t1 left outer join t2 on t1.a1=10;
 | |
| max(t1.a2)
 | |
| zzz
 | |
| select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and 1=0 where t2.a1='AAA';
 | |
| max(t2.a1)
 | |
| NULL
 | |
| explain select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and 1=0 where t2.a1='AAA';
 | |
| select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1   where t2.a1='AAA';
 | |
| max(t2.a1)
 | |
| AAA
 | |
| explain select max(t2.a1) from t1 left outer join t2 on t1.a2=t2.a1  where t2.a1='AAA';
 | |
| select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk   where t2.a1='AAA' and 1=0;
 | |
| max(t2.a1)
 | |
| NULL
 | |
| explain select max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk  where t2.a1='AAA' and 1=0;
 | |
| select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.a1=10;
 | |
| max(t1.a2)	max(t2.a1)
 | |
| zzz	BBB
 | |
| explain select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.a1=10;
 | |
| select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.pk=10;
 | |
| max(t1.a2)	max(t2.a1)
 | |
| zzz	NULL
 | |
| explain select max(t1.a2),max(t2.a1) from t1 left outer join t2 on t1.pk=t2.pk and t1.pk=10;
 | |
| select * from t1 left join t2 on t1.pk=t2.pk where t2.a2 > 1 and t2.a2 < 10;
 | |
| pk	a1	a2	pk	a1	a2	a3
 | |
| explain select * from t1 left join t2 on t1.pk=t2.pk where t2.a2 > 1 and t2.a2 < 10;
 | |
| select * from t1 left join t2 on t1.pk=t2.pk and t2.a2 > 1 and t2.a2 < 10 order by t1.pk;
 | |
| pk	a1	a2	pk	a1	a2	a3
 | |
| 1	10	aaa	NULL	NULL	NULL	NULL
 | |
| 2	10	NULL	NULL	NULL	NULL	NULL
 | |
| 3	10	bbb	NULL	NULL	NULL	NULL
 | |
| 4	20	zzz	NULL	NULL	NULL	NULL
 | |
| explain select * from t1 left join t2 on t1.pk=t2.pk and t2.a2 > 1 and t2.a2 < 10;
 | |
| drop table t1,t2;
 | |
| CREATE TABLE t1 (pk int primary key, a int, b int);
 | |
| select count(b), sum(b), avg(b),  min(b), max(b) from t1;
 | |
| count(b)	sum(b)	avg(b)	min(b)	max(b)
 | |
| 0	NULL	NULL	NULL	NULL
 | |
| select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
 | |
| a	count(b)	sum(b)	avg(b)	min(b)	max(b)
 | |
| insert into t1 values (1, 1,null);
 | |
| select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
 | |
| a	count(b)	sum(b)	avg(b)	min(b)	max(b)
 | |
| 1	0	NULL	NULL	NULL	NULL
 | |
| insert into t1 values (2, 1,null);
 | |
| insert into t1 values (3, 2,null);
 | |
| select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
 | |
| a	count(b)	sum(b)	avg(b)	min(b)	max(b)
 | |
| 1	0	NULL	NULL	NULL	NULL
 | |
| 2	0	NULL	NULL	NULL	NULL
 | |
| insert into t1 values (4, 2,1);
 | |
| select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
 | |
| a	count(b)	sum(b)	avg(b)	min(b)	max(b)
 | |
| 1	0	NULL	NULL	NULL	NULL
 | |
| 2	1	1	1.0000	1	1
 | |
| insert into t1 values (5, 3,1);
 | |
| select a,count(b), sum(b), avg(b), min(b), max(b) from t1 group by a order by a;
 | |
| a	count(b)	sum(b)	avg(b)	min(b)	max(b)
 | |
| 1	0	NULL	NULL	NULL	NULL
 | |
| 2	1	1	1.0000	1	1
 | |
| 3	1	1	1.0000	1	1
 | |
| drop table t1;
 | |
| create table t1 (col int primary key,col2 int);
 | |
| insert into t1(col) values (-1), (-2), (-3);
 | |
| drop table t1;
 | |
| create table t1 (a int primary key,b int);
 | |
| select avg(2) from t1;
 | |
| avg(2)
 | |
| NULL
 | |
| drop table t1;
 |