Insert sql with WHERE NOT EXISTS rule does not work

Asked

Viewed 242 times

0

In this Sert below only works until the first rule of "Where" then no longer works

Example

WHERE descricao ='$ccusto'  // funciona perfeitamente. Mas se uso o where abaixo

WHERE descricao ='$ccusto' AND id_transfer ='$id_transfer' AND id_empresa='$id_empresa')");
                           // já não funciona


echo $id_transfer22 = 555;
echo $id_empresa = 555; 
echo $ccusto = 'obaoba';
echo $id_novo8= 555;
$sql = mysql_query ("INSERT INTO c_custo(id_transfer,id_empresa,descricao,cod) 
SELECT '$id_transfer22','$id_empresa','$ccusto','$cod' 
FROM DUAL
WHERE NOT EXISTS(SELECT descricao,id_empresa,id_transfer FROM c_custo
WHERE descricao ='$ccusto' AND id_transfer ='$id_transfer' AND id_empresa='$id_empresa')");

What could I be doing wrong?

  • Opa, reading again the question I understood what you want: Do INSERT of what comes in SELECT in table c_cost, using Where the first way works and the second way does not and want to correct to get the second way too.

  • Put this query in a variable, echo it on the screen and run it straight to the seat. Try SELECT alone to see if something is coming. My guess is that when you include these more details there is no record in the bank satisfying this query.

  • Hello Putting the query in mysql is working perfectly returns 5 reptilian items, therefore it should not accept the Insert because it already has equal records

  • Check out Mike’s answer in this Soen topic: http://stackoverflow.com/questions/3164505/mysql-insert-record-if-not-exists-in-table

No answers

Browser other questions tagged

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