Flow control (MySQL)
Naar navigatie springen
Naar zoeken springen
Het goede nieuws: MySQL kent flow control. Het slechte nieuws: Het is beperkt, en de meeste mogelijkheden zijn alleen beschikbaar binnen sprocs, en niet in recht-toe-recht-aan SQL-scripts.
- Beschikbaar in stored procedures: if, case, iterative, leave, loop, while & repeat
- Beschikbaar in stored functions: return
- Beschikbaar in SQL: case, if(), ifnull(), nullif().
CASE-construct
Met de CASE-construct kun je (alleen in sprocs!) complete subprocedures implementeren. Bv.:
case when product_group="oem" then create table brands_per_sku_tmp select distinct root_tmp.sku_leading, group_concat(distinct brush_tool_tmp.tool_brand order by tool_brand separator ', ') as brands from root_tmp join brush_tool_tmp on root_tmp.sku_leading = brush_tool_tmp.sku group by root_tmp.sku_leading order by sku_leading; when product_group="bal" or product_group="spc" then create table brands_per_sku_tmp select distinct root_tmp.sku_leading, group_concat(distinct brush_tool_tmp.tool_brand order by tool_brand separator ', ') as brands from root_tmp join brush_tool_tmp on root_tmp.sku_oem = brush_tool_tmp.sku group by root_tmp.sku_leading order by sku_leading; else [statement_list] end case;
Zie SELECT CASE verderop hoe je een case binnen een select-statement kunt doen.
If-functie
Een directe IF-functie kan in SQL-scripts worden toegepast:
SELECT IF(expressie, if_true, if_false)
Voorbeelden:
# "if" en "(" hoeven niet op dezelfde regel te staan #################################################### # select if ( @mijn_var=1, 1, 2 ) as resultaat; +-----------+ | resultaat | +-----------+ | 2 | +-----------+ 1 row in set (0,00 sec)
# Constructie die ik vaak gebruik voor switches in sprocs ######################################################### # concat ( "a", if ( input_parameter like "b", "b", "z" ) )
# Inclusief complete subquery - Moet binnen haakjes ################################################### # select if ( ( select count(*) from information_schema.tables where table_schema='koolborstels' and table_name='term_data_bk01' ), 1, # If true 0 # If false );
IF-statement
Het IF-statement, dat echte conditional branching kent, kan alleen binnen stored procedures worden toegepast.
IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list] ... [ELSE statement_list] END IF;
Voorbeeld - Merk op dat je een ";" na end if
moet hebben:
if in_time_stamp_sort like "short" then set @time_stamp = DATE_FORMAT( NOW(),'%Y%m%d'); else set @time_stamp = DATE_FORMAT( NOW(),'%Y%m%d-%H%i'); end if;
IFNULL-construct
Heel handig om NULL-waardes te onderscheppen en te vervangen. Bv.:
select ifnull("hoihallo","het is null!") > hoihallo
select ifnull(null,"Het is null") > Het is null
Zie IFNULL (MySQL) voor meer.
SELECT CASE
Bv.:
# Element uit een select-statement ################################################################### # ( select case when markt="com" then price_tmp.sell_price_01_euro_txt_period when markt="de" then price_tmp.sell_price_01_euro_txt_period when markt="es" then price_tmp.sell_price_01_euro_txt_period when markt="fr" then price_tmp.sell_price_01_euro_txt_period when markt="uk" then price_tmp.sell_price_02_pond_txt_period end ) as standard_price,
WHILE-construct
Zie WHILE (MySQL)
Zie ook
Bronnen
- https://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html - Flow control in scripts
- https://dev.mysql.com/doc/refman/5.7/en/flow-control-statements.html - Flow control in sprocs
- https://dev.mysql.com/doc/refman/5.1/en/if.html - if-Statement
- https://dev.mysql.com/doc/refman/5.0/en/user-variables.html