LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2004, 08:39 PM   #1
spoody_goon
Member
 
Registered: Sep 2003
Location: Michigan USA
Distribution: Mandrake, DamnSmallLinux, VectorLinux
Posts: 416

Rep: Reputation: 30
sql query redundant returns


I need help with a mysql query I am not getting the results I want.
I have two tables
booths(booths , dealer_id)
dealer(dealer_id , first_name, last_name, phone, email)

booths.dealer_id and dealer.dealer_id are not dependant on each other.
In other words there can be a booths with out a dealer_id.

I a query to get all the booths and where there are dealer that goes with it I want to print out that info too.

e.g.
booths1, dealer1, dealer name
booths2, dealer2, dealer name
booths3, NULL
booths4 NULL

I don't mind doing this with two queries I just don't know how to do this without getting redundant infor.
My current queries are:
SELECT booths.booths, dealer.dealer_id, dealer.first_name, dealer.last_name FROM booths,dealer WHERE dealer.dealer_id=booths.dealer_id
and
SELECT * FROM booths

This give redundant info like this:
booths1, dealer1, dealer name
booths2, dealer2, dealer name
booths1, dealer1
booths2, dealer2
booths3, NULL
booths4 NULL

Thanks for your help
 
Old 03-19-2004, 04:28 AM   #2
vi0lat0r
Member
 
Registered: Aug 2003
Location: Lewisville, TX
Distribution: Kubuntu
Posts: 295

Rep: Reputation: 30
After selecting them all you can assort them into an array... and then do something like...

Code:
echo $i[1];
echo $i[3];
echo $i[5];
 
Old 03-19-2004, 08:28 PM   #3
spoody_goon
Member
 
Registered: Sep 2003
Location: Michigan USA
Distribution: Mandrake, DamnSmallLinux, VectorLinux
Posts: 416

Original Poster
Rep: Reputation: 30
Thanks vi0lat0r but I was hoping for something in a sql query language solution. I will try a couple other ideas and if I come accross something worth while I will post it.
Any one else have an idea?
 
Old 03-19-2004, 09:11 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You need an outer join:
Code:
mysql> select t1.booths, t2.dealer_id, t2.first_name from booths t1 left outer join dealers t2 using (dealer_id);
+---------+-----------+------------+
| booths  | dealer_id | first_name |
+---------+-----------+------------+
| booths1 | dealer1   | dealer1    |
| booths2 | dealer2   | dealer2    |
| booths3 | NULL      | NULL       |
+---------+-----------+------------+
3 rows in set (0.00 sec)
 
Old 03-19-2004, 09:44 PM   #5
spoody_goon
Member
 
Registered: Sep 2003
Location: Michigan USA
Distribution: Mandrake, DamnSmallLinux, VectorLinux
Posts: 416

Original Poster
Rep: Reputation: 30
Excellent crabboy thanks for the sql lesson!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL query performance smaida Programming 6 06-08-2005 09:22 AM
SQL query help pls. vickr1z Programming 8 10-18-2004 11:25 PM
Massive SQL Query patpawlowski Programming 7 03-05-2004 04:24 PM
SQL Query question oulevon Programming 7 01-16-2004 01:50 AM
SQL query, comparing tables ngomong Programming 3 07-07-2002 07:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration