简体   繁体   English

左联接以过滤UPDATE上的行?

[英]Left join to filter rows on UPDATE?

I'm trying to update a table but I need to do a left join to determine which rows I want to select. 我正在尝试更新表,但是我需要进行左联接以确定我要选择的行。

These are two examples of what I've been trying, I think it's clear what I want to do by seeing any of them: 这是我一直在尝试的两个示例,我认为通过查看其中的任何一个,我很清楚想要做什么:

   UPDATE `User_Likes` 
      SET `Status` = 'read'
LEFT JOIN Posts ON User_Likes.PID = Posts.ID
    WHERE Posts.UID = '1'

Here I'm trying another way: 在这里,我正在尝试另一种方式:

UPDATE `User_Likes` 
   SET `Status` = 'read' 
 WHERE `ID` IN (SELECT Posts.ID
                  FROM `User_Likes`
             LEFT JOIN Posts ON User_Likes.PID = Posts.ID
                 WHERE Posts.UID = '$userID')

Have a look at this Example 看看这个例子

UPDATE User_Likes
LEFT JOIN
Posts ON User_Likes.PID = Posts.ID 
SET Status='read'
WHERE Posts.UID = '1' ;

SQL Fiddle DEMO SQL小提琴演示

The issue with your query is that Id expects only one Id, and your select statement returns multiple ID. 您查询的问题是Id仅需要一个Id,而您的select语句返回多个ID。 so: 所以:

UPDATE `User_Likes` 
   SET `Status` = 'read'
FROM `User_Likes`
LEFT JOIN Posts ON User_Likes.PID = Posts.ID
WHERE Posts.UID = '$userID'

Some thing like this.. 像这样的东西..

UPDATE `User_Likes` u LEFT JOIN Posts p 
ON u.User_Likes.PID = p.Posts.ID
SET `Status` = 'read'
WHERE p.Posts.UID = '1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM