jeudi 8 novembre 2018

Creating a conditional SQL select statement that inserts based on a count in MYSQL

I am trying to develop a statement that counts the sizes of a product and insert a statement base on the amount of that count.

For Example,

I have a shirt that has 3 sizes (S, M, L) For reach size I would need to insert a SQL statement. The insert statement is to insert on Table 2 the menu position.

Table1

ID |  Product | Size
1      Shirt      S  
2      Shirt      M
3      Shirt      L

Table2

ID  |  Dropdown_menu_Position
1        0
2        1
3        2

I know the following query is incorrect but I am struggling with the logic behind it. Any SQL guru can help solve or guide me in the right direction?

INSERT INTO Table2
CASE
WHEN COUNT (SIZE) = 1 THEN
     SELECT NULL, '0' 
WHEN COUNT (SIZE) = 2 THEN
     SELECT NULL, '1' 
WHEN COUNT (SIZE) = 3 THEN
     SELECT NULL, '2' 
ELSE ''
END

I am new at making CASE statement and maybe an IF statement would work better for this, but neither one I know.

Aucun commentaire:

Enregistrer un commentaire