JustinK101 Posted October 26, 2006 Share Posted October 26, 2006 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 https://forums.phpfreaks.com/topic/25213-mysql_field_table-not-working-with-my-union-query/ Share on other sites More sharing options...
JustinK101 Posted October 26, 2006 Author Share Posted October 26, 2006 Anybody know?Shoot I dont think the UNION works with mysql_field_table(). That sucks, that seems like the entire point of this function, to determine which rows are coming from which table. Any help much appreciated! Link to comment https://forums.phpfreaks.com/topic/25213-mysql_field_table-not-working-with-my-union-query/#findComment-114988 Share on other sites More sharing options...
JustinK101 Posted October 26, 2006 Author Share Posted October 26, 2006 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 https://forums.phpfreaks.com/topic/25213-mysql_field_table-not-working-with-my-union-query/#findComment-115117 Share on other sites More sharing options...
btherl Posted October 27, 2006 Share Posted October 27, 2006 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 https://forums.phpfreaks.com/topic/25213-mysql_field_table-not-working-with-my-union-query/#findComment-115185 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.