Page 1 of 1
MySQL JOIN without using ON when no rows match?
Posted: Mon Mar 23, 2009 12:34 pm
by JAB Creations
I want to use the language row from my cms_sections table however no column/rows will match in my blog_threads table to do a successful JOIN. Since I only need that one row from cms_sections but I need two rows from blog_threads I can't do a UNION either.
So is there a way I can get the three columns (one column from cms_sections and two columns from blog_threads) in a single MySQL query?
Re: MySQL JOIN without using ON when no rows match?
Posted: Tue Mar 24, 2009 1:41 pm
by waylon999
I'm not sure if this is what you are after, but say you have 2 tables:
users {id, name} -> (1,joe), (2,sue)
providers {id, name} -> (1,verizon), (2,cingular)
then you can query the db on these 2 tables that don't have a common column:
Code: Select all
select p.name, u.name from providers p, users u where p.id = 1 and (u.id=1 OR u.id=2)
which would return 2 rows:
verizon, joe
verizon, sue
Is that close to what you are trying to accomplish?
Re: MySQL JOIN without using ON when no rows match?
Posted: Tue Mar 24, 2009 11:48 pm
by JAB Creations
AWESOME!
That worked great!
Code: Select all
SELECT cs.section_name, ua.user_username FROM cms_sections AS cs, user_accounts AS ua WHERE cs.id = 1 AND (ua.user_id=1 OR ua.user_id=2)
I see the you can choose multiple tables by using a comma...it's one of those odd ball things that aren't covered by tutorials. Dam...once I get really good with databases I'm
so going to write a tutorial or something and include this sort of stuff!
I had to add AS (alias) to get your example to work though thankfully I was already aware of that. But yeah thanks a ton again for helping me out with that!
