I have query that INSERTS both explicit values and SELECTd content. I’m also doing basic incrementing.
INSERT INTO `table` (`myID`, `myVal1`, `myVal2`) SELECT `myID` + 1, 'explValHere', 'otherValThere')
FROM `table` ORDER BY `myID` DESC LIMIT 0,1
I am doing this as the table has multiple id’s and incrementing within a specific column. So I can’t, as you would first say, use auto incrementing and insert_id.
The problem of course is the insert doesn’t return the select, but can it? Is there a way of running this insert query and returning any of the result?
Since your query has a
LIMIT 1you could store the “result” in a session/user-defined variable. Still two queries but reentrant; each connection is its own session.the “first” record in tbl2 having x=0 is (myID=3,x=0) and the script prints
4.Other than that and stored procedures et al there’s (to my knowledge) nothing like SQLServer’s OUTPUT or postgresql’s RETURNING clause for MySQL.