11.12.2013 12:02
Aleks_Str
 
Вопрос по поводу облегчения работы в mysql
Приходится изменять тип чека созданного на вебке кассы.
Делаю так:
select max(id) from trm_out_receipt_header; записал на бумажке id
update trm_out_receipt_header set type=1 where id=ТО_ЧТО_ЗАПИСАЛ_НА_БУМАЖКЕ

А можно-ли сделать так, как в фоксе:
select max(id) from trm_out_receipt_header into ПЕРЕМЕННАЯ;
update trm_out_receipt_header set type=1 where id=ПЕРЕМЕННАЯ;
???

З.Ы. Пока писал вопрос пришло в голову, что можно сделать так:
update trm_out_receipt_header set type=1 where id=(select max(id) from trm_out_receipt_header);

З.З.Ы. Попробовал последнюю идею - не прокатило.
11.12.2013 12:35
OlegON
 
а что именно не прокатило? вроде все правильно написано... мне потестить не на чем :(
11.12.2013 12:45
Aleks_Str
 
ERROR 1093 (HY000): You can't specify target table 'trm_out_receipt_header' for
update in FROM clause

Предполагаю, что не учет какие-то тонкости mysql
11.12.2013 13:10
vdm
 
"Переменные mysql в запросе" гуглятся.

Код:
select @header:=max(id) from trm_out_receipt_header where cash_id=12345;

update trm_out_receipt_header 
set type=1
where cash_id=12345 and id=@header;
11.12.2013 13:30
OlegON
 
Цитата:
Currently, you cannot update a table and select from the same table in a subquery.

а если так?
Код:
update trm_out_receipt_header set type=1 where id=(select id from (select max(id) from trm_out_receipt_header) as x);
UPD:работает, проверил на форумной БД :) вопрос только быстродействия
Код:
mysql> create table trm_out_receipt_header (type int, id int);
Query OK, 0 rows affected (0.03 sec)

mysql> update trm_out_receipt_header set type=1 where id=(select id from (select max(id) from trm_out_receipt_header) as x);
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> insert into trm_out_receipt_header select 2,2;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> update trm_out_receipt_header set type=1 where id=(select id from (select max(id) from trm_out_receipt_header) as x);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> drop table trm_out_receipt_header;
Query OK, 0 rows affected (0.00 sec)
28.06.2014 01:20
nistelroi
 
Есть ещё один рабочий вариант запроса

Код:
UPDATE trm_out_receipt_header h
	JOIN (SELECT MAX(id) AS id FROM trm_out_receipt_header) m
		ON h.id=m.id
	SET h.type=1
30.06.2014 14:08
Павел Сосновских
 
только:
Код:
ON h.cash_id=m.cash_id and h.id=m.id
Часовой пояс GMT +3, время: 06:31.

Форум на базе vBulletin®
Copyright © Jelsoft Enterprises Ltd.
В случае заимствования информации гипертекстовая индексируемая ссылка на Форум обязательна.