简体   繁体   English

仅当其他表中的字段满足条件时,MySQL UPDATE

[英]MySQL UPDATE only if field in other table meets condition

I am having a little trouble trying to accomplish this. 我在尝试完成此操作时遇到了一些麻烦。 Here is the gist of what I need to do: 这是我需要做的要点:

UPDATE links SET
 link = '$link', rid = $rid, order = $order
WHERE lid = $lid
IF (SELECT COUNT(*) FROM resources WHERE rid = $rid AND (sid = $sid OR sid IS NULL) AND types IS NULL) == 1;

So basically, I want to run the UPDATE if and only if the resource in the resources table is associated with the site (sid) or is not associated with any particular site and types is null. 因此,基本上,我仅在资源表中的资源与站点(sid)相关联或与任何特定站点无关且类型为null时,才运行UPDATE。

I suppose I can run a PHP conditional, but it would be preferable if I could do this with one query. 我想我可以运行一个有条件的PHP,但是如果我可以用一个查询执行此操作,那将是更好的选择。 Is it possible? 可能吗?

Thansk so much in advance! 非常感谢坦斯克!

UPDATE links
SET link = '$link', rid = $rid, order = $order
WHERE lid = $lid
and (SELECT COUNT(*) 
    FROM resources 
    WHERE rid = $rid 
    AND (sid = $sid OR sid IS NULL) 
    AND types IS NULL) = 1;

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

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