从其他表数据更新表行不起作用

时间:2019-01-29 15:40:09

标签: php mysql database wordpress

我的数据库中有两个表。一个叫做wp_tickets,另一个叫wp_tickets_regenerated

如果wp_tickets_regenerated匹配,我试图从wp_tickets表中更新lottery_id表中的某些数据,所以我最终得到了一个新表,该表生成了不同的ticket_number该产品的每个订单。

到目前为止,我的代码抛出错误:

 WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM wp_tickets
        WHERE lottery_id = wp_tickets.lottery_id' at line 6 for query UPDATE wp_tickets_regenerated

有人可以帮忙吗?

wp_tickets表

+---------+-----------+-----------+---------------+----------+------------+
| user_id | full_name | answer_id | ticket_number | order_id | lottery_id |
+---------+-----------+-----------+---------------+----------+------------+
| 0       | test      | 1         | 3             | 791      | 790        |
+---------+-----------+-----------+---------------+----------+------------+
| 0       | test      | 1         | 5             | 791      | 790        |
+---------+-----------+-----------+---------------+----------+------------+
| 0       | test      | 1         | 10            | 791      | 790        |
+---------+-----------+-----------+---------------+----------+------------+

wp_tickets_regeneration

   +---------+-----------+-----------+---------------+----------+------------+
    | user_id | full_name | answer_id | ticket_number | order_id | lottery_id |
    +---------+-----------+-----------+---------------+----------+------------+
    |         |           |           | 1             |          | 790        |
    +---------+-----------+-----------+---------------+----------+------------+
    |         |           |           | 2             |          | 790        |
    +---------+-----------+-----------+---------------+----------+------------+
    |         |           |           | 3             |          | 790        |
    +---------+-----------+-----------+---------------+----------+------------+

我到目前为止有:

 $wpdb->query(
       'UPDATE wp_tickets_regenerated
        SET user_id = wp_tickets.user_id,
            full_name = wp_tickets.full_name,
            answer_id = wp_tickets.answer_id,
            order_id = wp_tickets.order_id
        FROM wp_tickets
        WHERE lottery_id = wp_tickets.lottery_id'
  );

2 个答案:

答案 0 :(得分:1)

您需要一个内部联接表 SELECT CodeID codes , COUNT(DISTINCT ClientID) clients FROM Entry GROUP BY codes ORDER BY Codes 进行更新

尝试

wp_tickets

希望这对您有帮助

答案 1 :(得分:0)

可能需要内部联接进行更新

    UPDATE wp_tickets_regenerated a 
    INNER JOIN wp_tickets b a.lottery_id = b.lottery_id
    SET a.user_id = b.user_id,
        a.full_name = b.full_name,
        a.answer_id = b.answer_id,
        a.order_id = b.order_id

(不使用更新中的...)