Forum Moderators: open

Message Too Old, No Replies

Connection/Join Two Tables Simple Select

         

brandon0401

4:44 pm on Dec 12, 2007 (gmt 0)

10+ Year Member



Hey guys having a hard time getting this down.

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.

coopster

6:34 pm on Dec 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sounds like a good place to use a UNION query.

brandon0401

7:13 pm on Dec 12, 2007 (gmt 0)

10+ Year Member



thanks, got it working...

one questions

lets say your iterating trough the results using a while loop....how would you do a if statement to check what table the results were for, so you could modify the link?

can you tell what table the result is from on a record?

Thanks.

phranque

12:26 am on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could try something like this:
(select *,'links' as tablename from links) union (select *,'stories' as tablename from stories)

brandon0401

1:25 am on Dec 13, 2007 (gmt 0)

10+ Year Member



how would I identify which table the record was in then?

phranque

2:30 am on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you would be looking at the value of the "tablename" column/alias.

brandon0401

6:37 am on Dec 13, 2007 (gmt 0)

10+ Year Member



so

select tablename from link_tablename;

?

thanks

phranque

12:42 pm on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



(select *,'link_tablename' as tablename from link_tablename) union (select *,'story_tablename' as tablename from story_tablename)

brandon0401

7:13 am on Dec 14, 2007 (gmt 0)

10+ Year Member



Ok I guess what im trying to ask is

$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.

ZydoSEO

7:39 am on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I'm not mistaken... the data you need is there already to determine which table the row was retrieved from. It looks like the query:

(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.

brandon0401

10:49 pm on Dec 14, 2007 (gmt 0)

10+ Year Member



thanks guys got it all working perfectly! Greatly appreciate it!