0

I have the following Mysql-table:

+-------+--------+------------+----+-------+
| Name  | Number | Department | id | JobID |
+-------+--------+------------+----+-------+
| Sven  |   2204 | Marketing  | 10 |   111 |
| Peter |   2304 | IT         | 20 |   222 |
| Bjorn |   4409 | IT         | 30 |   333 |
+-------+--------+------------+----+-------+

I get the three columns: Name, Number, Department from a system where I don't have the id and need to perform something in my php script.

Now I would like to performa an insert if there is a new record. If there is an existing record I would like to perform an update, if something changed like Name, Number or Department.
For example, if Number changes it should compare Name and Department and then change number. If Departmend changes it should compare Name and Number and then change Department and so on.

The problem is, that I can not use insert...on duplicate key, because I don't get the primary key.

If I use Replace Into it deletes me also the entry for JobID. Is there a solution how to perform a sql that it will insert and also update if there is now entry? Or something that can do the trick?

Thanks for your help!

triplus
  • 91
  • 5
  • 17
  • 3
    I'd add a primary key, you really need it for this - have a look here http://stackoverflow.com/questions/9070764/insert-auto-increment-primary-key-to-existing-mysql-database – StudioTime Mar 04 '15 at 16:01
  • @Fred -ii-: I use an Excel File, with an Excel Reader. Normaly the primary key would be the JobiD, however HR decided instead of deleting the employees the JobiD changes.... . However I just had the idea, if the JobiD doesn't match it should perform an update on the condition of the Name, because the combination of leaving the company and changed the name is not possible. Maybe this works.... . – triplus Mar 04 '15 at 16:05
  • Primary keys aren't my *forté*. Best I know would be to first query to see if it exists, if not then insert. – Funk Forty Niner Mar 04 '15 at 17:08
  • 1
    *HR decided instead of deleting the employees the JobiD changes* HR could not decide how you must design your sql tables and manipulate with it. that is out of their qualifications. so they only can ask how do they want data be presented. and you must provide the solution how to get the required result. – Alex Mar 04 '15 at 17:23

0 Answers0