I have the following table:
id, product_id, store_id, stock_id, priority 1, 905, 0, 1, 10 2, 905, 0, 1, 20 3, 905, 1, 1, 5
I need to filter by store_id, but if store_id doesn't exist, filter by store_id = 0 instead e.g. SELECT * FROM table WHERE product_id = 905 AND store_id = 1 returns row 3 as expected; SELECT * FROM table WHERE product_id = 905 AND store_id = 2 should return rows 1 and 2.
The following works where store_id can't be found: SELECT * FROM table WHERE product_id = 905 AND (CASE WHEN store_id = 2 THEN store_id = 2 ELSE store_id = 0 END)
However, the following returns all rows, rather than just row 3: SELECT * FROM table WHERE product_id = 905 AND (CASE WHEN store_id = 1 THEN store_id = 1 ELSE store_id = 0 END)
Aucun commentaire:
Enregistrer un commentaire