• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

mysql insert if not in another table

I have table A and table B

If an employee number exists in table B, but does not exist in table A, I want to insert  the employee number, the date and .01 into fields  'emp', 'date', and 'sales' of table A

What is the best way to do this with mysql in a PHP script?
0
birwin
Asked:
birwin
1 Solution
 
Aaron TomoskyTechnology ConsultantCommented:
One SQL query:
Insert into a (eid,date,sales)
Select eid, now(), '.01' from b where eid not in (select eid from a)
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
i would use a syntax like below ;

SELECT B.emp_id FROM tableA as B, tableB as A WHERE B.emp_id <>A.emp_id AND B.emp_id='1234'

or better ;

SELECT emp_id FROM tableB WHERE emp_id  NOT IN ( SELECT emp_id FROM tableA )
0
 
birwinAuthor Commented:
Worked perfectly, first try. Genius!

Thank you for that great code.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now