php两表更新

时间:2019-02-13 00:23:32

标签: php mysql pdo

我有一个类似于以下内容的MySQL数据库:

优惠券表

| id | coupon_id | host      | away        |guess    |skor  |status|
+----+-----------+-----------+-------------+---------+------+------+
|  1 | 22        | Barcelona | Real Madrid | 1       |2-1   | 1    |
|  2 | 22        | Celtic    | Porto       | 2       |1-0   | 2    |
|  3 | 23        | Barcelona | Real Madrid | 1       |2-1   | 1    |
|  4 | 23        | M city   | Chelsea      | 1       |4-1    | 1   |

优惠券表

| id | user       | Copuonstatus
+----+------------+-------------+
|  22 | Admin     | 0           | 
|  23 | Roberto   | 0           | 

我需要对结果进行排序,如下所示:

COUPONS TABLE

| id | user       | Copuonstatus
+----+------------+-------------+
|  22 | Admin     | 2           | 
|  23 | Roberto   | 1           | 


NOTE : 
Copuonstatus= default value 0
status = 0 waiting
status = 1 win
status = 2 los

im查询尝试, 我不能 或更新INNER JOIN使用?

$q=$db->prepare("SELECT * FROM coupons INNER JOIN coupon ON 
coupon.coupon_id = coupons.id AND coupon.status=2");
$q->execute();
$sss=$q->fetchAll(PDO::FETCH_GROUP);
if ( $sss > 0){ 
$db->exec("UPDATE coupons SET coupon_status = 2  FROM coupons ON 
coupon.coupon_id = coupons.id WHERE coupons.Copuonstatus=0 ");}

1 个答案:

答案 0 :(得分:0)

UPDATE
  coupons AS A
  INNER JOIN coupon AS B ON A.id = B.coupon_id
SET
  coupon_status = 2
WHERE
  A.Copuonstatus = 0

希望有帮助