Jump to content

Determine Whether Mysql Join Result Is Either From The 'kick' Or 'ban' Table


Classico

Recommended Posts

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'");

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'

 

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'

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.