Would this SQL query be possible? [closed]

-5
$sql = "INSERT INTO USUARIOS (partidas,partidas_ganas,turnos) 
    VALUES ((select partidas FROM usuarios WHERE nombre like='$nombre') + 1,
        (SELECT partidas_perdidas FROM usuarios WHERE nombre LIKE = '$nombre') + 1)";
    
asked by Diego 09.06.2017 в 22:06
source

3 answers

0

If you can because you use sub-queries you just have to assemble your query well if you are going to insert three data you must send the three data. for example:

Suppose we have a table called repres with the same structure as the employees table, and we want to insert in that table the employees that have the title rep sales

INSERT INTO repres SELECT * FROM empleados WHERE titulo = 'rep ventas'

With the SELECT we get the rows corresponding to the employees with title rep sales , and insert them in the table repres . As the tables have the same structure, it is not necessary to put the list of columns and we can use * in the selection list of the SELECT .

Suppose now that the repres table had the following columns numemp , oficinarep , nombrerep . In this case we could not use the asterisk, we would have to put:

INSERT INTO repres SELECT numemp, oficina, nombre FROM empleados WHERE titulo = 'rep ventas'

Or:

INSERT INTO repres (numemp, oficinarep, nombrerep) SELECT numemp, oficina, nombre FROM empleados WHERE titulo = 'rep ventas'
    
answered by 09.06.2017 / 22:25
source
0

It is possible to insert data through these subqueries. But if you look at the VALUES you are only inserting 2 of the 3 columns, games and games_games , leaving out turns strong> so it will not let you perform the insert. To compensate for this, if a value does not go, use a 0.

$sql = "INSERT INTO USUARIOS (partidas,partidas_ganas,turnos) 
        VALUES ((select partidas FROM usuarios WHERE nombre like='$nombre') + 1,
        (SELECT partidas_perdidas FROM usuarios WHERE nombre LIKE = '$nombre') + 1), 0";

I hope my answer will be good, greetings.

    
answered by 09.06.2017 в 22:18
0

You can use an insert into select, 'turns' I do not know its value

"INSERT INTO USUARIOS (partidas,partidas_ganas,turnos) SELECT partidas+1,partidas_perdidas+1,turnos FROM usuarios WHERE nombre like='$nombre'"
    
answered by 09.06.2017 в 22:18