在 SQL 中从另一个更新一个 table 的最佳方法是什么?
What is the best way to update a one table from another in SQL?
我有 2 个表,第一个是产品页面 已访问
+------------+--------------+------+-----+---------+----------------+
| 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 | |
+------------+--------------+------+-----+---------+----------------+
第二个是产品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 | |
+-------------+--------------+------+-----+-------------------+----------------+
如何更新 product_visited 中的 commanded 列,if product_visited.idproduct = product_commanded.idproduct and product_visited.logdate = product_commanded.date
我对使用 inner join 或 exists
感到困惑
我想update product_visited.commanded = 1
当logdate
和idproduct
的值存在于product_commanded时,这意味着访问的产品也被命令
好的,我已经对连接字段进行了猜测,但您正在寻找这样的东西;
UPDATE pv
SET pv.Commanded = 1
FROM Product_Visited pv
JOIN Product_Commanded pc
ON pv.logdate = pc.date
AND pv.idproduct = pc.id
内部联接意味着您只需要根据您提供的联接谓词更新 Product_Visited 中存在匹配行的 Product_Commanded 中的记录。
注意:这是 SQL 服务器的回答。在 MySQL
中可能有效,也可能无效
听起来您想在 commanded
中存在相同产品的记录时更新 commanded
table?
在任何数据库中:
Update product_visited set commanded = 1
Where exists(Select * from product_commanded
where product_id = product_visited.Product_id)
我相信这就是您要找的:
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
);
我有 2 个表,第一个是产品页面 已访问
+------------+--------------+------+-----+---------+----------------+
| 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 | |
+------------+--------------+------+-----+---------+----------------+
第二个是产品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 | |
+-------------+--------------+------+-----+-------------------+----------------+
如何更新 product_visited 中的 commanded 列,if product_visited.idproduct = product_commanded.idproduct and product_visited.logdate = product_commanded.date
我对使用 inner join 或 exists
感到困惑我想update product_visited.commanded = 1
当logdate
和idproduct
的值存在于product_commanded时,这意味着访问的产品也被命令
好的,我已经对连接字段进行了猜测,但您正在寻找这样的东西;
UPDATE pv
SET pv.Commanded = 1
FROM Product_Visited pv
JOIN Product_Commanded pc
ON pv.logdate = pc.date
AND pv.idproduct = pc.id
内部联接意味着您只需要根据您提供的联接谓词更新 Product_Visited 中存在匹配行的 Product_Commanded 中的记录。
注意:这是 SQL 服务器的回答。在 MySQL
中可能有效,也可能无效听起来您想在 commanded
中存在相同产品的记录时更新 commanded
table?
在任何数据库中:
Update product_visited set commanded = 1
Where exists(Select * from product_commanded
where product_id = product_visited.Product_id)
我相信这就是您要找的:
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
);