简体   繁体   English

SQL JOIN查询错误

[英]SQL JOIN query error

Alright, so i've been working with SQL for some time now.. probably 2maybe 3 years off and on. 好吧,所以我现在使用SQL已经有一段时间了..大概2也许3年了。 I'd say i'm decently good at it, but with that being said, I have never been able to fully comprehend the "JOIN" function in SQL. 我会说我非常擅长,但是话虽如此,我还是无法完全理解SQL中的“ JOIN”功能。 The tutorials and what not do not help me either. 教程和其他内容也无济于事。

i've been fiddling around with it today, and i still cannot get it to work; 我今天一直在摆弄它,但我仍然无法使它工作。

    <?php
include($_SERVER['DOCUMENT_ROOT'].'/includes/constants.php');

$sql = "UPDATE users SET health = store.type_value - health WHERE uid = :uid INNER JOIN store AS store WHERE iid = :iid";
$que = $db->prepare($sql);
$que->bindParam('uid', $_SESSION['uid']);
$iid = '1';
$que->bindParam('iid', $iid);
try{$que->execute();}catch(PDOException $e){ echo $e->getMessage(); }
?>

could someone please explain to me how to do this correctl? 有人可以向我解释如何纠正吗?

If you are using MySQL, then the join goes above the set statement: 如果你正在使用MySQL,那么join推移上面set的语句:

update users join
       store
       on users.uid = :uid  and iid = :iid
    set health = store.type_value - health;

This puts both conditions in the on clause. 这会将两个条件都放在on子句中。 However, this is a strange condition, and I suspect it is not what you really want. 但是,这是一个奇怪的情况,我怀疑这不是您真正想要的。 What is the join key between users and stores ? usersstores之间的联接密钥是什么?

If you are using SQL Server, then the join goes in a from clause after the set : 如果您正在使用SQL Server,然后join进去一from 子句set

update users 
    set health = store.type_value - health
    from users join
         store
         on users.uid = :uid  and iid = :iid;

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

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