更新,加入和在哪里MySQL

时间:2015-04-16 05:21:50

标签: php mysql

我正在处理我的查询。所以我有两个表,tbl_room和tbl_reservation。我想在以下条件下进行更新查询:

如果出现以下情况,请在tbl_room中将房间计数器减1:

  1. 客户的名字和姓氏与tbl_reservation
  2. 中的名字和姓氏相匹配
  3. tbl_reservation会议室ID与tbl_room会议室ID
  4. 匹配

    我现在坚持这个:

    $result = mysql_query("UPDATE 
      tbl_room 
      JOIN tbl_reservation 
        ON tbl_room.roomID = tbl_reservation.roomID 
        AND tbl_reservation.cus_fname = '$cusFN' 
        AND tbl_reservation.cus_lname = '$cusLN' SET tbl_room.reserve = reserve - 1 ");
    

    这些是我的表格:

    ----------
    tbl_room
    ----------
    roomID,
    room_type,
    capacity,
    reserve <--- reservation counter
    
    
    ----------
    tbl_reservation
    ----------
    reserveID,
    cus_fname,
    cus_lname,
    

2 个答案:

答案 0 :(得分:1)

我已经提出了正确的查询。 Yehey!所以我在while语句之后移动了我的查询并使用了这种语法:

$ result6 = mysql_query(“UPDATE tbl_room JOIN tbl_reservation ON tbl_room.roomID = tbl_reservation.roomID                         AND tbl_reservation.cus_fname ='$ cusFN'                         AND tbl_reservation.cus_lname ='$ cusLN'                         SET tbl_room.reserve = tbl_room.reserve-1“);

感谢所有提供帮助的人!上帝保佑你们! :)

答案 1 :(得分:0)

试试这个......

$sql = "UPDATE `tbl_room`
SET `tbl_room`.`reserve` = `tbl_room`.`reserve` - 1 
WHERE `tbl_room`.`roomID` = `tbl_reservation`.`roomID` 
 AND `tbl_reservation`.`cus_fname` = '$cusFN' 
 AND `tbl_reservation`.`cus_lname` = '$cusLN'";