dc_jt Posted August 22, 2007 Share Posted August 22, 2007 Hi my code is below: I am returning all fields from news and from regional, however I want to determine where the fields have come from when I ouput the data, therefore I need to return the table name with the fields. Is this possible? Something like SELECT *, tablename from news...??? $sSql = "SELECT * from news WHERE publish_date <= '$now' AND hide = '0' UNION SELECT * from regional WHERE publish_date <= '$now' AND hide = '0' ORDER BY publish_date DESC LIMIT 30"; Quote Link to comment https://forums.phpfreaks.com/topic/66133-returning-table-name-as-well-as-fields-from-that-table/ Share on other sites More sharing options...
vijayfreaks Posted August 22, 2007 Share Posted August 22, 2007 Hi.. May I know what you want to do with this.. yea you can do.. like this.. select col1 as col1_table1,col2 as col2_table1 from table1 Regards, Vijay Quote Link to comment https://forums.phpfreaks.com/topic/66133-returning-table-name-as-well-as-fields-from-that-table/#findComment-330791 Share on other sites More sharing options...
dc_jt Posted August 22, 2007 Author Share Posted August 22, 2007 Hi I dont think you quite understand what I mean. I know how to get all the columns but I want to return the table NAME (news and regional) as well as the columns. Therefore, when each column is returned as: id, title, content id, title, content it will be returned as news, id, title, content regional, id, title, content So when it is ouput I can see which colums have come from which table. Any idea?? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/66133-returning-table-name-as-well-as-fields-from-that-table/#findComment-330795 Share on other sites More sharing options...
beboo002 Posted August 22, 2007 Share Posted August 22, 2007 for my point of view plz dont use select * from table name . u use select columnname1 AS tablename.columnname1,columnname2 AS tablename.columnname2 from table name Quote Link to comment https://forums.phpfreaks.com/topic/66133-returning-table-name-as-well-as-fields-from-that-table/#findComment-330821 Share on other sites More sharing options...
chocopi Posted August 22, 2007 Share Posted August 22, 2007 to get the tablenames use mysql_list_tables and then just use some sort of regex to get the table you want Quote Link to comment https://forums.phpfreaks.com/topic/66133-returning-table-name-as-well-as-fields-from-that-table/#findComment-330846 Share on other sites More sharing options...
sasa Posted August 22, 2007 Share Posted August 22, 2007 try $sSql = "SELECT *, 'news' as table_nam from news WHERE publish_date <= '$now' AND hide = '0' UNION SELECT *, 'regional' as table_nam from regional WHERE publish_date <= '$now' AND hide = '0' ORDER BY publish_date DESC LIMIT 30"; Quote Link to comment https://forums.phpfreaks.com/topic/66133-returning-table-name-as-well-as-fields-from-that-table/#findComment-330912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.