I'm planning on a price alert system to notify users once a product hit lower than their target prices. Some circumstances confine me to store the mainstream and second hand retailers' prices in two separate tables. I may not be able to change it in the near future so forgive me if it may complicate the issue. To notify users once a merchant's price, (whether it's mainstream or second hand) reaches a user's target price zone, I need to insert a record into table price_alert
with values entry_id
,user_id
,merchant_id
and lowest_price
. Here's my question. Can I insert records into price_alert
from table mainstream
and second hand
separately in one statement?
My expected output should be like this:
ENTRY_ID USER_ID MERCHANT_ID LOWEST_PRICE Is_read
1 1 3 100 0
3 2 1 300 0 // Merchant 3 is a second hand store while Merchant 1 is a mainstream store.
This code won't work because it can only get the price from the mainstream_retailer_price
INSERT INTO price_alert (entry_id,user_id,merchant_id,lowest_price)
SELECT
u.entry_id,mrp.merchant_id,u.user_id,mrp.price
FROM
user_target_price u
INNER JOIN mainstream_retailer_price mrp
ON u.product_id = mrp.product_id
INNER JOIN second_hand_retailer_price shrp
ON u.product_id = shrp.product_id
WHERE
(u.target_low_price > mrp.price)
OR u.target_low_price > shrp.price
GROUP BY u.entry_id
Can I do something to the effect of:
SELECT
u.entry_id,(mrp.merchant_id OR shrip.merchant_id),u.user_id,(mrp.price OR shrp.price)
Table Schema:
CREATE TABLE mainstream_retailer_price
(`id` int, `merchant_id` int,`product_id`int,`price` int)
;
INSERT INTO mainstream_retailer_price
(`id`,`merchant_id`,`product_id`,`price`)
VALUES
(1,1,1,200),
(2,1,2,300),
(3,2,1,150)
;
CREATE TABLE second_hand_retailer_price
(`id` int, `merchant_id` int,`product_id` int, `price` int)
;
INSERT INTO second_hand_retailer_price
(`id`,`merchant_id`,`product_id`,`price`)
VALUES
(1,3,1,100),
(2,3,2,600)
;
CREATE TABLE user_target_price
(`entry_id` int,`user_id` int, `target_low_price` int,`product_id` int)
;
INSERT INTO user_target_price
(`entry_id`,`user_id`,`target_low_price`,`product_id`)
VALUES
(1,1,150,1),
(2,1,200,2),
(3,2,350,2)
;
CREATE TABLE merchant
(`merchant_id` int, `merchant` varchar(20))
;
INSERT INTO merchant
(`merchant_id`,`merchant`)
VALUES
(1,'First Hand A'),
(2,'First Hand B'),
(3,'Second Hand A')
;
CREATE TABLE price_alert
(`entry_id` int, `user_id` int,`merchant_id` int,`lowest_price` int,`is_read` int)
;
Seems like you might want to use a UNION to SELECT from both table and INSERT into one. Here is the basic query structure. Your doing 2 selects, combining the results with a UNION, the SELECTin on that UNION for the INSERT. Your field names need to be exactly the same on the inner SELECTs.
INSERT INTO price_alert (entry_id,user_id,merchant_id,lowest_price)
SELECT * FROM (
SELECT ... FROM user_target_price u INNER JOIN mainstream_retailer_price mrp ...
UNION
SELECT ... FROM user_target_price u INNER JOIN second_hand_retailer_price srp ...
) AS rp
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments