php中的内连接 [英] Inner joins in php

查看:24
本文介绍了php中的内连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 的表.

I have a table called posts, posts_id and tags.

如何使用 INNER JOINS 或其他方式扩展上述 SQL 语句以获取与每个帖子相关的所有标签?

How can I extend the above SQL statement to get all the tags related to each post using INNER JOINS,...or something else?

推荐答案

试试这个(假设带有标签信息的表是标签并且有一个 thread_id 列):

Try this(assumed the table with tag info to be tags and having a thread_id column):

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 向上移动一行.

Moved GROUP By up one line.

这篇关于php中的内连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