Make Insert with select

Asked

Viewed 49 times

0

I have a table called label, with nameTag varchar and Quant int 11.

When I insert in the label table, I have to increase the total quantity registered in the bank +1. But I don’t know how to do this without having to do another query and then add through php. You can do this without the whole process of another query with PHP?

$querysql = $con->prepare("INSERT INTO rótulo(nomeTag, quant) VALUES(?,?)");
$querysql->bindValue(1, $nomeTag);
$querysql->bindValue(2, "SELECT quant+1 FROM rótulo"); // nessa linha qe eu ñ sei
$querysql->execute();

Can you do something like that? Can someone help me?

  • it is necessary to first select the table and then do Insert

  • Is there no other way? Similar to the example I asked?

  • I’m saying I think it’s more viable, but you can expect someone more experienced, but I’ve never used it and I haven’t seen it like this

  • I believe that when you enter the record you already have an extra record, that is, +1, in your database. see if your database has id <auto increment>

  • insert select has another syntax.

  • It is to make a counter of how many posts you have with a certain label, and one can be auto-increment

  • 1

    Do you really need to save that amount? Don’t you want to make one count() when recover this information? this situation seems the same as recording age as a int there every year you increase :)

  • 1

    This nametag will be repeated anyway?

  • I would use a TRIGGER AFTER UPDATE https://www.techonthenet.com/mysql/triggers/after_update.php

  • It does not make sense what you are trying to do, or I did not understand your question, because it is only you create a field with primary key and auto_increment that will do this for you automatically, Ref: https://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.