2
# Testing of found_rows()
5
drop table if exists t1,t2;
8
create table t1 (a int not null auto_increment, b int not null, primary key(a));
9
insert into t1 (b) values (2),(3),(5),(5),(5),(6),(7),(9);
10
select SQL_CALC_FOUND_ROWS * from t1;
12
select SQL_CALC_FOUND_ROWS * from t1 limit 1;
14
select SQL_BUFFER_RESULT SQL_CALC_FOUND_ROWS * from t1 limit 1;
16
select SQL_CALC_FOUND_ROWS * from t1 order by b desc limit 1;
18
select SQL_CALC_FOUND_ROWS distinct b from t1 limit 1;
20
select SQL_CALC_FOUND_ROWS b,count(*) as c from t1 group by b order by c desc limit 1;
22
select SQL_CALC_FOUND_ROWS * from t1 left join t1 as t2 on (t1.b=t2.a) limit 2,1;
27
# Test SQL_CALC_FOUND_ROWS optimization when used with one table and filesort
30
create table t1 (a int not null primary key);
31
insert into t1 values (1),(2),(3),(4),(5);
32
select sql_calc_found_rows a from t1 where a in (1,2,3) order by a desc limit 0,2;
34
select sql_calc_found_rows a from t1 where a in (1,2,3) order by a+2 desc limit 0,2;
39
# Test of SQL_CALC_FOUND_ROWS with DISTINCT
43
`id` smallint(5) unsigned NOT NULL auto_increment,
44
`kid` smallint(5) unsigned NOT NULL default '0',
50
id smallint(5) unsigned NOT NULL auto_increment,
51
name varchar(50) NOT NULL default '',
52
email varchar(50) NOT NULL default '',
54
UNIQUE KEY e_n (email,name)
63
eval INSERT INTO t2 VALUES ($2,'name$2','email$2');
68
EXPLAIN SELECT SQL_CALC_FOUND_ROWS DISTINCT email FROM t2 LEFT JOIN t1 ON kid = t2.id WHERE t1.id IS NULL LIMIT 10;
69
SELECT SQL_CALC_FOUND_ROWS DISTINCT email FROM t2 LEFT JOIN t1 ON kid = t2.id WHERE t1.id IS NULL LIMIT 10;
72
SELECT SQL_CALC_FOUND_ROWS DISTINCT email FROM t2 LEFT JOIN t1 ON kid = t2.id WHERE t1.id IS NULL order by email LIMIT 10;
75
SELECT DISTINCT email FROM t2 LEFT JOIN t1 ON kid = t2.id WHERE t1.id IS NULL LIMIT 10;
76
SELECT DISTINCT email FROM t2 LEFT JOIN t1 ON kid = t2.id WHERE t1.id IS NULL ORDER BY email LIMIT 10;
77
INSERT INTO `t1` (`id`, `kid`) VALUES ('0', '150');
79
SELECT SQL_CALC_FOUND_ROWS DISTINCT email FROM t2 LEFT JOIN t1 ON kid = t2.id WHERE t1.id IS NULL LIMIT 10;
85
# Test bug when using range optimization
89
`titre` char(80) NOT NULL default '',
90
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
91
`maxnumrep` int(10) unsigned NOT NULL default '0',
92
PRIMARY KEY (`numeropost`),
93
KEY `maxnumrep` (`maxnumrep`)
94
) ENGINE=MyISAM ROW_FORMAT=FIXED;
95
INSERT INTO t1 (titre,maxnumrep) VALUES
96
('test1','1'),('test2','2'),('test3','3');
97
SELECT SQL_CALC_FOUND_ROWS titre,numeropost,maxnumrep FROM t1 WHERE numeropost IN (1,2) ORDER BY maxnumrep DESC LIMIT 0, 1;
99
SELECT SQL_CALC_FOUND_ROWS 1 FROM (SELECT 1) as a LIMIT 0;
101
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE numeropost > 1 LIMIT 0;
103
SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 0;
105
SELECT SQL_CALC_FOUND_ROWS * FROM t1 ORDER BY numeropost LIMIT 0;
110
# Test problem with impossible WHERE (Bug #1468)
113
create table t1 (id int, primary key (id));
114
insert into t1 values (1), (2), (3), (4), (5);
115
select SQL_CALC_FOUND_ROWS * from t1 where id > 3 limit 0, 1;
117
select SQL_CALC_FOUND_ROWS * from t1 where id > 3 AND 1=2 limit 0, 1;
119
select SQL_CALC_FOUND_ROWS * from t1 where id > 6 limit 0, 1;
124
# Other bugs with range optimization
129
CREATE TABLE t1 ( a int not null, b int not null, KEY ab(a,b) );
130
INSERT INTO t1 VALUES ( 47, 1 );
131
INSERT INTO t1 VALUES ( 70, 1 );
135
( b =1 AND a BETWEEN 14 AND 21 ) OR
136
( b =2 AND a BETWEEN 16 AND 18 ) OR
137
( b =3 AND a BETWEEN 15 AND 19 )
143
CREATE TABLE t1 ( a integer, u varchar(15), r integer, key uao_idx( r, a, u));
145
WHERE ( r = 1 AND a IN ( 1, 2 ) AND ( u = 'w' OR u LIKE 'w/%' ) )
146
OR ( r = 1 AND a IN ( 3 ) AND ( u = 'w/U' OR u LIKE 'w/U/%' ) )
147
OR ( r = 1 AND a IN ( 1, 2, 3 ) AND ( u = 'w' ) );
151
# Bug #3738: we have a ref key
154
CREATE TABLE t1 (a VARCHAR(16), UNIQUE(a));
155
INSERT INTO t1 VALUES ('1'), ('2'), ('3');
156
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a = '2' LIMIT 0, 1;
161
# Bug #3845: group by, having and empty result
164
CREATE TABLE t1 (a INT);
165
INSERT INTO t1 VALUES (0), (0), (1), (2);
166
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a = 0 GROUP BY a HAVING a > 10;
171
# Bug #6089: queries which don't use any tables
176
SELECT SQL_CALC_FOUND_ROWS 'foo';
178
SELECT SQL_CALC_FOUND_ROWS 'foo' limit 0;
182
SELECT SQL_CALC_FOUND_ROWS 'foo' UNION SELECT 'bar' LIMIT 0;
186
# Bug #7945: group by + distinct with constant expression + limit
189
CREATE TABLE t1 (a int, b int);
190
INSERT INTO t1 VALUES (1,2), (1,3), (1,4), (1,5);
191
SELECT SQL_CALC_FOUND_ROWS DISTINCT 'a' FROM t1 GROUP BY b LIMIT 2;