What is the best way to update a one table from another in SQL?

49 Views Asked by At

I have 2 tables the first one is the product-page visited

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| idproduct  | varchar(128) | YES  |     | NULL    |                |
| logdate    | date         | YES  |     | NULL    |                |
| idmagasin  | int(20)      | YES  |     | NULL    |                |
| idenseigne | int(20)      | YES  |     | NULL    |                |
| commanded  | int(2)       | YES  |     | 0       |                |
+------------+--------------+------+-----+---------+----------------+

And the second one is the product commanded

+-------------+--------------+------+-----+-------------------+----------------+
| Field       | Type         | Null | Key | Default           | Extra          |
+-------------+--------------+------+-----+-------------------+----------------+
| id          | int(11)      | NO   | PRI | NULL              | auto_increment |
| idproduct   | varchar(255) | NO   |     | NULL              |                |
| idenseigne  | int(11)      | NO   |     | NULL              |                |
| idmagasin   | int(11)      | NO   |     | NULL              |                |
| ingredients | tinytext     | YES  |     | NULL              |                |
| date        | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
+-------------+--------------+------+-----+-------------------+----------------+

How can i update the column commanded in product_visited , if product_visited.idproduct = product_commanded.idproduct and product_visited.logdate = product_commanded.date

i'm confused to use inner join or exists

I want to update product_visited.commanded = 1 when the value of logdate and idproduct exists in product_commanded, it will mean the product visited is commanded too

3

There are 3 best solutions below

1
On BEST ANSWER

I believe this is what you are looking for:

Update product_visited pv
    set commanded = 1
    Where exists (Select 1
                  from product_commanded pc
                  where pv.idproduct = pc.idproduct and pv.logdate = pc.date
                 );
0
On

Ok, I've made guesses with the join fields but you're after something like this;

UPDATE pv
SET pv.Commanded = 1
FROM Product_Visited pv
JOIN Product_Commanded pc
    ON pv.logdate = pc.date
    AND pv.idproduct = pc.id

The inner join means that you're only going to update records in Product_Visited where there are matching rows in Product_Commanded based on the join predicates you give it.

Note: this is a SQL Server answer. May or may not work in MySQL

3
On

Sounds like you want to update commanded whenever a record exists for same product in commanded table?

in any database:

Update product_visited set commanded = 1
Where exists(Select * from product_commanded
             where product_id = product_visited.Product_id)