# Here follows some examples from our testsuite of mysql
# I'm sorry that I can't distribute the tables because they are quite big,
# but this file will at least show the mysql syntax.
# When I got more time I will write a full test..
#
#
# Test is usualy run with:
#
# mysql -v database < this_file > result_file
#

#
# enkel skning
#
select period from system;
select * from system;
select system.* from system;

# skning med ej nyckel
#
select station.namn from station where freningsnr = 58 and namn like "%LJUGARN%";
select namn from station where namn like "%leden" ;

# skning med ej nyckel samt sortering
#
select station.namn from station where freningsnr = 58 order by namn;
select namn from station order by namn desc limit 10;


# Skning med nyckel = constant p icke unik nyckel
# Hr lses databasen direkt med read-next p namn
#
select station.namn from station where namn = 'JRLSMACKEN AB';

# Test av sortering p p anvnd nyckel (ingen sortering behvs)
#
select station.namn from station where namn >= 'JRLSMACKEN AB' and namn <= 'JRLSMACKEN AB' order by namn;
select stationsnr,namn from station where namn="" or namn = "." order by namn ;

# skning med nyckel = constant med flera trffar
# Hr lses databasen direkt med read-next p namn ifall f trffar
#
select stationsnr,namn from station where freningsnr = 37 and namn = 'STERSUND';


# skning med eller-niver p nyckel
# Ifall man kan begrnsa nyckel till ett antal intervall skes endast de
# mjliga intervallen igenom.
#
select stationsnr from station where stationsnr=250501 or stationsnr=250502; 
select stationsnr from station where stationsnr=250501 or stationsnr=250502 or stationsnr >= 250505 and stationsnr <= 250601;


# skning med nyckel LIKE constant
# Ifall LIKE begreppet brjar p en bokstav anvnds nyckeln
#
select stationsnr,namn from station where freningsnr = 37 and namn like 'f%';
select namn from station where namn like "L%" and namn = "test";
select namn from station where (namn like "L%" and namn = "Lund");


# Skning med distinct
# ifall distinct endast r p en riktig tabell grs en automatisk group ver
# alla flt. I andra fall skapas alltid en temporr databas.
# Ifall endast sorteringsflt frn huvudregistret, sorteras detta frst innan
# den distinkta tabellen skapas.
#
select distinct freningsnr from station ;
select distinct station.namn,period from station,system where freningsnr = 58 and namn like "O%";
select distinct namn from station where freningsnr = 34 order by namn;


# Skning med distinct och order och flera register
# Ifall result-posten r liten skapas en unik nyckel i temporrbasen som
# tcker hela posten. I annat fall grs en jmfrelse alla-mot-alla fr att
# eliminera dubletterna :(
# Detta specifica exempel tar tyvrr station_period som huvudtabell. Fixas genom att
# stta nyckel p freningsnr.

select distinct namn from station,station_period where station.freningsnr = 34 and station.stationsnr=station_period.stationsnr order by namn;


# Skning med konstantregister (systemregister samt nyckel = konstant)
# Alla konstantregister lses i brjan och hrefter lses resten normalt.
#
select period from system;
select period from system where period=1900;
select namn,period from system,station where stationsnr = 011401;


# Skning med konstantregister och flera nyckeldelar (posterna lses 1 gng vid
# start av skningen)
#
select namn,period from station,station_period where station.stationsnr = 011401 and station.stationsnr=station_period.stationsnr and station_period.period=9501;


# Skning med ett konstantregister och flera poster i andra register
#
select namn,period from station,system where freningsnr = 37;


# Skning med register referens och skning p ej nyckel
# (Hr r station_period huvudregister)
#
select namn,period,priv_inkp,ftg_inkp from station,station_period where station.stationsnr=station_period.stationsnr and period >= 9401 and period <= 9402 and station.freningsnr = 34 order by namn,period;


# Skning med intervall p ett register med full nyckel p referensregister
# (Hr r station huvudregister och endast stationerna i stationsnummer
# intervallet kontrolleras)
#
select station.stationsnr,namn,period,priv_inkp,ftg_inkp from station,station_period where station.stationsnr>= 250501 and station.stationsnr <= 250505 and station.stationsnr=station_period.stationsnr and period = 9401 and station.freningsnr = 34;


