是否可以将这些表合并为一个数组值

时间:2016-06-10 11:47:29

标签: mysql sql phpmyadmin

我正在尝试将来自三个不同表格的一些数据合并到一个可用的数组中,但我仍然坚持如何处理它。

首先让我解释一下结构:

我的'主'表snm_content

我只想在这里使用id

enter image description here

第二个表'snm_fieldsandfilters_connections':

element_id在这里很重要,它与下一个表格中的id具有相同的价值

enter image description here

第三张表'snm_fieldsandfilters_elements':

此处item_id非常重要,item_id与'snm_content'中的id值相同

enter image description here

例如,我添加了另一篇文章(snm_content中的项目)id 4,然后另一行将添加到'snm_fieldsandfilters_connections'中element_id 22,并且还会添加另一行使用id 22和item_id 4来'snm_fieldsandfilters_elements'。

如何从'nm_fieldsandfilters_connections'中获取element_id以匹配'snm_fieldsandfilters_elements'中的id?然后将'snm_content'中的id与来自'snm_fieldsandfilters_elements'的item_id进行比较。

我希望我解释得很好。

实现这一目标的最佳方法是什么? (我的意思是SQL明智)

感谢答案,我想出了如何做到这一点。

解决方案:

select ct.*, fe.*, cn.*
from snm_content ct
inner join snm_fieldsandfilters_elements fe on fe.item_id = ct.id
inner join snm_fieldsandfilters_connections cn on cn.element_id = fe.id
WHERE ct.catid IN ('10')

1 个答案:

答案 0 :(得分:1)

听起来你在询问关于组合来自不同表格的数据。这样做的方法是使用SQL JOIN。如果您不知道如何使用它们,我建议您阅读它们。与此同时,作为一个例子,如果我已正确理解要求,这应该可以解决您的具体问题:

select ct.id 
from snm_content ct
inner join snm_fieldsandfilters_elements fe on fe.item_id = ct.id
inner join snm_fieldsandfilters_connections cn on cn.id = fe.element_id
相关问题