SELECT * from tb1, but one from tb2???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

SELECT * from tb1, but one from tb2???

Post by seodevhead »

Hey guys, I want to construct a MySQL SELECT query that grabs columns from two tables (let's call them table1 and table2). But the thing is, I want to select ALL (*) of table1, but only one column from table2. So I made a query like this:

Code: Select all

SELECT a.*, b.column1 FROM table1 AS a, table2 AS b
I've tested this and it seems to work just fine. But for some reason it didn't look like kosher to me, and thought I'd ask if this is perfectly acceptable to do. I looked it up in the SELECT SYNTAX mysql documentation, but didn't see an example like this anywhere. So I thought I'd ask first. Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: SELECT * from tb1, but one from tb2???

Post by Christopher »

Yes, that's fine. But you might want to do a proper join if the two tables are linked by a key.
(#10850)
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Re: SELECT * from tb1, but one from tb2???

Post by seodevhead »

arborint wrote:Yes, that's fine. But you might want to do a proper join if the two tables are linked by a key.
Well I don't use a JOIN if that's what your asking. There is a linkage between table1 and table2. This is what I have:

Code: Select all

SELECT a.*, b.column1 FROM table1 AS a, table2 AS b WHERE a.stuff_id=b.stuff_id AND a.stuff_id=5
Is this what you are referring to? Because I'm not sure how I could change the above to use a JOIN???
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: SELECT * from tb1, but one from tb2???

Post by Christopher »

Code: Select all

SELECT a.*, b.column1 FROM table1 AS a LEFT JOIN table2 AS b ON a.stuff_id=b.stuff_id WHERE a.stuff_id=5
(#10850)
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Re: SELECT * from tb1, but one from tb2???

Post by seodevhead »

Other than your query doesn't fail if b.column1 doesn't exist for a record, is there a reason why you went with a JOIN as opposed to my initial way?
Post Reply