Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在SQL中,从一个表更新另一个表的最佳方法是什么?_Php_Mysql_Sql_Inner Join_Exists - Fatal编程技术网

Php 在SQL中,从一个表更新另一个表的最佳方法是什么?

Php 在SQL中,从一个表更新另一个表的最佳方法是什么?,php,mysql,sql,inner-join,exists,Php,Mysql,Sql,Inner Join,Exists,我有两个表,第一个是产品页面 +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11)

我有两个表,第一个是产品页面

+------------+--------------+------+-----+---------+----------------+
| 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 |                |
+-------------+--------------+------+-----+-------------------+----------------+
第二个是产品指令

+------------+--------------+------+-----+---------+----------------+
| 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.idproduct=product_commanded.idproduct和product_visited.logdate=product_commanded.date,我如何更新product_visited中的列commanded

我不知道如何使用内部联接存在


我想
更新product\u visitored.commanded=1
logdate
idproduct
的值存在于product\u 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_中的记录,其中Product_中有匹配的行,这些行基于您提供的连接谓词


注意:这是一个SQL Server答案。可能在MySQL中工作,也可能不工作,好吧,我已经对连接字段进行了猜测,但是您想要的是这样的东西

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_中的记录,其中Product_中有匹配的行,这些行基于您提供的连接谓词


注意:这是一个SQL Server答案。在MySQL中可能工作,也可能不工作听起来像是要更新
commanded
,只要
commanded
表中存在相同产品的记录

在任何数据库中:

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

听起来您想在
commanded
表中的同一产品存在记录时更新
commanded

在任何数据库中:

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

我相信这就是你想要的:

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

如果要联接表,使用exists将不允许您使用子查询中的任何字段来更新主表。您可以将您提到的谓词条件放入update语句中的where子句中。但是为了得到完整的SQL,您必须告诉我们您想要更新列
commanded
@RichBenner的值(或可以计算的表达式),我阅读了文档中的“join”,但是当我们加入时,我们可以同时更新表吗?顺便说一句,所有
id.*
字段都应该有一个索引(简单或独特,我让你选择).这会加快你的速度query@parik,这取决于您使用的数据库供应商。对于SQL Server、mySql、Oracle,执行此操作的语法略有不同…如果要加入表,使用exists将不允许您使用子查询中的任何字段来更新主表。您可以将您提到的谓词条件放入更新中的where子句中但是为了得到完整的SQL,你必须告诉我们你想要更新列
commanded
@RichBenner的值(或者可以计算的表达式),我读了文档中的“join”,但是当我们加入时,我们能同时更新表吗?顺便说一句,你所有的
id.*
字段都应该有一个索引(简单或独特,我让你选择)。它会加快你的速度query@parik,这取决于您使用的数据库供应商。SQL Server、mySql和Oracle的语法略有不同……这是标准SQL,应该适用于任何数据库。@Gordon,是的,我从不同的代码开始编写,而这些代码在mySQL@Gordon,(和@parik),是否需要Exists子查询中的
logdate
上的谓词?这是标准SQL,应该适用于任何数据库。@Gordon,是的,我从不同的代码开始,而这些代码在mySQL@Gordon,(和@parik),您是否需要Exists子查询中
logdate
上的谓词?我现在不能投票,非常感谢,这正是我搜索的内容。我现在不能投票,非常感谢,这正是我搜索的内容