Jump to content

mysql_field_table() Not Working With My Union Query


JustinK101

Recommended Posts

Hello I am trying to retrieve the table name of a given row returned. I am using the following code:

$sql = "(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";
$result = mysql_query($sql) or die(fatal_error_alert(mysql_error(), $sql));
echo "The Table Is: " . mysql_field_table($result, 0);

Nothing is being returned though from the mysql_field_table() function call. Does this function work with UNION mysql operations? Thanks.
Link to comment
Share on other sites

I got the fix, thought kind of dirty. Simply put the table name in the select that why you reference it for every row.

(SELECT 'employee', id, first_name, last_name, job_title_occupation, current_clock_status, last_succ_login FROM employees) UNION ALL (SELECT 'inactive_employee', id, first_name, last_name, job_title_occupation, current_clock_status, last_succ_login FROM inactive_employees) ORDER BY last_name ASC
Link to comment
Share on other sites

I think that's the best solution, dirty though it is.  Once you've unioned two result sets, it's impossible to tell which set the data originally came from unless you add a marker like you have there.

Another option would be to do two selects and merge them and sort them in php.  Merging and sorting is usually easier in sql though :)
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.