SELECT thread.id AS p_id, post.title, post.content, author.username, author.id AS a_id
FROM thread INNER JOIN author
ON author_id = author.id
ORDER BY thread.created DESC
LIMIT $start, 30
我有一个名为posts,posts_id和tags的表。
如何使用INNER JOINS扩展上述SQL语句以获取与每个帖子相关的所有标记,或者其他什么?
答案 0 :(得分:2)
试试这个(假设带有标签信息的表是标签并且有一个thread_id列):
SELECT thread.id AS p_id,
post.title,
post.content,
author.username,
author.id AS a_id,
GROUP_CONCAT(DISTINCT tag_name ORDER BY tag_name DESC SEPARATOR ',') AS tags
FROM thread INNER JOIN author
ON author_id = author.id INNER JOING TAGS
ON thread.id = tags.thread_id
GROUP BY thread.id
ORDER BY thread.created DESC
LIMIT $start, 30
编辑:移动GROUP上一行。
答案 1 :(得分:0)
SELECT thread.id AS p_id,post.title,post.content,author.username,author.id AS a_id 来自线程 LEFT JOIN作者ON author_id = author.id LEFT JOIN标签ON tags.thread_id = thread.id ORDER BY thread.created DESC LIMIT $ start,30
Sanil