我可以看出为什么这会有问题,但似乎无法像其他人那样解决问题,因此......
我不想使用sub_query来修改票号。不能使用auto_inc已经拥有auto_inc主键..
这是我想做的事情:
UPDATE tickets SET tickets.ticket_number=(
SELECT (MAX(ticket_number)+1) FROM tickets
) WHERE ticket_id=12345;
给我:You can't specify target table 'tickets' for update in FROM clause
谢谢!
答案 0 :(得分:6)
如果我错了,请纠正我,但这是你想要实现的结果吗?:
UPDATE tickets SET tickets.ticket_number=(
SELECT max_ticket FROM (
SELECT (MAX(ticket_number)+1) AS max_ticket FROM tickets ) AS sub_table)
WHERE ticket_id=12345;
答案 1 :(得分:1)
我以前碰过这个。在mySQL中,当您尝试使用同一个表中的值更新表时,您必须创建另一个抽象层。
UPDATE tickets SET ticket_number=(
select t from (
SELECT (MAX(ticket_number)+1) t FROM tickets t1) as t2
) WHERE ticket_id=12345;
答案 2 :(得分:0)
update tickets
set ticket_number= (
select (MAX(ticket_number)+1) from (
select * from ticket
) as x)
where ticket_id=12345;