2013-08-03 86 views
1

好的,所以我一直在使用SQL一段时间..可能2maybe 3年和关闭。我会说我非常擅长,但据说,我从来没有能够完全理解SQL中的“JOIN”函数。教程和什么都不帮助我。SQL JOIN查询错误

今天我一直在摆弄它,而且我仍然无法完成它的工作;

<?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(); } 
?> 

有人可以请向我解释如何做到这一点正确吗?

回答

2

如果你正在使用MySQL,那么join去上面的set声明:

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

这使得这两个条件的on子句。但是,这是一个奇怪的情况,我怀疑这不是你真正想要的。 usersstores之间的加入密钥是什么?

如果使用的是SQL Server,则在join进去一from条款set

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