summaryrefslogtreecommitdiff
path: root/mysql-test/main/cte_nonrecursive.result
blob: 5989abb7324799ddd2491c520d6d2a23ce155bc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
create table t1 (a int, b  varchar(32));
insert into t1 values
(4,'aaaa' ), (7,'bb'), (1,'ccc'), (4,'dd');
insert into t1 values
(3,'eee'), (7,'bb'), (1,'fff'), (4,'ggg');
create table t2 (c int);
insert into t2 values
(2), (4), (5), (3);
# select certain field in the specification of t
with t as (select a from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a;
c	a
4	4
3	3
4	4
select * from t2, (select a from t1 where b >= 'c') as t
where t2.c=t.a;
c	a
4	4
3	3
4	4
explain 
with t as (select a from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select * from t2, (select a from t1 where b >= 'c') as t
where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# select '*' in the specification of t
with t as (select * from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a;
c	a	b
4	4	dd
3	3	eee
4	4	ggg
select * from t2, (select * from t1 where b >= 'c') as t
where t2.c=t.a;
c	a	b
4	4	dd
3	3	eee
4	4	ggg
explain
with t as (select * from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select * from t2, (select * from t1 where b >= 'c') as t
where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# rename fields returned by the specication when defining t
with t(f1,f2) as (select * from t1 where b >= 'c') 
select * from t2,t where t2.c=t.f1;
c	f1	f2
4	4	dd
3	3	eee
4	4	ggg
explain
with t(f1,f2) as (select * from t1 where b >= 'c') 
select * from t2,t where t2.c=t.f1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# materialized query specifying t
with t as (select a, count(*) from t1 where b >= 'c' group by a) 
select * from t2,t where t2.c=t.a;
c	a	count(*)
4	4	2
3	3	1
select * from t2, (select a, count(*) from t1 where b >= 'c' group by a) as t 
where t2.c=t.a;
c	a	count(*)
4	4	2
3	3	1
explain
with t as (select a, count(*) from t1 where b >= 'c' group by a) 
select * from t2,t where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
explain
select * from t2, (select a, count(*) from t1 where b >= 'c' group by a) as t 
where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
# specivication of t contains having
with t as (select a, count(*) from t1 where b >= 'c'
             group by a having count(*)=1 ) 
select * from t2,t where t2.c=t.a;
c	a	count(*)
3	3	1
select * from t2, (select a, count(*) from t1 where b >= 'c'
                      group by a having count(*)=1) t
where t2.c=t.a;
c	a	count(*)
3	3	1
# main query contains having
with t as (select * from t2 where c <= 4) 
select a, count(*) from t1,t where t1.a=t.c group by a having count(*)=1;
a	count(*)
3	1
select a, count(*) from t1, (select * from t2 where c <= 4) t 
where t1.a=t.c group by a having count(*)=1;
a	count(*)
3	1
# main query contains group by + order by
with t as (select * from t2 where c <= 4 ) 
select a, count(*) from t1,t where t1.a=t.c group by a order by count(*);
a	count(*)
3	1
4	3
select a, count(*) from t1, (select * from t2 where c <= 4 ) t
where t1.a=t.c group by a order by count(*);
a	count(*)
3	1
4	3
# main query contains group by + order by + limit
with t as (select * from t2 where c <= 4 ) 
select a, count(*) from t1,t
where t1.a=t.c group by a order by count(*) desc limit 1;
a	count(*)
4	3
select a, count(*) from t1, (select * from t2 where c <= 4 ) t
where t1.a=t.c group by a order by count(*) desc limit 1;
a	count(*)
4	3
# t is used in a subquery
with t as (select a from t1 where a<5)
select * from t2 where c in (select a from t);
c
4
3
select * from t2 
where c in (select a from (select a from t1 where a<5) as t);
c
4
3
explain
with t as (select a from t1 where a<5)
select * from t2 where c in (select a from t);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; FirstMatch(t2); Using join buffer (flat, BNL join)
explain
select * from t2 
where c in (select a from (select a from t1 where a<5) as t);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; FirstMatch(t2); Using join buffer (flat, BNL join)
# materialized t is used in a subquery
with t as (select count(*) as c from t1 where b >= 'c' group by a)
select * from t2 where c in (select c from t);
c
2
select * from t2
where c in (select c from (select count(*) as c from t1
where b >= 'c' group by a) as t);
c
2
explain
with t as (select count(*) as c from t1 where b >= 'c' group by a)
select * from t2 where c in (select c from t);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived2>	ref	key0	key0	8	test.t2.c	1	Using where; FirstMatch(t2)
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
explain
select * from t2
where c in (select c from (select count(*) as c from t1
where b >= 'c' group by a) as t);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived3>	eq_ref	distinct_key	distinct_key	8	test.t2.c	1	Using where
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
# two references to t specified by a query
# selecting a field:  both in main query
with t as (select a from t1 where b >= 'c')
select * from t as r1, t as r2 where r1.a=r2.a;
a	a
1	1
1	1
4	4
4	4
3	3
1	1
1	1
4	4
4	4
select * from (select a from t1 where b >= 'c') as r1,
(select a from t1 where b >= 'c') as r2 
where r1.a=r2.a;
a	a
1	1
1	1
4	4
4	4
3	3
1	1
1	1
4	4
4	4
explain
with t as (select a from t1 where b >= 'c')
select * from t as r1, t as r2 where r1.a=r2.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select * from (select a from t1 where b >= 'c') as r1,
(select a from t1 where b >= 'c') as r2 
where r1.a=r2.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# two references to materialized t: both in main query
with t as (select distinct a from t1 where b >= 'c')
select * from t as r1, t as r2 where r1.a=r2.a;
a	a
1	1
4	4
3	3
select * from (select distinct a from t1 where b >= 'c') as r1,
(select distinct a from t1 where b >= 'c') as r2 
where r1.a=r2.a;
a	a
1	1
4	4
3	3
explain
with t as (select distinct a from t1 where b >= 'c')
select * from t as r1, t as r2 where r1.a=r2.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	8	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	r1.a	1	
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary
explain
select * from (select distinct a from t1 where b >= 'c') as r1,
(select distinct a from t1 where b >= 'c') as r2 
where r1.a=r2.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	8	Using where
1	PRIMARY	<derived3>	ref	key0	key0	5	r1.a	1	
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary
# two references to t specified by a query
# selecting all fields:  both in main query
with t as (select * from t1 where b >= 'c')
select * from t as r1, t as r2 where r1.a=r2.a;
a	b	a	b
1	ccc	1	ccc
1	fff	1	ccc
4	dd	4	dd
4	ggg	4	dd
3	eee	3	eee
1	ccc	1	fff
1	fff	1	fff
4	dd	4	ggg
4	ggg	4	ggg
select * from (select * from t1 where b >= 'c') as r1,
(select * from t1 where b >= 'c') as r2
where r1.a=r2.a;
a	b	a	b
1	ccc	1	ccc
1	fff	1	ccc
4	dd	4	dd
4	ggg	4	dd
3	eee	3	eee
1	ccc	1	fff
1	fff	1	fff
4	dd	4	ggg
4	ggg	4	ggg
explain
with t as (select * from t1 where b >= 'c')
select * from t as r1, t as r2 where r1.a=r2.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select * from (select * from t1 where b >= 'c') as r1,
(select * from t1 where b >= 'c') as r2
where r1.a=r2.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# two references to t specifying explicitly column names
with t(c) as (select a from t1 where b >= 'c')
select * from t r1, t r2 where r1.c=r2.c;
c	c
1	1
1	1
4	4
4	4
3	3
1	1
1	1
4	4
4	4
# t two references of t used in different parts of a union
with t as (select a from t1 where b >= 'c')
select * from t where a < 2
union
select * from t where a >= 4;
a
1
4
select * from (select a from t1 where b >= 'c') as t
where t.a < 2
union
select * from (select a from t1 where b >= 'c') as t
where t.a >= 4;
a
1
4
explain
with t as (select a from t1 where b >= 'c')
select * from t where a < 2
union
select * from t where a >= 4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain
select * from (select a from t1 where b >= 'c') as t
where t.a < 2
union
select * from (select a from t1 where b >= 'c') as t
where t.a >= 4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
3	UNION	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
NULL	UNION RESULT	<union1,3>	ALL	NULL	NULL	NULL	NULL	NULL	
# specification of t contains union
with t as (select a from t1 where b >= 'f' 
union
select c as a from t2 where c < 4) 
select * from t2,t where t2.c=t.a;
c	a
2	2
4	4
3	3
select * from t2, 
(select a from t1 where b >= 'f' 
union
select c as a from t2 where c < 4) as t 
where t2.c=t.a;
c	a
2	2
4	4
3	3
explain
with t as (select a from t1 where b >= 'f' 
union
select c as a from t2 where c < 4) 
select * from t2,t where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived2>	eq_ref	distinct_key	distinct_key	5	test.t2.c	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
3	UNION	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
explain
select * from t2, 
(select a from t1 where b >= 'f' 
union
select c as a from t2 where c < 4) as t 
where t2.c=t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived2>	eq_ref	distinct_key	distinct_key	5	test.t2.c	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
3	UNION	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
NULL	UNION RESULT	<union2,3>	ALL	NULL	NULL	NULL	NULL	NULL	
# t is defined in the with clause of a subquery
select t1.a,t1.b from t1,t2
where t1.a>t2.c and
t2.c in (with t as (select * from t1 where t1.a<5)
select t2.c from t2,t where t2.c=t.a);
a	b
4	aaaa
7	bb
7	bb
4	dd
7	bb
7	bb
4	ggg
select t1.a,t1.b from t1,t2
where t1.a>t2.c and
t2.c in (select t2.c
from t2,(select * from t1 where t1.a<5) as t
where t2.c=t.a);
a	b
4	aaaa
7	bb
7	bb
4	dd
7	bb
7	bb
4	ggg
explain
select t1.a,t1.b from t1,t2
where t1.a>t2.c and
t2.c in (with t as (select * from t1 where t1.a<5)
select t2.c from t2,t where t2.c=t.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	
1	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
3	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select t1.a,t1.b from t1,t2
where t1.a>t2.c and
t2.c in (select t2.c
from t2,(select * from t1 where t1.a<5) as t
where t2.c=t.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	
1	PRIMARY	<subquery2>	eq_ref	distinct_key	distinct_key	4	func	1	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
2	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
2	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# two different definitions of t: one in the with clause of the main query,
# the other in the with clause of a subquery 
with t as (select c from t2 where c >= 4)
select t1.a,t1.b from t1,t
where t1.a=t.c and
t.c in (with t as (select * from t1 where t1.a<5)
select t2.c from t2,t where t2.c=t.a);
a	b
4	aaaa
4	dd
4	ggg
select t1.a,t1.b from t1, (select c from t2 where c >= 4) as t
where t1.a=t.c and
t.c in (select t2.c from t2,  (select * from t1 where t1.a<5) as t
where t2.c=t.a);
a	b
4	aaaa
4	dd
4	ggg
explain
with t as (select c from t2 where c >= 4)
select t1.a,t1.b from t1,t
where t1.a=t.c and
t.c in (with t as (select * from t1 where t1.a<5)
select t2.c from t2,t where t2.c=t.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<subquery4>	eq_ref	distinct_key	distinct_key	4	func	1	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
4	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
4	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select t1.a,t1.b from t1, (select c from t2 where c >= 4) as t
where t1.a=t.c and
t.c in (select t2.c from t2,  (select * from t1 where t1.a<5) as t
where t2.c=t.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	4	func	1	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
3	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# another with table tt is defined in the with clause of a subquery
# from the specification of t
with t as (select * from t1
where a>2 and
b in (with tt as (select * from t2 where t2.c<5)
select t1.b from t1,tt where t1.a=tt.c))
select t.a, count(*) from t1,t where t1.a=t.a  group by t.a;
a	count(*)
3	1
4	9
select t.a, count(*)
from t1,
(select * from t1
where a>2 and
b in (select t1.b
from t1,
(select * from t2 where t2.c<5) as tt
where t1.a=tt.c)) as t
where t1.a=t.a  group by t.a;
a	count(*)
3	1
4	9
explain
with t as (select * from t1
where a>2 and
b in (with tt as (select * from t2 where t2.c<5)
select t1.b from t1,tt where t1.a=tt.c))
select t.a, count(*) from t1,t where t1.a=t.a  group by t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
1	PRIMARY	<subquery4>	eq_ref	distinct_key	distinct_key	35	func	1	
4	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
4	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain
select t.a, count(*)
from t1,
(select * from t1
where a>2 and
b in (select t1.b
from t1,
(select * from t2 where t2.c<5) as tt
where t1.a=tt.c)) as t
where t1.a=t.a  group by t.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
1	PRIMARY	<subquery3>	eq_ref	distinct_key	distinct_key	35	func	1	
3	MATERIALIZED	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
3	MATERIALIZED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# with clause in the specification of a derived table
select *
from t1, 
(with t as (select a from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a) as tt
where t1.b > 'f' and tt.a=t1.a;
a	b	c	a
4	ggg	4	4
4	ggg	4	4
select *
from t1, 
(select * from t2,
(select a from t1 where b >= 'c') as t
where t2.c=t.a) as tt
where t1.b > 'f' and tt.a=t1.a;
a	b	c	a
4	ggg	4	4
4	ggg	4	4
explain
select *
from t1, 
(with t as (select a from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a) as tt
where t1.b > 'f' and tt.a=t1.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (incremental, BNL join)
explain
select *
from t1, 
(select * from t2,
(select a from t1 where b >= 'c') as t
where t2.c=t.a) as tt
where t1.b > 'f' and tt.a=t1.a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (incremental, BNL join)
# with claused in the specification of a view
create view v1 as 
with t as (select a from t1 where b >= 'c') 
select * from t2,t where t2.c=t.a;
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with t as (select `t1`.`a` AS `a` from `t1` where `t1`.`b` >= 'c')select `t2`.`c` AS `c`,`t`.`a` AS `a` from (`t2` join `t`) where `t2`.`c` = `t`.`a`	latin1	latin1_swedish_ci
select * from v1;
c	a
4	4
3	3
4	4
explain
select * from v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	4	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# with claused in the specification of a materialized view
create view v2 as 
with t as (select a, count(*) from t1 where b >= 'c' group by a) 
select * from t2,t where t2.c=t.a;
show create view v2;
View	Create View	character_set_client	collation_connection
v2	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with t as (select `t1`.`a` AS `a`,count(0) AS `count(*)` from `t1` where `t1`.`b` >= 'c' group by `t1`.`a`)select `t2`.`c` AS `c`,`t`.`a` AS `a`,`t`.`count(*)` AS `count(*)` from (`t2` join `t`) where `t2`.`c` = `t`.`a`	latin1	latin1_swedish_ci
select * from v2;
c	a	count(*)
4	4	2
3	3	1
explain
select * from v2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	Using where
1	PRIMARY	<derived2>	ref	key0	key0	5	test.t2.c	1	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort
# with clause in the specification of a view that whose definition
# table alias for a with table
create view v3 as
with t(c) as (select a from t1 where b >= 'c')
select * from t r1 where r1.c=4;
show create view v3;
View	Create View	character_set_client	collation_connection
v3	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v3` AS with t(`c`) as (select `t1`.`a` AS `c` from `t1` where `t1`.`b` >= 'c')select `r1`.`c` AS `c` from `t` `r1` where `r1`.`c` = 4	latin1	latin1_swedish_ci
select * from v3;
c
4
4
# with clause in the specification of a view that whose definition
# two table aliases for for the same with table
create view v4(c,d) as
with t(c) as (select a from t1 where b >= 'c')
select * from t r1, t r2 where r1.c=r2.c and r2.c=4;
show create view v4;
View	Create View	character_set_client	collation_connection
v4	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS with t(`c`) as (select `t1`.`a` AS `c` from `t1` where `t1`.`b` >= 'c')select `r1`.`c` AS `c`,`r2`.`c` AS `d` from (`t` `r1` join `t` `r2`) where `r1`.`c` = `r2`.`c` and `r2`.`c` = 4	latin1	latin1_swedish_ci
select * from v4;
c	d
4	4
4	4
4	4
4	4
explain
select * from v4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
drop view v1,v2,v3,v4;
# currently any views containing with clause are not updatable
create view v1(a) as 
with t as (select a from t1 where b >= 'c') 
select t.a from t2,t where t2.c=t.a;
update v1 set a=0 where a > 4;
ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
# prepare of a query containing a definition of a with table t
prepare stmt1 from "
with t as (select a from t1 where b >= 'c') 
  select * from t2,t where t2.c=t.a;
";
execute stmt1;
c	a
4	4
3	3
4	4
execute stmt1;
c	a
4	4
3	3
4	4
deallocate prepare stmt1;
# prepare of a query containing a definition of a materialized t
prepare stmt1 from "
with t as (select a, count(*) from t1 where b >= 'c' group by a) 
 select * from t2,t where t2.c=t.a;
";
execute stmt1;
c	a	count(*)
4	4	2
3	3	1
execute stmt1;
c	a	count(*)
4	4	2
3	3	1
deallocate prepare stmt1;
# prepare of a query containing two references to with table t
prepare stmt1 from "
with t as (select * from t1 where b >= 'c')
  select * from t as r1, t as r2 where r1.a=r2.a;
";
execute stmt1;
a	b	a	b
1	ccc	1	ccc
1	fff	1	ccc
4	dd	4	dd
4	ggg	4	dd
3	eee	3	eee
1	ccc	1	fff
1	fff	1	fff
4	dd	4	ggg
4	ggg	4	ggg
execute stmt1;
a	b	a	b
1	ccc	1	ccc
1	fff	1	ccc
4	dd	4	dd
4	ggg	4	dd
3	eee	3	eee
1	ccc	1	fff
1	fff	1	fff
4	dd	4	ggg
4	ggg	4	ggg
deallocate prepare stmt1;
with t(f) as (select * from t1 where b >= 'c') 
select * from t2,t where t2.c=t.f1;
ERROR HY000: WITH column list and SELECT field list have different column counts
with t(f1,f1) as (select * from t1 where b >= 'c') 
select * from t2,t where t2.c=t.f1;
ERROR 42S21: Duplicate column name 'f1'
with t as (select * from t2 where c>3),
t as (select a from t1 where a>2)
select * from t,t1 where t1.a=t.c;
ERROR HY000: Duplicate query name `t` in WITH clause
with t as (select a from s where a<5),
s as (select a from t1 where b>='d')
select * from t,s where t.a=s.a;
ERROR 42S02: Table 'test.s' doesn't exist
with recursive
t as (select a from s where a<5),
s as (select a from t1 where b>='d')
select * from t,s where t.a=s.a;
a	a
4	4
4	4
3	3
1	1
4	4
4	4
with recursive t as (select * from s where a>2),
s as (select a from t1,r where t1.a>r.c), 
r as (select c from t,t2 where t.a=t2.c) 
select * from r where r.c<7;
ERROR HY000: No anchors for recursive WITH element 't'
with recursive
t as (select * from s where a>2),
s as (select a from t1,r where t1.a>r.c), 
r as (select c from t,t2 where t.a=t2.c) 
select * from r where r.c<7;
ERROR HY000: No anchors for recursive WITH element 't'
with recursive
t as (select * from t1
where a in (select c from s where b<='ccc') and  b>'b'),
s as (select * from t1,t2
where t1.a=t2.c and t1.c in (select a from t where a<5))
select * from s where s.b>'aaa';
ERROR HY000: No anchors for recursive WITH element 't'
with recursive 
t as (select * from t1 where b>'aaa' and b <='d') 
select t.b from t,t2
where t.a=t2.c and
t2.c in (with recursive
s as (select t1.a from s,t1 where t1.a=s.a and t1.b<'c')
select * from s);
ERROR HY000: No anchors for recursive WITH element 's'
#erroneous definition of unreferenced with table t
with t as (select count(*) from t1 where d>='f' group by a)
select t1.b from t2,t1 where t1.a = t2.c;
ERROR 42S22: Unknown column 'd' in 'where clause'
with t as (select count(*) from t1 where b>='f' group by a)
select t1.b from t2,t1 where t1.a = t2.c;
b
aaaa
dd
eee
ggg
#erroneous definition of s referring to unreferenced t
with t(d) as (select count(*) from t1 where b<='ccc' group by b),
s as (select * from t1 where a in (select t2.d from t2,t where t2.c=t.d))
select t1.b from t1,t2 where t1.a=t2.c;
ERROR 42S22: Unknown column 't2.d' in 'field list'
with t(d) as (select count(*) from t1 where b<='ccc' group by b),
s as (select * from t1 where a in (select t2.c from t2,t where t2.c=t.c))
select t1.b from t1,t2 where t1.a=t2.c;
ERROR 42S22: Unknown column 't.c' in 'where clause'
with t(d) as (select count(*) from t1 where b<='ccc' group by b),
s as (select * from t1 where a in (select t2.c from t2,t where t2.c=t.d))
select t1.b from t1,t2 where t1.a=t2.c;
b
aaaa
dd
eee
ggg
#erroneous definition of unreferenced with table t
with t(f) as (select * from t1 where b >= 'c') 
select t1.b from t2,t1 where t1.a = t2.c;
ERROR HY000: WITH column list and SELECT field list have different column counts
#erroneous definition of unreferenced with table t
with t(f1,f1) as (select * from t1 where b >= 'c') 
select t1.b from t2,t1 where t1.a = t2.c;
ERROR 42S21: Duplicate column name 'f1'
# explain for query with unreferenced with table
explain 
with t as (select a from t1 where b >= 'c') 
select t1.b from t2,t1 where t1.a = t2.c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
explain 
with t as (select a, count(*) from t1 where b >= 'c' group by a)
select t1.b from t2,t1 where t1.a = t2.c;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	4	
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	8	Using where; Using join buffer (flat, BNL join)
# too many with elements in with clause
with s65 as (select * from t1), s64 as (select * from t1) , s63 as (select * from t1) , s62 as (select * from t1) , s61 as (select * from t1) , s60 as (select * from t1) , s59 as (select * from t1) , s58 as (select * from t1) , s57 as (select * from t1) , s56 as (select * from t1) , s55 as (select * from t1) , s54 as (select * from t1) , s53 as (select * from t1) , s52 as (select * from t1) , s51 as (select * from t1) , s50 as (select * from t1) , s49 as (select * from t1) , s48 as (select * from t1) , s47 as (select * from t1) , s46 as (select * from t1) , s45 as (select * from t1) , s44 as (select * from t1) , s43 as (select * from t1) , s42 as (select * from t1) , s41 as (select * from t1) , s40 as (select * from t1) , s39 as (select * from t1) , s38 as (select * from t1) , s37 as (select * from t1) , s36 as (select * from t1) , s35 as (select * from t1) , s34 as (select * from t1) , s33 as (select * from t1) , s32 as (select * from t1) , s31 as (select * from t1) , s30 as (select * from t1) , s29 as (select * from t1) , s28 as (select * from t1) , s27 as (select * from t1) , s26 as (select * from t1) , s25 as (select * from t1) , s24 as (select * from t1) , s23 as (select * from t1) , s22 as (select * from t1) , s21 as (select * from t1) , s20 as (select * from t1) , s19 as (select * from t1) , s18 as (select * from t1) , s17 as (select * from t1) , s16 as (select * from t1) , s15 as (select * from t1) , s14 as (select * from t1) , s13 as (select * from t1) , s12 as (select * from t1) , s11 as (select * from t1) , s10 as (select * from t1) , s9 as (select * from t1) , s8 as (select * from t1) , s7 as (select * from t1) , s6 as (select * from t1) , s5 as (select * from t1) , s4 as (select * from t1) , s3 as (select * from t1) , s2 as (select * from t1) , s1 as (select * from t1)  select * from s65;
ERROR HY000: Too many WITH elements in WITH clause
drop table t1,t2;
#
# Bug mdev-9937: View used in the specification of with table 
#                refers to the base table with the same name 
#
create table t1 (a int);
insert into t1 values (20), (30), (10);
create view v1 as select * from t1 where a > 10;
with t1 as (select * from v1) select * from t1;
a
20
30
drop view v1;
drop table t1;
#
# Bug mdev-10058: Invalid derived table with WITH clause  
#                 
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
CREATE TABLE t3 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
INSERT INTO t2 VALUES (1),(2),(3);
INSERT INTO t3 VALUES (1),(2),(3);
SELECT * FROM (WITH a AS (SELECT * FROM t1) (t2 NATURAL JOIN t3));
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't2 NATURAL JOIN t3))' at line 1
SELECT * FROM (WITH a AS (SELECT * FROM t1) SELECT * FROM t2 NATURAL JOIN t3) AS d1;
a
1
2
3
DROP TABLE t1,t2,t3;
#
# Bug mdev-10344: the WITH clause of the query refers to a view that uses 
#     a base table with the same name as a  CTE table from the clause 
#
create table ten(a int primary key);
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table one_k(a int primary key);
insert into one_k select A.a + B.a* 10 + C.a * 100 from ten A, ten B, ten C;
create view v1 as select * from ten;
select * from v1;
a
0
1
2
3
4
5
6
7
8
9
drop view v1;
drop table ten, one_k;
#
# MDEV-10057 : Crash with EXPLAIN + WITH + constant query
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
SELECT * FROM (WITH a AS (SELECT * FROM t1) SELECT 1) AS t1;
1
1
EXPLAIN SELECT * FROM (WITH a AS (SELECT * FROM t1) SELECT 1) AS t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived3>	system	NULL	NULL	NULL	NULL	1	
3	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
DROP TABLE t1;
#
# MDEV-10058: Suspicious EXPLAIN output for a derived table + WITH + joined table	
#
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT);
CREATE TABLE t3 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
INSERT INTO t2 VALUES (1),(2),(3);
INSERT INTO t3 VALUES (1),(2),(3);
EXPLAIN SELECT * FROM (WITH a AS (SELECT * FROM t1) (t2 NATURAL JOIN t3));
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't2 NATURAL JOIN t3))' at line 1
explain SELECT * FROM (WITH a AS (SELECT * FROM t1) SELECT * FROM t2 NATURAL JOIN t3) AS d1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	3	
1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
DROP TABLE t1,t2,t3;
#
# MDEV-10729: Server crashes in st_select_lex::set_explain_type
#
CREATE TABLE t1 (i1 INT, KEY(i1)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4),(8);
CREATE TABLE t2 (a2 INT, b2 INT, KEY(b2)) ENGINE=MyISAM;
INSERT INTO t2 VALUES (8,7);
CREATE TABLE t3 (i3 INT) ENGINE=MyISAM;
INSERT INTO t3 VALUES (2),(6);
SELECT * FROM t1, t2 WHERE a2 = i1 and b2 >= i1 AND i1 IN ( SELECT i3 FROM t3 )
UNION
SELECT * FROM t1, t2 WHERE a2 = i1 and b2 >= i1 AND i1 IN ( SELECT i3 FROM t3 )
;
i1	a2	b2
DROP TABLE t1,t2,t3;
#
# MDEV-10923: mergeable CTE used twice in the query
#
create table employees (
name varchar(32),
dept varchar(32),
country varchar(8)
);
insert into employees 
values 
('Sergei Golubchik', 'Development', 'DE'),
('Claudio Nanni', 'Support', 'ES'),
('Sergei Petrunia', 'Development', 'RU');
with eng as 
(
select * from employees
where dept in ('Development','Support')
),
eu_eng  as 
(
select * from eng where country IN ('DE','ES','RU')
)
select * from eu_eng T1 
where 
not exists (select 1 from eu_eng T2 
where T2.country=T1.country
and T2.name <> T1.name);
name	dept	country
Sergei Golubchik	Development	DE
Claudio Nanni	Support	ES
Sergei Petrunia	Development	RU
drop table employees;
#
# MDEV-11818: EXPLAIN EXTENDED for a query with optimized away CTE table
#
CREATE TABLE t1 (i INT, c VARCHAR(3));
INSERT INTO t1 VALUES (1,'foo');
EXPLAIN EXTENDED
WITH cte AS ( SELECT * FROM t1 ) SELECT i FROM cte;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	system	NULL	NULL	NULL	NULL	1	100.00	
Warnings:
Note	1003	with cte as (select `test`.`t1`.`i` AS `i`,`test`.`t1`.`c` AS `c` from `test`.`t1`)select 1 AS `i` from dual
DROP TABLE t1;
#
# MDEV-12185: view defintion contains WITH clause with 
#             several specifications of CTE
#
with
alias1 as (select 1 as one),
alias2 as (select 2 as two)
select one, two from alias1, alias2;
one	two
1	2
create view v1 as 
with
alias1 as (select 1 as one),
alias2 as (select 2 as two)
select one, two from alias1, alias2;
select * from v1;
one	two
1	2
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with alias1 as (select 1 AS `one`), alias2 as (select 2 AS `two`)select `alias1`.`one` AS `one`,`alias2`.`two` AS `two` from (`alias1` join `alias2`)	latin1	latin1_swedish_ci
drop view v1;
#
# MDEV-12440: the same CTE table is used twice
#
create table t1 (a int, b  varchar(32));
insert into t1 values
(4,'aaaa' ), (7,'bb'), (1,'ccc'), (4,'dd');
# cte2 is used in the main query and in the spec for ct3
with
cte1 as (select * from t1 where  b >= 'c'),
cte2 as (select * from cte1 where a < 7),
cte3 as (select * from cte2 where a > 1)
select * from cte2, cte3 where cte2.a = cte3.a;
a	b	a	b
4	dd	4	dd
# cte2 is used twice in the spec for ct3
with
cte1 as (select * from t1 where  b >= 'b'),
cte2 as (select * from cte1 where b > 'c'),
cte3 as (select * from cte2 where a > 1 union select * from cte2 where a > 1)
select * from cte3;
a	b
4	dd
drop table t1;
#
# MDEV-12558: CTE with the same name as temporary table
#
CREATE TABLE t ENGINE=MyISAM AS SELECT 1 AS i;
CREATE TEMPORARY TABLE cte ENGINE=MyISAM AS SELECT 2 AS f;
WITH cte AS ( SELECT i FROM t ) SELECT * FROM cte;
i
1
WITH cte AS ( SELECT i FROM t GROUP BY i) SELECT * FROM cte;
i
1
SELECT * FROM cte;
f
2
DROP TABLE cte;
DROP TABLE t;
#
# MDEV-13107: SHOW TABLE STATUS, SHOW CREATE VIEW 
#             for CTEs that use derived tables
#
create table t1(a int) engine=myisam;
insert into t1 values (3), (1), (2);
create table t2 (b int) engine=myisam;
insert into t2 values (2), (10);
create view v1 as
with t as (select s.a from (select t1.a from t1) s),
r as(select t.a from t2, t where t2.b=t.a)
select a from r;
create view v2 as
with t as (select s.a from (select t1.a from t1) s),
r as(select t.a from t2, t where t2.b=t.a)
select a from t1;
show table status;
show create view v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with t as (select `s`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `s`), r as (select `t`.`a` AS `a` from (`t2` join `t`) where `t2`.`b` = `t`.`a`)select `r`.`a` AS `a` from `r`	latin1	latin1_swedish_ci
show create view v2;
View	Create View	character_set_client	collation_connection
v2	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with t as (select `s`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `s`), r as (select `t`.`a` AS `a` from (`t2` join `t`) where `t2`.`b` = `t`.`a`)select `t1`.`a` AS `a` from `t1`	latin1	latin1_swedish_ci
select * from v1;
a
2
select * from v2;
a
3
1
2
prepare stmt1 from "select * from v1";
execute stmt1;
a
2
execute stmt1;
a
2
prepare stmt2 from "select * from v2";
execute stmt2;
a
3
1
2
execute stmt2;
a
3
1
2
deallocate prepare stmt1;
deallocate prepare stmt2;
drop view v1,v2;
drop table t1,t2;
#
# MDEV-13796: UNION of two materialized CTEs
#
CREATE TABLE t1 (id int, k int);
CREATE TABLE t2 (id int);
INSERT INTO t1 VALUES (3,5), (1,7), (4,3);
INSERT INTO t2 VALUES (4), (3), (2);
WITH d1 AS (SELECT SUM(k) FROM t1, t2 as t2 WHERE t1.id = t2.id),
d2 AS (SELECT SUM(k) FROM t1, t2 as t2 WHERE t1.id = t2.id)
SELECT * FROM d1 UNION  SELECT * FROM d2;
SUM(k)
8
explain WITH d1 AS (SELECT SUM(k) FROM t1, t2 as t2 WHERE t1.id = t2.id),
d2 AS (SELECT SUM(k) FROM t1, t2 as t2 WHERE t1.id = t2.id)
SELECT * FROM d1 UNION  SELECT * FROM d2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	9	
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	
2	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
4	UNION	<derived3>	ALL	NULL	NULL	NULL	NULL	9	
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	3	
3	DERIVED	t2	ALL	NULL	NULL	NULL	NULL	3	Using where; Using join buffer (flat, BNL join)
NULL	UNION RESULT	<union1,4>	ALL	NULL	NULL	NULL	NULL	NULL	
DROP TABLE t1,t2;
#
# MDEV-13780: tower of embedding CTEs with multiple usage of them
#
create table t1 (a int);
insert into t1 values (3), (2), (4), (7), (1), (2), (5);
with cte_e as
(
with cte_o as
(
with cte_i as (select * from t1 where a < 7)
select * from cte_i where a > 1
)
select * from cte_o as cto_o1 where a < 3
union
select * from cte_o as cto_o2 where a > 4
)
select * from cte_e as cte_e1 where a > 1
union
select * from cte_e as cte_e2;
a
2
5
explain extended with cte_e as
(
with cte_o as
(
with cte_i as (select * from t1 where a < 7)
select * from cte_i where a > 1
)
select * from cte_o as cto_o1 where a < 3
union
select * from cte_o as cto_o2 where a > 4
)
select * from cte_e as cte_e1 where a > 1
union
select * from cte_e as cte_e2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	PRIMARY	<derived4>	ALL	NULL	NULL	NULL	NULL	14	100.00	Using where
4	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
5	UNION	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
NULL	UNION RESULT	<union4,5>	ALL	NULL	NULL	NULL	NULL	NULL	NULL	
6	UNION	<derived11>	ALL	NULL	NULL	NULL	NULL	14	100.00	
11	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
12	UNION	t1	ALL	NULL	NULL	NULL	NULL	7	100.00	Using where
NULL	UNION RESULT	<union11,12>	ALL	NULL	NULL	NULL	NULL	NULL	NULL	
NULL	UNION RESULT	<union1,6>	ALL	NULL	NULL	NULL	NULL	NULL	NULL	
Warnings:
Note	1003	with cte_e as (with cte_o as (with cte_i as (/* select#2 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)/* select#3 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)/* select#4 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1 union /* select#5 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1)/* select#1 */ select `cte_e1`.`a` AS `a` from `cte_e` `cte_e1` where `cte_e1`.`a` > 1 union /* select#6 */ select `cte_e2`.`a` AS `a` from `cte_e` `cte_e2`
drop table t1;
#
# MDEV-13753: embedded CTE in a VIEW created in prepared statement
#
SET @sql_query = "
  CREATE OR REPLACE VIEW cte_test AS
   WITH  cte1  AS ( SELECT 1 as a from dual )
    ,    cte2  AS ( SELECT * FROM cte1 )
    SELECT * FROM cte2;
";
PREPARE stmt FROM @sql_query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SHOW CREATE VIEW cte_test;
View	Create View	character_set_client	collation_connection
cte_test	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `cte_test` AS with cte1 as (select 1 AS `a`), cte2 as (select `cte1`.`a` AS `a` from `cte1`)select `cte2`.`a` AS `a` from `cte2`	latin1	latin1_swedish_ci
SELECT * FROM cte_test;
a
1
DROP VIEW cte_test;
#
# mdev-14755 : PS for query using CTE in select with subquery
#
create table t1 (a int);
insert into t1 values
(7), (2), (8), (1), (3), (2), (7), (5), (4), (7), (9), (8);
with cte as
(select a from t1 where a between 4 and 7 group by a)
(select a from cte where exists( select a from t1 where cte.a=t1.a ))
union
(select a from t1 where a < 2);
a
1
4
5
7
prepare stmt from "with cte as
(select a from t1 where a between 4 and 7 group by a)
(select a from cte where exists( select a from t1 where cte.a=t1.a ))
union
(select a from t1 where a < 2)";
execute stmt;
a
1
4
5
7
execute stmt;
a
1
4
5
7
deallocate prepare stmt;
with cte as
(select a from t1 where a between 4 and 7 group by a)
(select a from t1 where a < 2)
union
(select a from cte where exists( select a from t1 where cte.a=t1.a ));
a
1
4
5
7
prepare stmt from "with cte as
(select a from t1 where a between 4 and 7 group by a)
(select a from t1 where a < 2)
union
(select a from cte where exists( select a from t1 where cte.a=t1.a ))";
execute stmt;
a
1
4
5
7
execute stmt;
a
1
4
5
7
deallocate prepare stmt;
with cte as
(select a from t1 where a between 4 and 7)
(select a from t1 where a < 2)
union
(select a from cte where exists( select a from t1 where cte.a=t1.a ));
a
1
4
5
7
prepare stmt from "with cte as
(select a from t1 where a between 4 and 7)
(select a from t1 where a < 2)
union
(select a from cte where exists( select a from t1 where cte.a=t1.a ))";
execute stmt;
a
1
4
5
7
execute stmt;
a
1
4
5
7
deallocate prepare stmt;
with cte as
(select a from t1 where a between 4 and 7)
(select a from cte
where exists( select a from t1 where t1.a < 2 and cte.a=t1.a ))
union
(select a from cte where exists( select a from t1 where cte.a=t1.a ));
a
4
5
7
prepare stmt from "with cte as
(select a from t1 where a between 4 and 7)
(select a from cte
where exists( select a from t1 where t1.a < 2 and cte.a=t1.a ))
union
(select a from cte where exists( select a from t1 where cte.a=t1.a ))";
execute stmt;
a
4
5
7
execute stmt;
a
4
5
7
deallocate prepare stmt;
drop table t1;
#
# MDEV-14852: CTE using temporary table in query
#             with two references to the CTE
#
create temporary table t1 (i int);
insert into t1 values (5),(4),(1),(2),(3);
with
c1 as (select i from t1),
c2 as (select i from c1 where c1.i=2)
select i from c1 where i > 3 union  select i from c2;
i
5
4
2
drop table t1;
create table t1 (term char(10));
create temporary table t2 (term char(10));
insert into t1 values ('TERM01'),('TERM02'),('TERM03');
insert into t2 values ('TERM02'),('TERM03'),('TERM04');
with c1 as (select * from t1), c2 as (select * from t2)
(select * from c1 left outer join c2 on c1.term = c2.term)
union all
(select * from c1 right outer join c2 on c1.term = c2.term
where c1.term is null);
term	term
TERM02	TERM02
TERM03	TERM03
TERM01	NULL
NULL	TERM04
drop table t1,t2;
#
# MDEV-14969: view using subquery with attached CTE
#
create table region (
r_regionkey int,
r_name char(25),
primary key (r_regionkey)
);
insert into region values
(0,'AFRICA'), (1,'AMERICA'), (2,'ASIA'), (3,'EUROPE'), (4,'MIDDLE EAST');
create table nation (
n_nationkey int,
n_name char(25),
n_regionkey int,
primary key (n_nationkey),
key i_n_regionkey (n_regionkey)
);
insert into nation values
(0,'ALGERIA',0), (1,'ARGENTINA',1), (2,'BRAZIL',1), (3,'CANADA',1),
(4,'EGYPT',4), (5,'ETHIOPIA',0), (6,'FRANCE',3), (7,'GERMANY',3),
(8,'INDIA',2), (9,'INDONESIA',2), (10,'IRAN',4), (11,'IRAQ',4),
(12,'JAPAN',2), (13,'JORDAN',4), (14,'KENYA',0), (15,'MOROCCO',0),
(16,'MOZAMBIQUE',0), (17,'PERU',1), (18,'CHINA',2), (19,'ROMANIA',3),
(20,'SAUDI ARABIA',4), (21,'VIETNAM',2), (22,'RUSSIA',3),
(23,'UNITED KINGDOM',3), (24,'UNITED STATES',1);
select * from nation n ,region r
where n.n_regionkey = r.r_regionkey and
r.r_regionkey in
(with t as (select * from region where r_regionkey <= 3 )
select r_regionkey from t where r_name <> "ASIA");
n_nationkey	n_name	n_regionkey	r_regionkey	r_name
0	ALGERIA	0	0	AFRICA
5	ETHIOPIA	0	0	AFRICA
14	KENYA	0	0	AFRICA
15	MOROCCO	0	0	AFRICA
16	MOZAMBIQUE	0	0	AFRICA
1	ARGENTINA	1	1	AMERICA
2	BRAZIL	1	1	AMERICA
3	CANADA	1	1	AMERICA
17	PERU	1	1	AMERICA
24	UNITED STATES	1	1	AMERICA
6	FRANCE	3	3	EUROPE
7	GERMANY	3	3	EUROPE
19	ROMANIA	3	3	EUROPE
22	RUSSIA	3	3	EUROPE
23	UNITED KINGDOM	3	3	EUROPE
create view v as
select * from nation n ,region r
where n.n_regionkey = r.r_regionkey and
r.r_regionkey in
(with t as (select * from region where r_regionkey <= 3)
select r_regionkey from t where r_name <> "ASIA");
show create view v;
View	Create View	character_set_client	collation_connection
v	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `n`.`n_nationkey` AS `n_nationkey`,`n`.`n_name` AS `n_name`,`n`.`n_regionkey` AS `n_regionkey`,`r`.`r_regionkey` AS `r_regionkey`,`r`.`r_name` AS `r_name` from (`nation` `n` join `region` `r`) where `n`.`n_regionkey` = `r`.`r_regionkey` and `r`.`r_regionkey` in (with t as (select `region`.`r_regionkey` AS `r_regionkey`,`region`.`r_name` AS `r_name` from `region` where `region`.`r_regionkey` <= 3)select `t`.`r_regionkey` from `t` where `t`.`r_name` <> 'ASIA')	latin1	latin1_swedish_ci
select * from v;
n_nationkey	n_name	n_regionkey	r_regionkey	r_name
0	ALGERIA	0	0	AFRICA
5	ETHIOPIA	0	0	AFRICA
14	KENYA	0	0	AFRICA
15	MOROCCO	0	0	AFRICA
16	MOZAMBIQUE	0	0	AFRICA
1	ARGENTINA	1	1	AMERICA
2	BRAZIL	1	1	AMERICA
3	CANADA	1	1	AMERICA
17	PERU	1	1	AMERICA
24	UNITED STATES	1	1	AMERICA
6	FRANCE	3	3	EUROPE
7	GERMANY	3	3	EUROPE
19	ROMANIA	3	3	EUROPE
22	RUSSIA	3	3	EUROPE
23	UNITED KINGDOM	3	3	EUROPE
drop view v;
drop table region, nation;
#
# MDEV-15120: cte name used with database name
#
WITH cte AS (SELECT 1 AS a) SELECT test.cte.a FROM test.cte;
ERROR 42S02: Table 'test.cte' doesn't exist
CREATE DATABASE db1;
USE db1;
WITH cte AS (SELECT 1 AS a) SELECT db1.cte.a FROM db1.cte;
ERROR 42S02: Table 'db1.cte' doesn't exist
DROP DATABASE db1;
USE test;
#
# MDEV-15119: CTE c2 specified after CTE c1 and is used in
#             CTE c3 that is embedded into the spec of c1
#
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES (1),(2),(3);
WITH c1 AS (WITH c3 AS (SELECT * FROM c2) SELECT * FROM c3),
c2 AS (SELECT * FROM t1)
SELECT * FROM c1;
ERROR 42S02: Table 'test.c2' doesn't exist
WITH RECURSIVE c1 AS (WITH c3 AS (SELECT * FROM c2) SELECT * FROM c3),
c2 AS (SELECT * FROM t1)
SELECT * FROM c1;
i
1
2
3
DROP TABLE t1;
#
# MDEV-14297: Lost name of a explicitly named CTE column used in
#             the non-recursive CTE via prepared statement
#
CREATE TABLE t1 (i int);
INSERT INTO t1 VALUES (1),(2),(3);
PREPARE stmt FROM "WITH cte(a) AS (SELECT 1) SELECT * FROM cte";
EXECUTE stmt;
a
1
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "CREATE VIEW v1 AS WITH cte(a) AS (SELECT 1) SELECT * FROM cte";
EXECUTE stmt;
SELECT * FROM v1;
a
1
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM "CREATE VIEW v2 AS WITH cte(a) AS (SELECT * FROM t1) SELECT * FROM cte";
EXECUTE stmt;
SELECT * FROM v2;
a
1
2
3
DEALLOCATE PREPARE stmt;
DROP TABLE t1;
DROP VIEW v1,v2;
#
# MDEV-15478: Lost name of a explicitly named CTE column used in
#             the non-recursive CTE defined with UNION
#
CREATE TABLE t1 (x int, y int);
INSERT INTO t1 VALUES (1,2),(2,7),(3,3);
WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT * FROM cte;
a
1
2
WITH cte(a) AS (SELECT 1 UNION SELECT 2) SELECT a FROM cte;
a
1
2
WITH cte(a) AS (SELECT 1 UNION ALL SELECT 1) SELECT a FROM cte;
a
1
1
WITH cte(a) AS (SELECT x from t1 UNION SELECT 4) SELECT a FROM cte;
a
1
2
3
4
WITH cte(a) AS (SELECT 4 UNION SELECT x FROM t1 UNION SELECT 5)
SELECT a FROM cte;
a
4
1
2
3
5
WITH cte(a,b) AS (SELECT 4,5 UNION SELECT 4,3) SELECT a,b FROM cte;
a	b
4	5
4	3
DROP TABLE t1;
#
# MDEV-16353: unreferenced CTE specified by query with UNION
#
with cte as
(select 1 union select 2 union select 3)
select 1 as f;
f
1
create table t1 (a int);
insert into t1 values (2), (1), (7), (1), (4);
with cte as
(select * from t1 where a < 2 union select * from t1 where a > 5)
select 2 as f;
f
2
drop table t1;
#
# MDEV-16473: query with CTE when no database is set
#
create database db_mdev_16473;
use db_mdev_16473;
drop database db_mdev_16473;
# Now no default database is set
select database();
database()
NULL
with cte as (select 1 as a) select * from cte;
a
1
create database db_mdev_16473;
create table db_mdev_16473.t1 (a int);
insert into db_mdev_16473.t1 values (2), (7), (3), (1);
with cte as (select * from db_mdev_16473.t1) select * from cte;
a
2
7
3
1
with cte as (select * from db_mdev_16473.t1)
select * from cte, t1 as t where cte.a=t.a;
ERROR 3D000: No database selected
with cte as (select * from db_mdev_16473.t1)
select * from cte, db_mdev_16473.t1 as t where cte.a=t.a;
a	a
2	2
7	7
3	3
1	1
drop database db_mdev_16473;
use test;
#
# MDEV-17154: using parameter markers for PS within CTEs more than once
#             using local variables in SP within CTEs more than once
#
prepare stmt from "
with cte(c) as (select ? ) select r.c, s.c+10  from cte as r, cte as s;
";
set @a=2;
execute stmt using @a;
c	s.c+10
2	12
set @a=5;
execute stmt using @a;
c	s.c+10
5	15
deallocate prepare stmt;
prepare stmt from "
with cte(c) as (select ? ) select c from cte union select c+10 from cte;
";
set @a=2;
execute stmt using @a;
c
2
12
set @a=5;
execute stmt using @a;
c
5
15
deallocate prepare stmt;
prepare stmt from "
with cte_e(a,b) as
(
  with cte_o(c) as (select ?)
  select r.c+10, s.c+20 from cte_o as r, cte_o as s
)
select * from cte_e as cte_e1 where a > 12
union all
select * from cte_e as cte_e2;
";
set @a=2;
execute stmt using @a;
a	b
12	22
set @a=5;
execute stmt using @a;
a	b
15	25
15	25
deallocate prepare stmt;
create table t1 (a int, b int);
insert into t1 values
(3,33), (1,17), (7,72), (4,45), (2,27), (3,35), (4,47), (3,38), (2,22);
prepare stmt from "
with cte as (select * from t1 where a < ? and b > ?)
  select r.a, r.b+10, s.a, s.b+20 from cte as r, cte as s where r.a=s.a+1;
";
set @a=4, @b=20;
execute stmt using @a,@b;
a	r.b+10	a	s.b+20
3	43	2	47
3	45	2	47
3	48	2	47
3	43	2	42
3	45	2	42
3	48	2	42
set @a=5, @b=20;
execute stmt using @a,@b;
a	r.b+10	a	s.b+20
4	55	3	53
4	57	3	53
3	43	2	47
3	45	2	47
3	48	2	47
4	55	3	55
4	57	3	55
4	55	3	58
4	57	3	58
3	43	2	42
3	45	2	42
3	48	2	42
deallocate prepare stmt;
create procedure p1()
begin
declare i int;
set i = 0;
while i < 4 do
insert into t1
with cte(a) as (select i) select r.a-1, s.a+1 from cte as r, cte as s;
set i = i+1;
end while;
end|
create procedure p2(in i int)
begin
insert into t1
with cte(a) as (select i) select r.a-1, s.a+1 from cte as r, cte as s;
end|
delete from t1;
call p1();
select * from t1;
a	b
-1	1
0	2
1	3
2	4
call p1();
select * from t1;
a	b
-1	1
0	2
1	3
2	4
-1	1
0	2
1	3
2	4
delete from t1;
call p2(3);
select * from t1;
a	b
2	4
call p2(7);
select * from t1;
a	b
2	4
6	8
drop procedure p1;
drop procedure p2;
drop table t1;
#
# MDEV-17107: PS for CREATE OR REPLACE VIEW defined by SELECT with CTEs
#
create table t1(a int);
insert into t1 values (3), (1), (2);
create table t2 (b int);
insert into t2 values (2), (10);
prepare stmt from
"create or replace view v1 as
 with t as (select s.a from (select t1.a from t1) s),
      r as(select t.a from t2, t where t2.b=t.a)
 select a from r;";
execute stmt;
select * from v1;
a
2
drop view v1;
drop table t1,t2;
#
# MDEV-19112: CTE usage when information_schema is set as default db
#
with t as (select 1 as t ) select * from t;
t
1
use information_schema;
with t as (select 1 as t) select * from t;
t
1
with columns as (select 1 as t) select * from columns;
t
1
use test;
#
# MDEV-18460: Server crashed in strmake / tdc_create_key /
# THD::create_tmp_table_def_key
#
connect con1,localhost,root,,;
CREATE TEMPORARY TABLE test.t (a INT);
WITH cte AS (SELECT 1) SELECT * FROM cte;
1
1
WITH t AS (SELECT 1) SELECT * FROM t;
1
1
WITH cte AS (SELECT 1) SELECT * FROM t;
ERROR 3D000: No database selected
DROP TABLE test.t;
connection default;
disconnect con1;
#
# MDEV-22781: create view with CTE without default database
#
create database db;
use db;
drop database db;
create database db1;
create table db1.t1 (a int);
insert into db1.t1 values (3),(7),(1);
create view db1.v1 as with t as (select * from db1.t1) select * from t;
show create view db1.v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `db1`.`v1` AS with t as (select `db1`.`t1`.`a` AS `a` from `db1`.`t1`)select `t`.`a` AS `a` from `t`	latin1	latin1_swedish_ci
select * from db1.v1;
a
3
7
1
drop view db1.v1;
prepare stmt from "
create view db1.v1 as with t as (select * from db1.t1) select * from t;
";
execute stmt;
deallocate prepare stmt;
show create view db1.v1;
View	Create View	character_set_client	collation_connection
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `db1`.`v1` AS with t as (select `db1`.`t1`.`a` AS `a` from `db1`.`t1`)select `t`.`a` AS `a` from `t`	latin1	latin1_swedish_ci
select * from db1.v1;
a
3
7
1
drop view db1.v1;
drop table db1.t1;
drop database db1;
use test;
#
# MDEV-24597: CTE with union used multiple times in query
#
with cte(a) as
(select 1 as d union select 2 as d)
select a from cte as r1
union
select a from cte as r2;
a
1
2
create table t1 (a int, b int) engine=myisam;
insert into t1 values
(3,30), (7,70), (1,10), (7,71), (2,20), (7,72), (3,33), (4,44),
(5,50), (4,40), (3,33), (4,42), (4,43), (5,51);
with cte(c) as
(select a from t1 where b < 30 union select a from t1 where b > 40)
select * from cte as r1, cte as r2 where r1.c = r2.c;
c	c
1	1
2	2
7	7
4	4
5	5
with cte(a,c) as
(
select a, count(*) from t1 group by a having count(*) = 1
union
select a, count(*) from t1 group by a having count(*) = 3
)
select a, c from cte as r1 where a < 3
union
select a, c from cte as r2 where a > 4;
a	c
1	1
2	1
7	3
drop table t1;
#
# MDEV-23886: Stored Function returning the result of a query
#             that uses CTE over a table twice
#
create table t1 (c1 int);
insert into t1 values (1),(2),(6);
create function f1() returns int return
( with cte1 as (select c1 from t1)
select sum(c1) from
(select * from cte1 union all select * from cte1) dt
);
select f1();
f1()
18
create function f2() returns int return
( with cte1 as (select c1 from t1)
select sum(s.c1) from cte1 as s, cte1 as t where s.c1=t.c1
);
select f2();
f2()
9
create function f3() returns int return
( with cte1 as (select c1 from t1)
select
case
when exists(select 1 from cte1 where c1 between 1 and 2) then 1
when exists(select 1 from cte1 where c1 between 5 and 6) then 2
else 0
end
);
select f3();
f3()
1
create view v1 as (select c1 from t1);
create function f4() returns int return
( select sum(c1) from
(select * from v1 union all select * from v1) dt
);
select f4();
f4()
18
create function f5() returns int return
( select sum(s.c1) from v1 as s, v1 as t where s.c1=t.c1
);
select f5();
f5()
9
create view v2(s) as
with cte1 as (select c1 from t1)
select sum(c1) from (select * from cte1 union all select * from cte1) dt;
create function f6() returns int return
(select s from v2);
select f6();
f6()
18
create function f7() returns int return
( select r.s from v2 as r, v2 as t where r.s=t.s
);
select f7();
f7()
18
select f5() + f6();
f5() + f6()
27
prepare stmt from "select f5() + f6();";
execute stmt;
f5() + f6()
27
execute stmt;
f5() + f6()
27
deallocate prepare stmt;
drop function f1;
drop function f2;
drop function f3;
drop function f4;
drop function f5;
drop function f6;
drop function f7;
drop view v1;
drop view v2;
create table t2 (a int, b int);
insert into t2
with cte1 as (select c1 from t1)
select * from cte1 as s, cte1 as t where s.c1=t.c1 and s.c1 > 5;
select * from t2;
a	b
6	6
create procedure p1()
begin
insert into t2
with cte1 as (select c1 from t1)
select * from cte1 as s, cte1 as t where s.c1=t.c1 and s.c1 <= 2 and t.c1 >= 2;
end |
call p1();
select * from t2;
a	b
6	6
2	2
drop procedure p1;
# checking CTE resolution for queries with hanging CTEs
with
cte1(a) as (select * from t1 where c1 <= 2),
cte2(b) as (select * from cte1 where a >= 2),
cte3 as (select * from cte1,cte2 where cte1.a < cte2.b)
select * from cte3;
a	b
1	2
select * from t2;
a	b
6	6
2	2
with
cte1(a) as (select * from t1 where c1 <= 2),
cte2(b) as (select * from cte1 where a >= 2),
cte3 as (select * from cte1,cte2 where cte1.a < cte2.b)
select * from t2;
a	b
6	6
2	2
with
cte1(a) as (select * from t1 where c1 <= 2),
cte2(b) as (select * from cte1 where c1 >= 2),
cte3 as (select * from cte1,cte2 where cte1.a < cte2.b)
select * from t2;
ERROR 42S22: Unknown column 'c1' in 'where clause'
with
cte1(a) as (select * from t1 where c1 <= 2),
cte2(b) as (select * from cte1 where a >= 2),
cte3 as (select * from cte1,cte2 where cte1.a < cte2.c1)
select * from t2;
ERROR 42S22: Unknown column 'cte2.c1' in 'where clause'
with
cte1 as (select * from t1 where c1 <= 2),
cte2(a,b) as (select * from cte1 as s1, cte1 as s2 where s1.c1=s2.c1)
select * from cte2;
a	b
1	1
2	2
with
cte1 as (select * from t1 where c1 <= 2),
cte2(a,b) as (select * from cte1 as s1, cte1 as s2 where s1.c1=s2.c1)
select * from t2;
a	b
6	6
2	2
with
cte1 as (select * from t1 where c1 <= 2),
cte2(a,b) as (select * from cte1 as s1, cte1 as s2 where s1.c1=c1)
select * from t2;
ERROR 23000: Column 'c1' in where clause is ambiguous
with cte3 as
( with cte2(a,b) as
( with cte1 as (select * from t1 where c1 <= 2)
select * from cte1 as s1, cte1 as s2 where s1.c1=s2.c1)
select r1.a,r2.b from cte2 as r1, cte2 as r2)
select * from cte3;
a	b
1	1
2	1
1	2
2	2
with cte3 as
( with cte2(a,b) as
( with cte1 as (select * from t1 where c1 <= 2)
select * from cte1 as s1, cte1 as s2 where s1.c1=s2.c1)
select r1.a,r2.b from cte2 as r1, cte2 as r2)
select * from t2;
a	b
6	6
2	2
with cte3 as
( with cte2(a,b) as
( with cte1 as (select * from t1 where c1 <= 2)
select * from cte1 as s1, cte1 as s2 where s1.c1=s2.c1)
select r1.c1,r2.c1 from cte2 as r1, cte2 as r2)
select * from t2;
ERROR 42S22: Unknown column 'r1.c1' in 'field list'
create procedure p1()
begin
insert into t2
with cte1 as (select c1 from t1)
select * from t1 as s, t1 as t where s.c1=t.c1 and s.c1 <= 2 and t.c1 >= 2;
end |
call p1();
select * from t2;
a	b
6	6
2	2
2	2
drop procedure p1;
create procedure p1()
begin
insert into t2
with cte1 as (select a from t1)
select * from t1 as s, t1 as t where s.c1=t.c1 and s.c1 <= 2 and t.c1 >= 2;
end |
call p1();
ERROR 42S22: Unknown column 'a' in 'field list'
drop procedure p1;
drop table t1,t2;
#
# MDEV-20411: SP containing only one SELECT with WITH clause
#
create procedure sp1 ()
with cte as (select 1 as a) select * from cte;
call sp1();
a
1
call sp1();
a
1
create table t1 (a int);
insert into t1 values (3), (7), (1), (7), (1), (1), (3), (1), (5);
create procedure sp2 ()
with cte as (select * from t1) select * from cte;
call sp2();
a
3
7
1
7
1
1
3
1
5
call sp2();
a
3
7
1
7
1
1
3
1
5
create procedure sp3 ()
with cte as (select * from t1 group by a) select * from cte;
call sp3();
a
1
3
5
7
call sp3();
a
1
3
5
7
drop procedure sp1;
drop procedure sp2;
drop procedure sp3;
drop table t1;
#
# MDEV-26095: missing RECURSIVE for the recursive definition of CTE
#             embedded into another CTE definition
#
create table t1 (a int);
insert into t1 values (5), (7);
with cte_e as (
with recursive cte_r as (
select a from t1 union select a+1 as a from cte_r r where a < 10
) select * from cte_r
) select * from cte_e;
a
5
7
6
8
9
10
with cte_e as (
with cte_r as (
select a from t1 union select a+1 as a from cte_r r where a < 10
) select * from cte_r
) select * from cte_e;
ERROR 42S02: Table 'test.cte_r' doesn't exist
drop table t1;
#
# MDEV-26025: query with two usage of a CTE executing via PS /SP
#
create table t1 (a int, b int);
insert into t1 value (1,3), (3,2), (1,3), (4,1);
prepare stmt from "with
cte1 as ( select a,b from t1 where a = 1 AND b = 3 ),
cte2 as ( select a,b from cte1 ),
cte3 as ( select a,b from cte2 )
select * from cte3, cte2";
execute stmt;
a	b	a	b
1	3	1	3
1	3	1	3
1	3	1	3
1	3	1	3
execute stmt;
a	b	a	b
1	3	1	3
1	3	1	3
1	3	1	3
1	3	1	3
deallocate prepare stmt;
create procedure sp() with
cte1 as ( select a,b from t1 where a = 1 AND b = 3 ),
cte2 as ( select a,b from cte1 ),
cte3 as ( select a,b from cte2 )
select * from cte3, cte2;
call sp();
a	b	a	b
1	3	1	3
1	3	1	3
1	3	1	3
1	3	1	3
call sp();
a	b	a	b
1	3	1	3
1	3	1	3
1	3	1	3
1	3	1	3
drop procedure sp;
drop table t1;
#
# MDEV-26825: query with two usage of CTE that refers to another CTE
#             with stored function using a base table.
#
create table t1 (id int primary key);
insert into t1 values
(1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
create  function f(in_id int) returns integer
return (select id from t1 where t1.id = in_id);
with c1 as (select id from t1 where f(id)=id group by id),
c2 as (select id from c1 as pt group by id)
select id from c2 as s1 union select id from c2 as s2;
id
1
2
3
4
5
6
7
8
9
10
with c1 as (select id from t1 as r where f(id)=id group by id),
c2 as (select id from c1 as pt group by id)
select id from c2 as s1 union select id from c2 as s2;
id
1
2
3
4
5
6
7
8
9
10
create function g() returns int return (select count(*) from t1);
create  procedure sp1()
with c1 as (select id from t1 a where g() > 10),
c2 as (select id from c1)
select id from c2 as s1 union select id from c2 as s2;
call sp1();
id
call sp1();
id
drop procedure sp1;
drop function g;
drop function f;
drop table t1;
#
# MDEV-27086: union using CTEs in CREATE TABLE
#
create or replace temporary table tmp as
with cte1 as (select 1 as a), cte2 as (select 2 as a)
select * from cte1 union select * from cte2;
select * from tmp;
a
1
2
create table t1 as
with cte1 as (select 1 as a), cte2 as (select 2 as a)
select * from cte1 union select * from cte2;
select * from t1;
a
1
2
insert into t1 values (3);
create table t2 as
with cte1 as (select * from t1 where a <2), cte2 as (select * from t1 where a > 2)
select * from cte1 union select * from cte2;
select * from t2;
a
1
3
drop table t1,t2;
#
# MDEV-26470: CTE in WITH clause of subquery used in DELETE
#
create table t1 (a int);
insert into t1 values (3), (7), (1), (5);
create table t2 (b int);
insert into t2 values (4), (1), (3), (2);
delete from t1
where a in (with cte(a) as (select * from t2 where b <=2) select a from cte);
select * from t1;
a
3
7
5
insert into t1 values (1), (3);
delete t1 from t1, t2
where t1.a=t2.b or
t1.a in (with cte(a) as (select b+1 from t2) select * from cte);
select * from t1;
a
7
drop table t1,t2;
#
# MDEV-25766: Unused CTE lead to a crash in
# find_field_in_tables/find_order_in_list
#
create table t1 (f1 INTEGER);
create view v1 as
select
subq_0.c4 as c2,
subq_0.c4 as c4
from
(select
ref_0.f1 as c4
from
t1 as ref_0
where (select 1)
) as subq_0
order by c2, c4 desc;
WITH
unused_with AS (select
subq_0.c4 as c6
from
(select
11 as c4
from
v1 as ref_0
) as subq_0,
v1 as ref_2
)
select 1 ;
1
1
drop view v1;
drop table t1;
# End of 10.2 tests
#
# MDEV-21673: several references to CTE that uses
#             local variables / parameters of SP
#
CREATE TABLE t1 (col1 int);
CREATE TABLE t2 (col1 int, col2 date, col3 varchar(16), col4 int);
CREATE TABLE t3 (col1 int, col2 date);
CREATE TABLE t4 (col1 int, col2 date);
INSERT INTO t1 VALUES (3), (7), (9), (1);
INSERT INTO t2 VALUES
(3,'2019-09-01','AAA',2), (7,'2019-10-01','AAA',4), (3,'2019-10-01','AAA',8),
(1,'2019-10-01','BBB',9), (1,'2019-10-01','AAA',4), (1,'2019-10-01','AAA',6);
INSERT INTO t3 VALUES
(4,'2018-10-01'), (6,'2018-10-01'), (4,'2017-10-01'), (7,'2017-10-01');
INSERT INTO t4 VALUES
(5,'2018-10-01'), (8,'2017-10-01'), (4,'2017-10-01');
CREATE OR REPLACE PROCEDURE SP1()
BEGIN
DECLARE p_date date;
DECLARE p_var2 varchar(16);
SET p_date='2019-10-01';
SET p_var2='AAA';
WITH cte_first(col) AS
(
SELECT DISTINCT col4
FROM t1, t2
WHERE t2.col1 = t1.col1 AND t2.col2 = p_date AND t2.col3 = p_var2
),
cte2 AS
(
SELECT DISTINCT col2
FROM t3
WHERE col1 IN ( SELECT col FROM cte_first )
),
cte3 AS (
SELECT distinct t4.col1
FROM cte2, t4
WHERE t4.col2 = cte2.col2 AND t4.col1 IN ( SELECT col FROM cte_first )
)
SELECT * FROM cte3;
END|
CREATE PROCEDURE SP2(IN d date)
BEGIN
DECLARE p_var2 varchar(16);
SET p_var2='AAA';
WITH cte_first(col) AS
(
SELECT DISTINCT col4
FROM t1, t2
WHERE t2.col1 = t1.col1 AND t2.col2 = d AND t2.col3 = p_var2
),
cte2 AS
(
SELECT DISTINCT col2
FROM t3
WHERE col1 IN ( SELECT col FROM cte_first )
),
cte3 AS (
SELECT distinct t4.col1
FROM cte2, t4
WHERE t4.col2 = cte2.col2 AND t4.col1 IN ( SELECT col FROM cte_first )
)
SELECT * FROM cte3;
END|
CREATE TABLE t AS
SELECT col4 AS col
FROM t1, t2
WHERE t2.col1 = t1.col1 AND t2.col2 ='2019-10-01' AND t2.col3 = 'AAA';
SELECT * FROM t;
col
4
8
4
6
CREATE TABLE tt AS
SELECT col2
FROM t3
WHERE col1 IN ( SELECT col FROM t );
SELECT * FROM tt;
col2
2018-10-01
2017-10-01
2018-10-01
SELECT t4.col1
FROM tt, t4
WHERE t4.col2 = tt.col2 AND t4.col1 IN ( SELECT col FROM t );
col1
4
8
DROP TABLE t,tt;
CALL SP1();
col1
8
4
CALL SP1();
col1
8
4
CALL SP2('2019-10-01');
col1
8
4
CALL SP2('2019-10-01');
col1
8
4
DROP PROCEDURE SP1;
DROP PROCEDURE SP2;
DROP TABLE t1,t2,t3,t4;
# End of 10.3 tests
#
# MDEV-20730: Syntax error on SELECT INTO @variable with CTE
#
with data as (select 1 as id)
select id into @myid from data;
set @save_sql_mode = @@sql_mode;
set sql_mode="oracle";
with data as (select 1 as id)
select id into @myid from data;
set sql_mode= @save_sql_mode;
# End of 10.4 tests