Jump to content

Recommended Posts

i have a query that uses the union command with 2 tables.  when i am printing out the values from the query in a table, i would like to print the table name that corresponds to the row/record i'm printing.

 

i traverse through the result with:

 

while ($field = mysql_fetch_field($result))
        {  //print table headers aka column names }

while ($row = mysql_fetch_assoc($result))
{
     print "<tr>";

         foreach ($row as $col=>$val)
          {    //print each value in a <td> tag   }

     print "</tr>";
}

 

 

 

How do i know what table the specific row i'm on is from?

 

Thanks!!

Link to comment
https://forums.phpfreaks.com/topic/68643-solved-show-table-name-on-union-query/
Share on other sites

SELECT 'foo' AS tablename, foo.* FROM foo
UNION ALL SELECT 'bar' AS tablename, bar.* FROM bar

 

Normally union will eliminate table names, so you need to record them yourself.  UNION ALL does not eliminate duplicates, which is what you want in this case, because duplicates cannot exist when you've added the "foo" and "bar" column.

Perhaps i worded my original question poorly...

 

I'm ok with the SQL and the Union statement.  That's no problem.

 

My question:  when i am traversing through my query result and printing each record as a row in a table(as in the code snippet i provided), how do i know which table the particular row i'm printing belongs to? (because i use an 'order by' statement in my SQL query, which mixes up the rows between tables, so it's not just printing one table, then the next, etc all in order).

 

Thanks

 

I think you read the reply poorly.

 

 

I probably did.  I'll give my table structure here and my previous sql query and let me know what to change since i don't totally understand what's going on in btherl's post (sorry, bare with the newb here!!).

 

I have 2 tables with the same structure named 'schedule' and 'newsched'

 

Column names (in order):  Date, Time, Away, Home, Field, Ump1, Ump2, Status

 

Here is my sql query that i used before that is not currently doing what i want it to be doing.  I'm trying to find all games that a particular umpire is scheduled to be on in 2 different leagues, which each have their own table (schedule and newsched, respectively):

 

"select * from schedule where ump1 = '$name' or ump2 = '$name' UNION ALL select * from newsched where ump1 = '$name' or ump2 = '$name' order by date"

 

 

So is there an object that i can reference to give me the name of the table as i'm printing it out (with code in original post), or how does this work?

 

 

Thanks a billion once again!

What btherl said is to include the table names as literals in the query

 

"select 'schedule ' as tablename, schedule.*

from schedule

where ump1 = '$name' or ump2 = '$name'

UNION ALL

select 'newsched' as tablename, newsched.*

from newsched

where ump1 = '$name' or ump2 = '$name'

order by date"

 

Now each row contains the the tablename from which it originated.

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.