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

Link to comment
Share on other sites

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'

 

Link to comment
Share on other sites

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'

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.