好的,我正在尝试更新一个表,具体取决于它是否属于用户,所以在一个表例如我有图片,如下:
+----------------------------------------------------+ | picture_id | picture_user | picture_title | +----------------------------------------------------+
在另一张表中我有:
+--------------------------------------------------------------+ | delete_pic_id | delete_pic_user | delete_pic_status | +--------------------------------------------------------------+
现在我只想在delete_pic_user与picture_user匹配时更新delete_pic_status
这就是我的想法,但需要一些帮助:
$manage_application = $database->prepare(
"UPDATE deleted_pic
SET delete_pic_status=1
WHERE delete_pic_user(the other table)=the id");
我将如何完成此项
答案 0 :(得分:2)
使用Exists
UPDATE deleted_pic
SET delete_pic_status = 1
WHERE EXISTS (SELECT 1
FROM other_table ot
WHERE ot.picture_user = deleted_pic.delete_pic_user)
答案 1 :(得分:1)
UPDATE deleted_pic SET delete_pic_status=1 WHERE
delete_pic_user in (select picture_user from myOthertable)
答案 2 :(得分:1)
我假设您正在使用PDO,并且因为您在会话变量中拥有用户的ID,您可以使用像这样的参数化查询
$sth = $database->prepare( "UPDATE deleted_pic
SET delete_pic_status = 1
WHERE delete_pic_user = :id");
$result = $sth->execute(array(':id' => $_SESSION['userId']) );
if ( $result ) {
// the delete worked
} else {
// the delete failed
}
答案 3 :(得分:0)
以下查询将满足您的需求 -
根据检查另一个表中是否存在来更新所有行
UPDATE deleted_pic dp
JOIN other_table ot ON ot.picture_user = dp.delete_pic_user
SET delete_pic_status = 1;
在所有行的另一个表中更新一个表的数据 -
UPDATE deleted_pic dp
JOIN other_table ot ON ot.picture_user = dp.delete_pic_user
SET dp.column1 = ot.column5;
在另一个表中更新特定行的一个表的数据 -
UPDATE deleted_pic dp
JOIN other_table ot ON ot.picture_user = dp.delete_pic_user
SET dp.column1 = ot.column5
where dp.status=1;
答案 4 :(得分:0)
更新deleted_pic
SET delete_pic_status = 1
WHERE deleted_pic.delete_pic_user in(SELECT picture_user from tab2
)