根据选择查询更新查询

时间:2014-01-29 13:37:12

标签: mysql select

我想写完" UPDATE"和" SELECT"进入一个查询。

我需要检查设置字段。 对于此操作,我在TABLE1上使用SELECT查询,然后如果它没有结果,则更新TABLE2的另一个字段。 FOR EX:

$res = mysqli_query($con , "select sID FROM schedule where (dayID = '{$this->dID}' AND patientID = '')");
$rep = mysqli_fetch_array($res);
if(count($rep) == 0)
mysqli_query($con,"update days set schFilled = 1  where dID = '{$this->dID}'");
else   
mysqli_query($con,"update days set schFilled = 0 where dID = '{$this->dID}'");

我想用ONE查询运行那些,实际上我想要这样的东西:(whit CASE也写第二次更新)

update days set schFilled = 0 where( (select sID FROM schedule where (dayID = '{$this->dID}' AND patientID = '') IS NULL) AND (dID = '{$this->dID}'))

2 个答案:

答案 0 :(得分:0)

使用像这样的mysqli对象:

 $res = mysqli_query($con , "your select");
 if($res->num_rows === 0) {
      //no res
 }
 else {
      //else
 }

答案 1 :(得分:0)

 "UPDATE days SET schFilled = 
                           CASE
                           WHEN (SELECT IF(EXISTS(select sID FROM schedule where (dayID = '{$this->dID}' AND patientID = '' AND hour != 0)), 1, 0))
                            THEN 0
                            ELSE 1
                            END
                            where dID = '{$this->dID}'"