1
drop table if exists t1,t2,t3;
2
create table t1 (a int);
3
select count(a) as b from t1 where a=0 having b > 0;
5
insert into t1 values (null);
6
select count(a) as b from t1 where a=0 having b > 0;
8
select count(a) as b from t1 where a=0 having b >=0;
11
explain extended select count(a) as b from t1 where a=0 having b >=0;
12
id select_type table type possible_keys key key_len ref rows filtered Extra
13
1 SIMPLE t1 ALL NULL NULL NULL NULL 1 100.00 Using where
15
Note 1003 select count(`test`.`t1`.`a`) AS `b` from `test`.`t1` where (`test`.`t1`.`a` = 0) having (`b` >= 0)
18
raw_id int NOT NULL default '0',
19
chr_start int NOT NULL default '0',
20
chr_end int NOT NULL default '0',
21
raw_start int NOT NULL default '0',
22
raw_end int NOT NULL default '0',
23
raw_ori int NOT NULL default '0'
25
INSERT INTO t1 VALUES (469713,1,164123,1,164123,1),(317330,164124,317193,101,153170,1),(469434,317194,375620,101,58527,1),(591816,375621,484273,1,108653,1),(591807,484274,534671,91,50488,1),(318885,534672,649362,101,114791,1),(318728,649363,775520,102,126259,1),(336829,775521,813997,101,38577,1),(317740,813998,953227,101,139330,1),(1,813998,953227,101,139330,1);
27
id int NOT NULL default '0',
28
contig_id int NOT NULL default '0',
29
seq_start int NOT NULL default '0',
30
seq_end int NOT NULL default '0',
31
strand int NOT NULL default '0',
34
INSERT INTO t2 VALUES (133195,469713,61327,61384,1),(133196,469713,64113,64387,1),(133197,1,1,1,0),(133197,1,1,1,-2);
36
MIN( IF(sgp.raw_ori=1,
37
(e.seq_start+sgp.chr_start-sgp.raw_start),
38
(sgp.chr_start+sgp.raw_end-e.seq_end))) as start,
39
MAX( IF(sgp.raw_ori=1,
40
(e.seq_end+sgp.chr_start-sgp.raw_start),
41
(sgp.chr_start+sgp.raw_end-e.seq_start))) as end,
42
AVG(IF (sgp.raw_ori=1,e.strand,(-e.strand))) as chr_strand
45
WHERE sgp.raw_id=e.contig_id
47
HAVING chr_strand= -1 and end >= 0
49
id start end chr_strand
50
133197 813898 813898 -1.0000
52
CREATE TABLE t1 (Fld1 int default NULL,Fld2 int default NULL);
53
INSERT INTO t1 VALUES (1,10),(1,20),(2,NULL),(2,NULL),(3,50);
54
select Fld1, max(Fld2) as q from t1 group by Fld1 having q is not null;
58
select Fld1, max(Fld2) from t1 group by Fld1 having max(Fld2) is not null;
62
select Fld1, max(Fld2) from t1 group by Fld1 having avg(Fld2) is not null;
66
select Fld1, max(Fld2) from t1 group by Fld1 having std(Fld2) is not null;
70
select Fld1, max(Fld2) from t1 group by Fld1 having variance(Fld2) is not null;
75
create table t1 (id int not null, qty int not null);
76
insert into t1 values (1,2),(1,3),(2,4),(2,5);
77
select id, sum(qty) as sqty from t1 group by id having sqty>2;
81
select sum(qty) as sqty from t1 group by id having count(id) > 0;
85
select sum(qty) as sqty from t1 group by id having count(distinct id) > 0;
91
`id` bigint NOT NULL default '0',
95
`id` bigint NOT NULL default '0',
96
`description` varchar(20)
98
INSERT INTO t1 VALUES (1, 'test');
99
INSERT INTO t2 VALUES (1, 'test');
101
`id` bigint NOT NULL default '0',
102
`order_id` bigint NOT NULL default '0'
107
from t1 a left join t3 b on a.id=b.order_id
108
group by a.id, a.description
109
having (a.description is not null) and (c=0);
115
from t2 a left join t3 b on a.id=b.order_id
116
group by a.id, a.description
117
having (a.description is not null) and (c=0);
120
INSERT INTO t1 VALUES (2, 'test2');
124
from t1 a left join t3 b on a.id=b.order_id
125
group by a.id, a.description
126
having (a.description is not null) and (c=0);
131
CREATE TABLE t1 (a int);
132
INSERT INTO t1 VALUES (3), (4), (1), (3), (1);
133
SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a)>0;
138
SELECT SUM(a) FROM t1 GROUP BY a HAVING SUM(a);
144
CREATE TABLE t1 (a int);
145
INSERT INTO t1 VALUES (1), (2), (1), (3), (2), (1);
146
SELECT a FROM t1 GROUP BY a HAVING a > 1;
150
SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
152
SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
154
EXPLAIN SELECT a FROM t1 GROUP BY a HAVING 1 != 1 AND a > 1;
155
id select_type table type possible_keys key key_len ref rows Extra
156
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
157
EXPLAIN SELECT 0 AS x, a FROM t1 GROUP BY x,a HAVING x=1 AND a > 1;
158
id select_type table type possible_keys key key_len ref rows Extra
159
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
161
CREATE TABLE t1 (a int PRIMARY KEY);
162
CREATE TABLE t2 (b int PRIMARY KEY, a int);
163
CREATE TABLE t3 (b int, flag int);
164
INSERT INTO t1 VALUES (1);
165
INSERT INTO t2 VALUES (1,1), (2,1), (3,1);
166
INSERT INTO t3(b,flag) VALUES (2, 1);
168
FROM t1 INNER JOIN t2 ON t1.a=t2.a LEFT JOIN t3 ON t2.b=t3.b
169
GROUP BY t1.a, t2.b HAVING MAX(t3.flag)=0;
171
SELECT DISTINCT t1.a, MAX(t3.flag)
172
FROM t1 INNER JOIN t2 ON t1.a=t2.a LEFT JOIN t3 ON t2.b=t3.b
173
GROUP BY t1.a, t2.b HAVING MAX(t3.flag)=0;
176
FROM t1 INNER JOIN t2 ON t1.a=t2.a LEFT JOIN t3 ON t2.b=t3.b
177
GROUP BY t1.a, t2.b HAVING MAX(t3.flag)=0;
180
create table t1 (col1 int, col2 varchar(5), col_t1 int);
181
create table t2 (col1 int, col2 varchar(5), col_t2 int);
182
create table t3 (col1 int, col2 varchar(5), col_t3 int);
183
insert into t1 values(10,'hello',10);
184
insert into t1 values(20,'hello',20);
185
insert into t1 values(30,'hello',30);
186
insert into t1 values(10,'bye',10);
187
insert into t1 values(10,'sam',10);
188
insert into t1 values(10,'bob',10);
189
insert into t2 select * from t1;
190
insert into t3 select * from t1;
191
select count(*) from t1 group by col1 having col1 = 10;
194
select count(*) as count_col1 from t1 group by col1 having col1 = 10;
197
select count(*) as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
200
select count(*) from t1 group by col2 having col2 = 'hello';
203
select count(*) from t1 group by col2 having col1 = 10;
204
ERROR 42S22: Unknown column 'col1' in 'having clause'
205
select col1 as count_col1 from t1 as tmp1 group by col1 having col1 = 10;
208
select col1 as count_col1 from t1 as tmp1 group by col1 having count_col1 = 10;
211
select col1 as count_col1 from t1 as tmp1 group by count_col1 having col1 = 10;
214
select col1 as count_col1 from t1 as tmp1 group by count_col1 having count_col1 = 10;
217
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col1 = 10;
223
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having count_col1 = 10;
229
select col1 as count_col1,col2 from t1 as tmp1 group by col1,col2 having col2 = 'hello';
234
select col1 as count_col1,col2 as group_col2 from t1 as tmp1 group by col1,col2 having group_col2 = 'hello';
235
count_col1 group_col2
239
select sum(col1) as co12 from t1 group by col2 having col2 10;
240
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10' at line 1
241
select sum(col1) as co2, count(col2) as cc from t1 group by col1 having col1 =10;
244
select t2.col2 from t2 group by t2.col1, t2.col2 having t1.col1 <= 10;
245
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
246
select t1.col1 from t1
248
(select t2.col2 from t2
249
group by t2.col1, t2.col2 having t2.col1 <= 10);
257
select t1.col1 from t1
259
(select t2.col2 from t2
260
group by t2.col1, t2.col2
262
(select min(t3.col1) from t3));
270
select t1.col1 from t1
272
(select t2.col2 from t2
273
group by t2.col1, t2.col2 having t1.col1 <= 10);
279
select t1.col1 as tmp_col from t1
281
(select t2.col2 from t2
282
group by t2.col1, t2.col2 having tmp_col <= 10);
288
select t1.col1 from t1
290
(select t2.col2 from t2
291
group by t2.col1, t2.col2 having col_t1 <= 10);
297
select sum(col1) from t1
299
having (select col_t1 from t2 where col_t1 = col_t2 order by col_t2 limit 1);
304
select t1.col1 from t1
306
(select t2.col2 from t2
307
group by t2.col1, t2.col2 having col_t1 <= 10)
309
ERROR 42S22: Unknown column 'col_t1' in 'having clause'
310
select t1.col1 from t1
312
(select t2.col2 from t2
313
group by t2.col1, t2.col2 having col_t1 <= 10)
318
select col_t1, sum(col1) from t1
320
having col_t1 > 10 and
321
exists (select sum(t2.col1) from t2
322
group by t2.col2 having t2.col2 > 'b');
326
select sum(col1) from t1
328
having col_t1 in (select sum(t2.col1) from t2
329
group by t2.col2, t2.col1 having t2.col1 = t1.col1);
330
ERROR 42S22: Unknown column 't1.col1' in 'having clause'
331
select sum(col1) from t1
333
having col_t1 in (select sum(t2.col1) from t2
334
group by t2.col2, t2.col1 having t2.col1 = col_t1);
339
select t1.col1, t2.col1 from t1, t2 where t1.col1 = t2.col1
340
group by t1.col1, t2.col1 having col1 = 2;
341
ERROR 23000: Column 'col1' in having clause is ambiguous
342
select t1.col1*10+t2.col1 from t1,t2 where t1.col1=t2.col1
343
group by t1.col1, t2.col1 having col1 = 2;
344
ERROR 23000: Column 'col1' in having clause is ambiguous
345
drop table t1, t2, t3;
346
create table t1 (s1 int);
347
insert into t1 values (1),(2),(3);
348
select count(*) from t1 group by s1 having s1 is null;
350
select s1*0 as s1 from t1 group by s1 having s1 <> 0;
356
Warning 1052 Column 's1' in group statement is ambiguous
357
Warning 1052 Column 's1' in having clause is ambiguous
358
select s1*0 from t1 group by s1 having s1 = 0;
360
select s1 from t1 group by 1 having 1 = 0;
362
select count(s1) from t1 group by s1 having count(1+1)=2;
364
select count(s1) from t1 group by s1 having s1*0=0;
369
select * from t1 a, t1 b group by a.s1 having s1 is null;
370
ERROR 23000: Column 's1' in having clause is ambiguous
372
create table t1 (s1 char);
373
insert into t1 values ('b'),('y');
374
select s1,count(s1) from t1 group by s1 having s1 = 'y';
378
DROP SCHEMA IF EXISTS HU;
382
(EMPNUM CHAR(3) NOT NULL UNIQUE,
387
(PNUM CHAR(3) NOT NULL UNIQUE,
392
INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale');
393
INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna');
394
INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna');
395
INSERT INTO STAFF VALUES ('E4','Don',12,'Deale');
396
INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron');
397
INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale');
398
INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna');
399
INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa');
400
INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale');
401
INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna');
402
INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale');
403
SELECT EMPNUM, GRADE*1000
404
FROM HU.STAFF WHERE GRADE * 1000 >
405
ANY (SELECT SUM(BUDGET) FROM HU.PROJ
407
HAVING HU.PROJ.CITY = HU.STAFF.CITY);
412
create table t1(f1 int);
413
select f1 from t1 having max(f1)=f1;
415
select f1 from t1 group by f1 having max(f1)=f1;
417
select f1 from t1 having max(f1)=f1;
419
select f1 from t1 group by f1 having max(f1)=f1;