Forum Moderators: open
I have two tables, exact same structure, ie links and stories
I want to do a statement:
select * from bothtables order by hits desc;
So it pulls from both tables, and orders by one column.
Havn't been able to get it, can anyone help me out?
Thanks in advance, appreciate it.
$results=mysql_query( (select *,'link_tablename' as tablename from link_tablename) union (select *,'story_tablename' as tablename from story_tablename) ))
while($results!= EOF){
if(result in list is from table name)
do
}
so wonder how I can identify which table the result is from so I can do logic? Thanks.
(select *,'links' as tablename from links) union (select *,'stories' as tablename from stories)
has added an additional column to the recordset returned... The column that comes back from the DB that is not really a column in either links or stories is named 'tablename'.
You'll access/reference the tablename column in the result set the same way you access the other columns returned by '*' in the select. I'm not really a php guy but once you fetch a row from the result set, you'll likely access them using the column name.
If tablename = 'links' do the code you want for rows from the links table.
If tablename = 'stories' do the code you want for rows from the stories table.