Current location: Hot Scripts Forums » Programming Languages » PHP » Select results from 2 tables


Select results from 2 tables

Reply
  #1 (permalink)  
Old 05-12-05, 10:13 AM
Mortal_Takeda Mortal_Takeda is offline
Newbie Coder
 
Join Date: May 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Select results from 2 tables

I'm trying to make a search function which should get the results from 2 tables. I found more post of people with similar problems, but those were different from mine. Here's what I'm trying to do:
PHP Code:

$query mysql_query("SELECT * FROM table1 LEFT JOIN table2 USING (post) WHERE post LIKE '$keywords'");


while ( 
$results mysql_fetch_array($query) )
{
    echo 
$results['title']."<br>";

This would return the following error: Column 'title' in field list is ambiguous

Why? Because both tables have a column with the same name (title). But I just want MySQL to treat those ambiguous columns as one column.

So I've read a few times through this page, but I just can't fix this. Any help appreciated.
Reply With Quote
  #2 (permalink)  
Old 05-12-05, 10:30 AM
LordDaimos LordDaimos is offline
Newbie Coder
 
Join Date: Apr 2005
Location: Vännäs, Sweden
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
You have to specify the fieldnames as `table1`.`title`, `table2`.`title` so it can seperate them. You can't do select * if you are joining tables with same fieldnames.

/ Karl
http://www.crazybeavers.se/
Reply With Quote
  #3 (permalink)  
Old 05-12-05, 11:01 AM
Mortal_Takeda Mortal_Takeda is offline
Newbie Coder
 
Join Date: May 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
But if I would specify the tables like you said, the above example would be useless. Is there something else I could do? I want the script to list the results of both tables.
Reply With Quote
  #4 (permalink)  
Old 05-12-05, 02:39 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

If the only field you want to retrieve from each table is "title", then I suggest that you explicitly say that, instead of retrieving every single field with * for performance and possibly other reasons.

Anyways, I think you will find SQL keyword UNION useful in your case, provided that these two fields have the same data type (and length). Check this out. In a word, I'd say JOIN joins horizontally, but UNION goes vertically, but I strongly recommend that you check out the following:

MySQL Doc
http://dev.mysql.com/doc/mysql/en/union.html

Or W3Schools (easier, but not MySQL official):
http://www.w3schools.com/sql/sql_union.asp

HTH.
__________________
Blavv =|
Reply With Quote
  #5 (permalink)  
Old 05-13-05, 05:00 AM
Mortal_Takeda Mortal_Takeda is offline
Newbie Coder
 
Join Date: May 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot blaw, that was exatly what I was looking for. The search function now gives the results from both tables

But I still have one question. I also want the results to show from which table the result came from. Is there a way to do this?
Reply With Quote
  #6 (permalink)  
Old 05-13-05, 05:19 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

I'm glad that you got it.

To show (or let PHP know) which table that "title" field belongs to, what I would usually do is simply create a flag field. For instance,

PHP Code:

SELECT title'A' AS 'from_table'

FROM TableA
UNION 
(
    
SELECT title'B'
    
FROM TableB

Now all of the records from TableA has a value "A" and the ones from TableB has a value "B" in the derived field named "from_table", so when you are looping through the results, check this flag to see which value is from which "title" table.

HTH.
__________________
Blavv =|
Reply With Quote
  #7 (permalink)  
Old 05-13-05, 07:28 AM
Mortal_Takeda Mortal_Takeda is offline
Newbie Coder
 
Join Date: May 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks blaw, it worked! Now I finally have my search function working for my website, thanks for your help guys!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to weigh mysql select results bugalyzer PHP 1 02-28-05 12:38 PM
select from 2 tables?.. kasper PHP 3 07-09-04 06:34 AM
PHP, MySQL Beginner... Help Needed Displaying Results db3db3 PHP 1 05-12-04 01:24 AM
Passing results of a form select to another form?? NCC1701 PHP 1 04-29-04 11:54 AM
[newbie] Displaying Database results in tables omgwtf PHP 4 03-21-04 07:15 PM


All times are GMT -5. The time now is 06:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.