Page 1 of 1
SELECT * from tb1, but one from tb2???
Posted: Thu May 01, 2008 4:29 pm
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!
Re: SELECT * from tb1, but one from tb2???
Posted: Thu May 01, 2008 4:35 pm
by Christopher
Yes, that's fine. But you might want to do a proper join if the two tables are linked by a key.
Re: SELECT * from tb1, but one from tb2???
Posted: Thu May 01, 2008 6:19 pm
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???
Re: SELECT * from tb1, but one from tb2???
Posted: Thu May 01, 2008 6:43 pm
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
Re: SELECT * from tb1, but one from tb2???
Posted: Thu May 01, 2008 7:36 pm
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?