#Full join (samt alias)
select * from system,system system2;
select station.stationsnr,station2.stationsnr from station,station station2 where station.stationsnr >= 250501 and station.stationsnr <= 250505 and station2.stationsnr >= 250501 and station2.stationsnr <= 250505;


# Skning med 'eller' med samma referens-begrepp
# Frst grs en intervall skning i det frsta registret och sedan refereras
# det andra med nyckel med en 'test ifall nyckelanvnding' fr varje post
#
select station.stationsnr,station.freningsnr,namn,period from station_period,station where station.stationsnr = 250501 and station.stationsnr=station_period.stationsnr and period = 9401 or station.stationsnr = 250502 and station.stationsnr=station_period.stationsnr and period = 9401;


#Skning med flera parentesniver
#
select period from system where (((period > 0) or period < 10000 or (period = 1900)) and (period=1900 and period <= 1901) or (period=1903 and (period=1903)) and period>=1902) or ((period=1904 or period=1905) or (period=1906 or period>1907)) or (period=1908 and period = 1909) ;

select a.stationsnr from station as a,station as b where ((a.stationsnr = 250501 and a.stationsnr=b.stationsnr) or a.stationsnr=250502 or a.stationsnr=250503 or (a.stationsnr=250505 and a.stationsnr<=b.stationsnr and b.stationsnr>=a.stationsnr)) and a.stationsnr=b.stationsnr;

select namn from station where (((namn like "_%L%" ) or (namn like "%test%")) and ( namn like "L%" or namn like "G%")) and namn like "L%" ;


#Group on one table
#optimizer: sort table by group and send rows.
#
select count(*) from system ;
select freningsnr,count(*),sum(stationsnr) from station group by freningsnr ;
select freningsnr,count(*) from station group by freningsnr order by freningsnr desc;
select count(*),min(stationsnamn),max(stationsnamn),sum(stationsnr),avg(stationsnr) from station where freningsnr = 34 ;
select freningsnr,stationsnr,count(priv_inkp),sum(priv_inkp),min(priv_inkp),max(priv_inkp),avg(priv_inkp) from station_period where freningsnr = 34 group by freningsnr,stationsnr ;
select freningsnr,count(priv_inkp),sum(priv_inkp),min(priv_inkp),max(priv_inkp),avg(priv_inkp) from station_period group by freningsnr ;


#Group with order on not first table
#optimizer: sort table by group and write group records to tmp table.
#           sort tmp_table and send rows.
#
select freningsnr,count(*) from station group by freningsnr order by 2 desc ;
select freningsnr,count(*) from station where freningsnr > 40 group by freningsnr order by 2 desc ;
select station.stationsnamn,station.stationsnr,count(priv_inkp),sum(priv_inkp),min(priv_inkp),max(priv_inkp),avg(priv_inkp) from station_period,station where station_period.freningsnr = 34 and station.stationsnr = station_period.stationsnr group by stationsnr,station.stationsnamn ;


#group by with many tables
#optimizer: create tmp table with group-by uniq index.
#           write with update to tmp table.
#           sort tmp table according to order (or group if no order)
#	    send rows
#
select station_period.freningsnr,namn,sum(priv_inkp) from station_period,station where station.stationsnr = station_period.stationsnr and station_period.freningsnr = 58 group by freningsnr,namn ;
select station.freningsnr,count(*),min(namn),max(namn),sum(priv_inkp),avg(priv_inkp) from station,station_period where station_period.freningsnr >= 30 and station_period.freningsnr <= 58 and station_period.stationsnr = station.stationsnr group by station.freningsnr ;


# group with many tables and long group on many tables. group on formula
#optimizer: create tmp table with neaded fields
#           sort tmp table by group and calculate sums to new table
#	    if different order by than group, sort tmp table
#	    send rows

select station_period.freningsnr+0,station_period.stationsnr,namn,sum(priv_inkp) from station_period,station where station.stationsnr = station_period.stationsnr and station_period.freningsnr = 34 group by 1,station_period.stationsnr,namn,namn,namn,namn,namn order by stationsnr;


#WHERE const folding
#optimize: If there is a "field = const" part in the where, change all
#          instances of field in the and level to const.
#	   All instances of const = const are checked once and removed.

