我有两张表,第一张是产品页,访问。
+------------+--------------+------+-----+---------+----------------+
| 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 | |
+------------+--------------+------+-----+---------+----------------+第二个是命令的产品。
+-------------+--------------+------+-----+-------------------+----------------+
| 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,if product_visited.idproduct = product_commanded.idproduct and product_visited.logdate = product_commanded.date中的列?
我很困惑于使用内部连接或存在
我想update product_visited.commanded = 1当logdate和idproduct的值存在于product_commanded中时,意味着访问的产品也是命令的。
发布于 2016-12-21 16:14:58
我相信这就是你想要的:
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
);发布于 2016-12-21 16:07:40
好吧,我已经猜到了连接字段,但是你想要的是这样的东西;
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中有匹配的行。
注意:这是Server的答案。可能在MySQL工作,也可能不工作
发布于 2016-12-21 16:09:52
听起来,当同一产品在commanded表中存在记录时,您想要更新commanded吗?
在任何数据库中:
Update product_visited set commanded = 1
Where exists(Select * from product_commanded
where product_id = product_visited.Product_id)https://stackoverflow.com/questions/41267024
复制相似问题