MySQL using variables in select

2

I have tried a few tutorials but I do not think there is any expectation on how to declare the variable in select by MySQL,

DECLARE @idPedido INT;
DECLARE @idCombo INT;

SELECT @idPedido = idPedido, @idCombo = idCombo 
FROM Pedidos
WHERE idCombo IS NOT NULL;

SELECT @idPedido;
SELECT @idCombo;
    
asked by anonymous 08.11.2017 / 19:01

2 answers

1

No MySql

Remembering that if SELECT has multiple results, it will only bring the last.

SELECT @idPedido := idPedido, @idCombo := idCombo 
FROM Pedidos
WHERE idCombo IS NOT NULL;

SELECT @idPedido, @idCombo;

If you wanted a predefined variable, use SET :

SET @idPedido = 10;
SELECT @idPedido;
    
09.11.2017 / 12:37
1

You're mixing everything.

To do select do so:

 SELECT idPedido, idCombo 
 FROM Pedidos
 WHERE idPedido = @idPedido AND idCombo = @idCombo

and to do update do so:

 UPDATE Pedidos set idPedido = @idPedido, idCombo = @idCombo 
 WHERE id = 1

To do the select assigning the returned values to the variables do so:

DECLARE @idPedido INT;
DECLARE @idCombo INT;

SELECT idPedido, idCombo into @idPedido, @idCombo
FROM Pedidos
WHERE idCombo = 1;

Attention SELECT has to return 1 and only one line.

    
08.11.2017 / 19:09