简体   繁体   中英

SQL JOIN query error

Alright, so i've been working with SQL for some time now.. probably 2maybe 3 years off and on. 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. 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:

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. 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 ?

If you are using SQL Server, then the join goes in a from clause after the set :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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