简体   繁体   English

联接表进行更新

[英]Joining Tables to Update

Can I do this to update a row? 我可以这样做来更新行吗? This is the first time I am using a LEFT JOIN with an UPDATE . 这是我第一次使用带有UPDATELEFT JOIN

UPDATE comments AS r

LEFT JOIN items AS i 
        ON i.items_id = r.item_id
LEFT JOIN master_cat AS c
        ON c.cat_id = i.items_id  
SET r.good = r.good + 1 
WHERE i.item = '{$item}' AND c.category = '{$cat}' AND r.review = '{$review}';

It doesn't throw an error, but it says 0 rows affected even though I have confirmed my variables are ok with right data. 它不会引发错误,但是即使我确认我的变量可以使用正确的数据,它也表示0行受到影响。 (Even if I hard code the data). (即使我对数据进行硬编码)。

Edit : I have tried inner join as well, and does not work either. 编辑 :我也尝试了内部联接,并且也不起作用。

Edit 2 : This is actually what I am trying to do, I have these two queires and they work fine. 编辑2 :这实际上是我想要做的,我有这两个查询器,它们工作正常。 I am trying to simplify code to one. 我正在尝试将代码简化为一个。 Can this be done: 可以这样做:

     // This query returns rate_id which I use as "$review_id" in the second query.

     $query = "
     SELECT r.rate_id
     FROM comments as r
     LEFT JOIN items AS i 
         ON i.items_id = r.item_id
     LEFT JOIN master_cat AS c
         ON c.cat_id = i.cat_id
     WHERE r.review = '{$review}' 
        AND i.item = '{$item}' 
        AND c.category = '{$cat}';"; 

    $query = "
    UPDATE comments 
    SET good = good + 1 
    WHERE rate_id = '{$review_id}';";

如果要外部连接到表c,然后在表c中的字段上有一个where子句,则实际上是有一个内部联接,因为如果缺少联接条件,where子句将不匹配任何内容。

Why are you wanting to do a LEFT JOIN ? 您为什么要LEFT JOIN You're not using any of the columns from the joined tables to update the row... so I'm assuming that the purpose of the joined tables is to limit the rows to be incremented in comments to only those that have a match in the associated tables. 您没有使用联接表中的任何列来更新行...因此,我假设联接表的目的是将comments要递增的行限制为仅包含匹配项的行。关联的表。

The reason that you're getting 0 rows affected is that you've placed additional conditions for the left joined tables in your WHERE clause, which is essentially turning each LEFT JOIN into an INNER JOIN . 您将受到0行影响的原因是,您已在WHERE子句中为左联接表放置了附加条件,这实际上是将每个LEFT JOIN变成INNER JOIN

Assuming that this is a simplified version of your query, and that you have other reasons for wanting to use LEFT JOIN , you would fix this by moving the conditions into your ON clauses: 假设这是查询的简化版本,并且您还有其他想使用LEFT JOIN原因,则可以通过将条件移到ON子句中来解决此问题:

UPDATE comments AS r
    LEFT JOIN items AS i 
        ON i.items_id = r.item_id
        AND i.item = '{$item}' 
    LEFT JOIN master_cat AS c
        ON c.cat_id = i.items_id  
        AND c.category = '{$cat}' 
SET r.good = r.good + 1 
WHERE r.review = '{$review}';

Your problem is that in the UPDATE statement you have a mismatching join ( cat_id with items_id ): 您的问题是,在UPDATE语句中您有一个不匹配的items_idcat_iditems_id ):

LEFT JOIN master_cat AS c
        ON c.cat_id = i.items_id  

while in the SELECT (that is working), you have the join correctly: SELECT (有效)中时,您具有正确的联接:

LEFT JOIN master_cat AS c
        ON c.cat_id = i.cat_id

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

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