I'm trying to create the following pseudocode in MySQL. I have the following tables:
The product_filter
table only has two columns, product_id
and filter_id
I'd like to come up with a SQL that:
specials
table and puts them in the product_filter
table specials
table, it needs to look at the price
column too Here is what I have so far:
INSERT INTO product_filter (product_id,filter_id)
SELECT product_id,
FROM specials;
Any help is appreciated.
Try this, using a CASE
to determine what the filter_id
is (this will set anything that doesn't have a price in those boundaries equal to 0):
INSERT INTO product_filter (product_id,filter_id)
SELECT product_id,
(CASE WHEN price < 100 then 1
WHEN price >= 100 AND price < 500 then 2
WHEN price >= 500 AND price < 1000 then 3
ELSE 0 END) AS filter_id
FROM specials;
Here's a working SQLFiddle .
try this query (you didn't specify for values higher than 1000 so it'll make filter_id 4 if values are greater than 1000).
INSERT INTO product_filter (product_id,filter_id)
SELECT product_id,
CASE WHEN price < 100 THEN 1
WHEN price BETWEEN 100 AND 500 THEN 2
WHEN price BETWEEN 500 AND 1000 THEN 3
ELSE 4
END as filter_id
FROM specials;
Use something like this:
INSERT INTO product_filter (product_id,filter_id)
SELECT
product_id,
(CASE
WHEN price < 100 THEN 1
WHEN price BETWEEN 100 AND 500 THEN 2
WHEN price BETWEEN 500 AND 1000 THEN 3
END) as filter_id
FROM specials;
Try this:
You also need minor change in between clause range values as shown below:
INSERT INTO product_filter (product_id,filter_id)
SELECT product_id,
case when price < 100 then 1 -- price under $100, the filter id be 1
-- price between $100 and $500, the filter id be 2
when price between 101 and 500 then 2
-- price between $500 and $1000, the filter id be 3
when price between 501 and 1000 then 3
-- else ? -- fill this as you need
FROM specials;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.