Jump to content

MySQL Question - Pulling From Two Tables And Merging


JustinK101

Recommended Posts

Hello, I have two tables which store the exact same fields with the exact same field names. One table is called employees and the other inactive_employees. I need to run a query and pull from employees and inactive_employees and merge the results.

Currently I have the following query:

$sql = "SELECT id, first_name, last_name, job_title_occupation, current_clock_status, last_succ_login FROM employees, inactive_employees ORDER BY last_name ASC";

The problem is that id, first_name, last_name, job_title_occupation, current_clock_status, and last_succ_login is ambigious since they are called the same thing in both tables. How do i pull the following fields from both tables and return one result. Thanks much.
Link to comment
Share on other sites

That syntax will do a join.. I think you want a union.  Try:

[code](SELECT id, first_name, last_name, job_title_occupation, current_clock_status, last_succ_login FROM employees)
UNION ALL
(SELECT id, first_name, last_name, job_title_occupation, current_clock_status, last_succ_login FROM inactive_employees)
ORDER BY last_name ASC[/code]

That will merge the two tables.  "UNION ALL" will not remove any duplicates, which is usually faster.  "UNION" will remove any duplicates which are in both table.

If you want to do some kind of merging between duplicates in the two tables, then you need to do a join instead.. post again and we can help you with that :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.