Tuesday, 25 April 2017

php - Variable in SQL not working with prepare statement

You can rewrite this query without using a MySQL variable..


INSERT INTO
GSM_Parent (OrderNumber)
values ( (SELECT COALESCE( MAX( OrderNumber ) , 0 ) +1 FROM GSM_Parent) )


Thanks, but I got the following error:#1093 - You can't specify target
table 'GSM_Parent' for update in FROM clause .



Indeed i missed you used the same table twice, the query below should work.


INSERT INTO
GSM_Parent (OrderNumber)
VALUES (
(SELECT OrderNumber FROM (
SELECT
(COALESCE(MAX(OrderNumber), 0) + 1) AS OrderNumber
FROM
GSM_Parent
) AS GSM_Parent
)
)

@Jarth 's query from the comments is also possible then you need to write


INSERT INTO
GSM_Parent (OrderNumber)
SELECT
OrderNumber
FROM(
SELECT COALESCE(MAX(OrderNumber), 0) +1 AS OrderNumber FROM GSM_Parent
) AS GSM_Parent

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...