SELECT inside INSERT PHP Mysql

Asked

Viewed 764 times

1

I have the following code:

$sql = mysql_query("INSERT INTO TB004_PRODUTO_FORNECEDOR ( TB002_CODIGO, TB003_CODIGO )     
                  SELECT TB003_CODIGO, 1 AS Codigo FROM TB003_PRODUTO WHERE TB003_DESCRICAO = '$name_02'
                  VALUES ( '$name_01', Codigo )");

How can I select the product code within Insert for it to just enter the code?

  • If that’s possible, I’d like to know.. rsrs. Just don’t use the mysql_*, because it is outdated. Use the mysqli_* or PDO

  • @Andreicoelho I’ve done in the PDO, but in this case I can’t, as much as I’d like to, I’ll have to msql even.

  • Got it =)... the answer below seems valid

1 answer

4


Hello, I do not know if I understand well, but from what I understand you want to enter the result of a select. If this is it just do +- like this:

INSERT INTO TB004_PRODUTO_FORNECEDOR ( TB002_CODIGO, TB003_CODIGO )     
VALUES ('$name_01',(SELECT TB003_CODIGO FROM TB003_PRODUTO WHERE TB003_DESCRICAO = '$name_02'))
  • Putting the second value in a select is it? Cool.. living and learning

  • Hello if you want to enter the second value (TB003_CODIGO), but only has the name to search then it would be just like that.

  • Got it. Interesting...

  • Worse I had done it, but something else was wrong and it didn’t work, I followed the way I had done in PDO $stmt = $this->conn->prepare("INSERT INTO tasks (Project, CompanyFantasy, Priorities, Delivery, Attachment, ByUser, Systems, OrderTask, Subject) VALUES 
 (:project, :companyfantasy, :priorities, :delivery, :attachment, :byuser, :systems, (select OrderTask from (SELECT MAX(OrderTask)+1 as OrderTask FROM tasks) X), :subject)");, slightly different logic because I am using MAX()+1.

Browser other questions tagged

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