#Where -> station_period.stationsnr = 98005 and station.stationsnr = 98005
select sum(priv_inkp) from station_period,station where station.stationsnr = station_period.stationsnr and station_period.freningsnr = 58 and station_period.stationsnr = 98004 and station.stationsnr = 98005 or station.stationsnr = station_period.stationsnr and station_period.stationsnr = 98005 and station.stationsnr = 98005 ;

select station.stationsnr,sum(priv_inkp) from station_period,station where station.stationsnr = station_period.stationsnr and station_period.freningsnr = 58 and station_period.stationsnr = 98004 and station.stationsnr = 98005 or station.stationsnr = station_period.stationsnr and station_period.stationsnr = 98005 and station.stationsnr = 98005 or station_period.stationsnr = station.stationsnr and station.stationsnr = 98004 group by station.stationsnr ;

#
# HAVING
#
select freningsnr,count(*) as count,sum(stationsnr) as sum from station group by freningsnr having count > 40 and sum/count >= 120000 ;

#
# Test of alias
#
select system.period from system = system ;
select system.period from system as system ;
select "Nuvarande period" := period from system;
select system.period as "Nuvarande period" from system as system;
select period:=period from system;
select period:=period from system group by period;
select summa:=1+1 from system group by summa;
select "Nuvarande period" := period from system group by "Nuvarande period";

#
# Some simple show commands
#
show databases;
show tables;
show tables from test;
show fields from station ;
show fields from station from test like 's%' ;
show keys from station ;

#
# Some new procedures
#
select stationsnr from station_drvm where stationsnr>=98001 and stationsnr <= 98002 group by stationsnr procedure split_sum(5,dm_1_kvant10,1,stationsnr,kundnr,kortnr) ;
select stationsnr from station_drvm where stationsnr>=98001 and stationsnr <= 98002 group by stationsnr procedure split_count(5,dm_1_kvant10,1,stationsnr,kundnr,kortnr) ;

#
# Functions
#
select 1+1,1-1,1+1*2,8/5,8%5,mod(8,5),-(1+1)*-2,abs(-1),floor(5.2),sign(-5) ;
select concat("monty"," was here ","again"),length("hello"),locate("hello","he"),locate("hello","he",2),locate("hello","lo",2)  ;
select left("hello",2),right("hello",2),substring("hello",2,2),mid("hello",3,2) ;
select concat(left(right(concat("what ",concat("is ","happening")),9),4),substring("monty",5,1)) ;
select min(1,2),max(3,4,5) ;
select NULL=NULL,0=0,1>0,1>=1,1<0,1<=0,strcmp("abc","abcd"),strcmp("b","a"),strcmp("a","a") ;
select "a"<"b","a"<="b","b">="a","b">"a","a"="A","a"<>"b","abc" like "a%", "abc" not like "%d%" ;
select !0,NOT 0=1,!(0=0),1 AND 1,1 && 0,0 OR 1,1 || NULL ;
select 1 | 2,5 & 3,bit_count(7) ;
select null,isnull(null),isnull(1/0),isnull(1/0 = null),ifnull(null,1),ifnull(null,"TRUE"),ifnull("TRUE","ERROR"),1/0 is null,1 is not null;
select IF(0,"ERROR","this"),IF(1,"is","ERROR"),IF(NULL,"ERROR","a"),IF(1,2,3) ;
select intervall(5,1,2,3,4,5,6,7,8,9,10),intervall(3,1,1+1,1+1+1),between(2,1,3),field("IBM","NCA","ICL","SUN","IBM") ;
select period_add(system.period,-12),period_diff(199505,"9404") from system;
select from_days(to_days("960101")),to_days(960201)-to_days("19960101"),to_days(curdate()+1)-to_days(curdate()),weekday(to_days("1996-01-01")) ;
select replace("aaaa","a","b"),replace("aaaa","aa","b"),replace("aaaa","a","bb") ;
select replace(concat(lcase(concat("THIS"," ","IS"," ","A"," ")),ucase("false")," ","test"),"FALSE","REAL") ;
#
# Functions and groups
#
select concat(station_period.freningsnr,station_period.stationsnr) as concat,station_period.stationsnr+1,left(namn,4),sum(priv_inkp/100),sum(length(concat("1","2"))) from station_period,station where station.stationsnr = station_period.stationsnr and station_period.freningsnr = 34 group by concat,2,3 order by 2;
