Insert rows into a table from another two tables separately in mysql

RedGiant

Fiddle Example

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)
;
Brent Baisley

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

insert multiple rows mysql from another table

From Dev

MYSQL: How to insert rows in a table based on a condition of other two tables

From Dev

Insert multiple rows from two tables into one table

From Dev

How to dynamically count rows in a table from two another tables?

From Dev

Mysql Insert data in third table based on data from two tables

From Dev

Insert data form two tables into another table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

insert from another table and then link tables by id

From Dev

Insert values from two tables into a third table

From Dev

Insert into a table from two different tables

From Dev

SQL INSERT INTO two tables from one table

From Dev

How to perform a mass SQL insert to one table with rows from two seperate tables

From Dev

Insert multiple rows from select into another table

From Dev

mysql insert into two tables id from first

From Dev

PHP pull from two MySQL tables, where multiple rows in table two

From Dev

How to insert a tables 600mln rows into another table

From Dev

PHP and MySql - fetching rows from two tables

From Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

From Dev

looping mysql query to get multiple rows of data from a table and insert into another table

From Dev

MySQL - Insert table data from another table

From Dev

MySQL - Insert table data from another table

From Java

How to fast return and count rows from one table based on filter by two another tables

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

How to insert one row of a table into two rows of another table

From Dev

Select rows from a table based on results from two other tables in mySQL using PDO

From Dev

mysql - insert in two tables using autogenerate key from first insert table without using LAST_INSERT_ID

From Dev

MySql Retrieving Data from multiple Tables then Insert limited values In another table

From Dev

Insert multiple rows into a MySQL database from a table

From Dev

MySQL - how to write stored procedure to insert data in to table from two other tables

Related Related

  1. 1

    insert multiple rows mysql from another table

  2. 2

    MYSQL: How to insert rows in a table based on a condition of other two tables

  3. 3

    Insert multiple rows from two tables into one table

  4. 4

    How to dynamically count rows in a table from two another tables?

  5. 5

    Mysql Insert data in third table based on data from two tables

  6. 6

    Insert data form two tables into another table

  7. 7

    MySQL select row with two matching joined rows from another table

  8. 8

    insert from another table and then link tables by id

  9. 9

    Insert values from two tables into a third table

  10. 10

    Insert into a table from two different tables

  11. 11

    SQL INSERT INTO two tables from one table

  12. 12

    How to perform a mass SQL insert to one table with rows from two seperate tables

  13. 13

    Insert multiple rows from select into another table

  14. 14

    mysql insert into two tables id from first

  15. 15

    PHP pull from two MySQL tables, where multiple rows in table two

  16. 16

    How to insert a tables 600mln rows into another table

  17. 17

    PHP and MySql - fetching rows from two tables

  18. 18

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  19. 19

    looping mysql query to get multiple rows of data from a table and insert into another table

  20. 20

    MySQL - Insert table data from another table

  21. 21

    MySQL - Insert table data from another table

  22. 22

    How to fast return and count rows from one table based on filter by two another tables

  23. 23

    Deleting rows from 3 tables in MySQL table

  24. 24

    How to insert one row of a table into two rows of another table

  25. 25

    Select rows from a table based on results from two other tables in mySQL using PDO

  26. 26

    mysql - insert in two tables using autogenerate key from first insert table without using LAST_INSERT_ID

  27. 27

    MySql Retrieving Data from multiple Tables then Insert limited values In another table

  28. 28

    Insert multiple rows into a MySQL database from a table

  29. 29

    MySQL - how to write stored procedure to insert data in to table from two other tables

HotTag

Archive