Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 07:53 PM   PM User | #1
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 47
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Inner joins

The 'fieldName' table has an ID and DivName for the Division List
The 'members' table has a Division1 and Division2 using the fieldName ID as entries.

My SQL Statement I need to list two columns one for Division1 and Divison2 but when I run this SQL statement, I get two lines for one member.


SELECT DivName, DivName FROM members INNER JOIN fieldName ON members.division1=fieldName.id OR members.division2=fieldName.id WHERE members.division1=3 OR members.division2=6

how to I run this SQL and one entry for each member?
chellert is offline   Reply With Quote
Old 10-11-2012, 12:12 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Use SELECT DISTINCT instead of just SELECT.

CAUTION: That only works if you aren't SELECTing fields from fieldname that would cause multiple distinct rows.

If you are SELECTing only fields from the members table it will work fine.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-11-2012, 04:03 PM   PM User | #3
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 47
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Unfortunately, I need to use the actual values from the fieldNames table to populate my query instead of using numbers that are stored in the members table.

fieldName table
ID, Name
1, Case Track
2, Business History
3, Accounting

members table
ID, Name, Primary, Secondary
1, Chris Ell, 2, 3
2, Bob Frank, 1, 2
3, John Smith, 3, 1

When I query and choose Primary=1 or Secondary=3, I want to see:

ID, Name, Primary, Secondary
2, Bob Frank, Case Track, Business History
1, Chris Ell, Business History, Accounting

What happens with either Select or Select Distinct, I get the following

ID, Name, Primary, Secondary
2, Bob Frank, Case Track, Case Track
2, Bob Frank, Business History, Business History
1, Chris Ell, Business History, Business History
1, Chris Ell, Accounting, Accounting

Last edited by chellert; 10-11-2012 at 04:13 PM.. Reason: more information
chellert is offline   Reply With Quote
Old 10-11-2012, 07:36 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Join the fieldName table on twice instead:
Code:
SELECT m.id, m.name, fn1.name AS "primary", fn2.name AS secondary
FROM members m
INNER JOIN fieldName fn1 ON fn1.id = m.primary
INNER JOIN fieldName fn2 ON fn2.id = m.secondary
WHERE m.primary = 1 OR m.secondary=3
You've kinda used two different structures here, so I just went with what was in the last post.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
chellert (10-11-2012)
Old 10-11-2012, 08:02 PM   PM User | #5
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 47
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Thank you that worked.
chellert is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:02 AM.


Advertisement
Log in to turn off these ads.