문제

I have the following tables:

+--------+    +-------+    +----------+    +-------+
| blogs  |    | users |    | blogtags |    | tags  |
+--------+    +-------+    +----------+    +-------+
| bid    |    | uid   |    | bid      |    | tid   |
| uid    |    | name  |    | tid      |    | tag   |
| title  |    +-------+    +----------+    +-------+
+--------+

Unfortunately I have a restriction whereby my MySQL server is separate to my web server. As such I want to minimise database calls.

I want to get a result like this:

bid | title | users.name | tag1 tag2 tag3 |

With the tags concatenated into one string.

I guess first question - is this even possible. Then could the kind SO community give me some guidance on how to do this.

I have been working all day and my brain is spinning even trying to work this out. I know I'm going to need some joins, a group-concant and probably a sub query but getting lost thinking about it. Thanks in advance

도움이 되었습니까?

해결책

select bid, title, users.name, group_concat(tags.tag)
from blogs inner join users on blogs.uid=users.uid
inner join blogtags on blogs.bid=blogtags.bid
inner join tags on tabs.tif=blogtags.bid
group by bid, title, users.name
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top