[thelist] MySQL: How to see which table it was, after UNION

Matt Warden mwarden at gmail.com
Thu Jul 8 21:08:30 CDT 2004


On Thu, 08 Jul 2004 22:06:47 +0200, Mike <evolt at muinar.com> wrote:
> $query = "
> (select id as homeId,title as homeTitle,teaser as homeTeaser,date as
> homeDate from muinar_webnews order by homeDate desc limit 1) union
> (select id as homeId,title as homeTitle,subtitle as homeTeaser,date as
> homeDate from kmu_tips order by homeDate desc limit 1)
> order by homeDate desc";
> $result = mysql_query( $query );
> $number = mysql_numrows( $result );
...
>          $homeTable = mysql_field_table( $result,$i );
...
> $homeTable always shows the same value, i. e. the first of the two tables,
> which is wrong. What's wrong with using mysql_field_table here? 

Well, I am not a PHP programmer (IANAPP?), but according to php.net
that second argument is a field offset, /not/ a row offset. You seem
to be passing it the counter you are using to loop through rows.

>Is there
> a better way to identify the tables?

Yes. Add a constant in your union SQL.


$query = "
(select id as homeId,title as homeTitle,teaser as homeTeaser,date as
homeDate, 1 as tableid
from muinar_webnews 
order by homeDate desc limit 1)
union
(select id as homeId,title as homeTitle,subtitle as homeTeaser,date as
homeDate, 2 as tableid
from kmu_tips
order by homeDate desc limit 1)
order by homeDate desc";

Obviously, you don't /have/ to use numeric constants. You could just
as easily use the table name itself as a string.

HTH,

-- 

Matt Warden
Berry Neuroscience Lab
Department of Psychology
Miami University



This email proudly and graciously contributes to entropy.


More information about the thelist mailing list