开发者

mysql select multiple rows in join

开发者 https://www.devze.com 2022-12-28 11:38 出处:网络
I have a simple mySQL problem-- I have two tables, one is a user\'s table, and one is a photos table (each user can upload multiple photos).

I have a simple mySQL problem--

I have two tables, one is a user's table, and one is a photos table (each user can upload multiple photos).

I'd like to write a query to join these tables, so I can pull all photos associated with a user (up to a certain limit).

However, when I do something obvious like this:

SELECT *.a, *.b FROM user_table a
 开发者_如何学Go JOIN photos_table b ON a.id = b.userid

it returns

a.id, a.name, a.email, a.address, b.id, b.userid, b.photo_title, b.location

but it only returns a single photo. Is there a way to return something like:

a.id, a.name, a.email, a.address, b.id, b.userid, b.photo_title, b.location, b.id2, b.photo_title2, b.location2 etc. . .

for a given LIMIT of photos?

Thanks for any ideas.


This is two separate types of data so it is best done with two separate queries:

SELECT * FROM user_table WHERE userid = @userid;
SELECT * FROM photos_table WHERE userid = @userid;

Combining it all into one mega-row is not the SQL way and is just asking for problems. You'll probably hit internal limits in the database at some point.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号