Reply to topic
Conditional Joins?
bobum
Elvis Fanatic
Elvis Fanatic

Joined: 16 Nov 2004
Posts: 746
Location: Montgomery, AL
Reply with quote
Ok - this is a good one for you SQL gurus out there.

I have 2 tables with data like such...

Code:

Table1
ID     |     Item
------------------
1      | square
2      | triangle
3      | circle

Table2
ID    |    ItemID     | Color
----------------------------
1     |        2      |  Red
2     |        1      |  Blue
3     |        1      |  Yellow
4     |        2      |  Blue
5     |        3      |  Green


Now what I want to do is list ALL of the items from table1 and *IF* they have a blue color - note that in an extra field, but if they do not have a blue color, just leave that field blank.

Final output should look like :
1 - Square - Blue
2 - Triangle - Blue
3 - Circle

I figture there's a LEFT JOIN in there somewhere...But a straight LEFT JOIN will give me the Red & Yellow items, if I add a WHERE color='blue' then I loose the Circle...

Is there a SQL solution to this? There HAS to be but I can't see it...
cpnet


Joined: 03 Nov 2004
Posts: 135
Reply with quote
I think if you move the "color='blue'" condition into the join, instead of the where, you'll be ok... so something like:
Code:
SELECT
  T1.Id, T1.Item, T2.Color
FROM
  Table1 T1
  LEFT JOIN Table2 T2 ON
    T1.Id=T2.ItemId AND
    T2.Color='blue'


Last edited by cpnet on Wed Dec 29, 2004 8:50 pm; edited 2 times in total
bobclingan
Forum Regular

Joined: 16 Sep 2004
Posts: 271
Location: Abingdon, MD
Reply with quote
Isn't this what you want?

Code:

SELECT t1.id, t1.item, t2.color
FROM table1 t1, table2 t2
WHERE t1.id *= t2.itemID
  AND color = 'Blue'
GROUP BY item,t1.ID, t2.color
ORDER BY t1.ID,t2.color,item
bobum
Elvis Fanatic
Elvis Fanatic

Joined: 16 Nov 2004
Posts: 746
Location: Montgomery, AL
Reply with quote
bobclingan wrote:
Isn't this what you want?

Code:

SELECT t1.id, t1.item, t2.color
FROM table1 t1, table2 t2
WHERE t1.id *= t2.itemID
  AND color = 'Blue'
GROUP BY item,t1.ID, t2.color
ORDER BY t1.ID,t2.color,item


If I use this bob - won't the "AND color = 'blue'" omit the non-blue circle from my list?
bobclingan
Forum Regular

Joined: 16 Sep 2004
Posts: 271
Location: Abingdon, MD
Reply with quote
It should come back with a "NULL" value associated with it.
bobum
Elvis Fanatic
Elvis Fanatic

Joined: 16 Nov 2004
Posts: 746
Location: Montgomery, AL
Reply with quote
BOOYAH:!: Exclamation

That did it!!!!!!

W()()T!

Bob - next time I see ya I owe you a beer.
bobum
Elvis Fanatic
Elvis Fanatic

Joined: 16 Nov 2004
Posts: 746
Location: Montgomery, AL
Reply with quote
Bob - what is the *= doing - I'm not familiar with that is that a shorcut for a join?

THIS is why I LOOOOVE SQL - that one line of code is saving me about 20 DB round trips PER page view now...
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1031
Location: Felton, Delaware
Reply with quote
Many many developers out there stiff themselves by only learning the ins/outs of the scripting language they're using... without ever giving a thought to how it interacts with other applications/services. By allowing SQL to do all that work for you you're saving a TON of time... Very Happy

SQLServer is amazingly powerful... I love working with it.
bobclingan
Forum Regular

Joined: 16 Sep 2004
Posts: 271
Location: Abingdon, MD
Reply with quote
Bob - what is the *= doing - I'm not familiar with that is that a shorcut for a join?


Yes that is another way of writing a left join, I believe it only works in SQL Server though[/quote]
Josh
Forum Regular

Joined: 01 Apr 2004
Posts: 1031
Location: Felton, Delaware
Reply with quote
^^^ I learned something new today Very Happy
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 770
Location: Newark, De
Reply with quote
Dunno if you all noticed, but we now have a MySQL 4.1 server. Wink
bobum
Elvis Fanatic
Elvis Fanatic

Joined: 16 Nov 2004
Posts: 746
Location: Montgomery, AL
Reply with quote
bobclingan wrote:
Yes that is another way of writing a left join, I believe it only works in SQL Server though

I knew there would have to be a LEFT JOIN in there somewhere. I just couldn't figure out how to get the correct clauses in there to get the right output.

In my final SQL Statement I ended up chucking the GROUP BY and added another condition - and the whole thing that WAS taking up about 12 lines of code, and extra loop and 20 or so DB hits is now actually not adding anymore code since it just replaced my initial SELECT, and I am only hitting the DB 1 time.
invertedspear


Joined: 07 Jul 2008
Posts: 1
Location: Phoenix, AZ
Reply with quote
I know this is a really old post but I came up with a solution I think is a little more simple and can handle more situations like where you would want to join on different fields conditionally (what I was researching when I found this forum) and that is to build a couple case statements into your where clause.

WHERE
CASE
WHEN x=y THEN TableA.col1
ELSE TableA.col2
END
=
CASE
WHEN x=y THEN TableB.col1
ELSE TableB.col2
END


I'm not even sure if this would resolve the problem originally submitted, but hopefully will help someone having the problem I was having.
Conditional Joins?
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic