Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从中间表中获取所有值_Mysql_Sql - Fatal编程技术网

Mysql 从中间表中获取所有值

Mysql 从中间表中获取所有值,mysql,sql,Mysql,Sql,我有一个mySQL三表多对多设置和链接表,简化如下: categories category_id category_name categories2entries category_id, entry_id entries entry_id entry_text 我需要返回给定类别名称的所有条目,但我希望包含给定条目所附加到的所有类别的列表。例如,如果搜索的是上瘾,并且条目也列在类别心理健康和年轻人中,我希望每个结果都有一个输出字段,并列出所有三个类别 如何执行此

我有一个mySQL三表多对多设置和链接表,简化如下:

categories
  category_id
  category_name

categories2entries
  category_id,
  entry_id

entries
  entry_id
  entry_text
我需要返回给定类别名称的所有条目,但我希望包含给定条目所附加到的所有类别的列表。例如,如果搜索的是
上瘾
,并且条目也列在类别
心理健康
年轻人
中,我希望每个结果都有一个输出字段,并列出所有三个类别


如何执行此操作?

您需要直接查找以获取条目列表,反向查找以获取每个条目的类别列表。反向查找必须使用其他表副本

选择e.entry\u文本,
集团公司(c2.类别名称)所属
来自类别c1
在c1.category\u id=ce1.category\u id上加入Categories 2入口ce1
连接ce1上的条目e.entry\u id=e.entry\u id
在ce2.entry\u id=e.entry\u id上加入类别2 Entries ce2
在c2.category\u id=ce2.category\u id上连接类别c2
其中c1.category_name=‘category name’
按e.entry\u文本分组
如果你需要为多个类别(甚至可能是所有类别)提供相同的服务,那么

选择c1.category\u名称,
e、 输入文本,
集团公司(c2.类别名称)所属
来自类别c1
在c1.category\u id=ce1.category\u id上加入Categories 2入口ce1
连接ce1上的条目e.entry\u id=e.entry\u id
在ce2.entry\u id=e.entry\u id上加入类别2 Entries ce2
在c2.category\u id=ce2.category\u id上连接类别c2
/*其中c1.category_name IN({category Names list})*/
按c1分类。类别名称,
e、 输入文本
请参见: