Classico Posted November 29, 2012 Share Posted November 29, 2012 I've ran into a small problem. I have 2 tables called 'kicks' and 'bans', and I have created a query that selects all the data from both of them. On my webpage, I have a table column called 'Type', and I'm wanting to display 'Ban' if it's from the ban table, and 'Kick' if it's from the kick table. Is there a way to echo words out based on what database table the values are coming from? Here's my query if needed. $query = mysql_query("SELECT * FROM bans WHERE player = '$search' union all SELECT * FROM kicks WHERE player = '$search'"); Quote Link to comment https://forums.phpfreaks.com/topic/271361-determine-whether-mysql-join-result-is-either-from-the-kick-or-ban-table/ Share on other sites More sharing options...
kicken Posted November 29, 2012 Share Posted November 29, 2012 Just add a static column to the select list. Also, don't use SELECT *, especially with a union. If you ever add an additional column to one of those tables, or the column order does not match exactly the whole query will break. List out the specific columns you need. SELECT 'ban' as type, col1, col2, col3 FROM bans WHERE player = '$search' union all SELECT 'kick' as type, col1, col2, col3 FROM kicks WHERE player = '$search' Quote Link to comment https://forums.phpfreaks.com/topic/271361-determine-whether-mysql-join-result-is-either-from-the-kick-or-ban-table/#findComment-1396228 Share on other sites More sharing options...
Classico Posted December 6, 2012 Author Share Posted December 6, 2012 Sorry for the late reply. And sorry for sounding like this... But what do you mean by 'add a static column to the select list'? I've done what you said, and to only use what columns I need, and have this so far. SELECT 'ban' as type, time, player, banned_by, reason FROM bans WHERE player = '$search' union all SELECT 'kick' as type, time, player, kicked_by, reason FROM kicks WHERE player = '$search' Quote Link to comment https://forums.phpfreaks.com/topic/271361-determine-whether-mysql-join-result-is-either-from-the-kick-or-ban-table/#findComment-1397848 Share on other sites More sharing options...
kicken Posted December 6, 2012 Share Posted December 6, 2012 SELECT 'ban' as type,... 'ban' as type is a static column. Rather than referencing any actual column in your table, it will always use the static string 'ban' as the value for that column in the results. Quote Link to comment https://forums.phpfreaks.com/topic/271361-determine-whether-mysql-join-result-is-either-from-the-kick-or-ban-table/#findComment-1397931 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.