Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.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
Php 在3个数组字段中选择3个表1 mysql查询_Php_Mysql_Wordpress - Fatal编程技术网

Php 在3个数组字段中选择3个表1 mysql查询

Php 在3个数组字段中选择3个表1 mysql查询,php,mysql,wordpress,Php,Mysql,Wordpress,首先是我的桌子 我已将产品的格式/模板数据存储在第一个表中。一个格式/模板可以被许多产品使用 Format meta table. Primary Key is format_id. (has some more other columns) +-----------+-------+ | format_id | title | +-----------+-------+ | 1 | f.a | | 2 | f.b | | 3 | f.c

首先是我的桌子

我已将产品的格式/模板数据存储在第一个表中。一个格式/模板可以被许多产品使用

Format meta table. Primary Key is format_id. (has some more other columns)
+-----------+-------+
| format_id | title |
+-----------+-------+
|     1     |  f.a  |
|     2     |  f.b  |
|     3     |  f.c  |
|     4     |  f.d  |
+-----------+-------+
在第二个表中,我存储产品数据。每个产品都有一个唯一的ID,并且只使用一个唯一的格式/模板。此表包含产品价格、产品数量等数据。每个产品的详细信息保存在另一个表中

Product meta table. Primary Key is product_id. (has some more other columns)
+-----------+------------+-------+
| format_id | product_id | title |
+-----------+------------+-------+
|     1     |      1     |  p.a  |
|     1     |      2     |  p.b  |
|     1     |      3     |  p.c  |
|     1     |      4     |  p.d  |
+-----------+------------+-------+
这是我保存产品详细信息的最后一个表。这里的值存储在单元格_id=>值对中

Details table. No primary key. (has some more other columns)
+-----------+------------+---------+-------+
| format_id | product_id | cell_id | value |
+-----------+------------+---------+-------+
|     1     |      1     |    1    |  d.a  |
|     1     |      1     |    2    |  d.b  |
|     1     |      1     |    3    |  d.c  |
|     1     |      1     |    4    |  d.d  |
+-----------+------------+---------+-------+
现在是我想要的结果

$result => ['format_meta']  => ['format_id'] = 1
                            => ['title']     = f.a

           ['product_meta'] => ['format_id']  = 1
                            => ['product_id'] = 1
                            => ['title']      = p.a

           ['details']      => [0]   => ['format_id']  = 1
                                     => ['product_id'] = 1
                                     => ['cell_id']    = 1
                                     => ['value']      = d.a

                            => [1]   => ['format_id']  = 1
                                     => ['product_id'] = 1
                                     => ['cell_id']    = 2
                                     => ['value']      = d.b

                            => [2]   => ['format_id']  = 1
                                     => ['product_id'] = 1
                                     => ['cell_id']    = 3
                                     => ['value']      = d.c

我完全不知道该怎么做

首先,无论您如何编写SQL语句,据我所知和所见,您都将获得您所请求的所有列的一行/数组值;它不会在多维度数组中重新格式化。这是您必须从接收到的SQL响应对象中精心设计的。要获取所有这些列数据,您需要使用连接来构建SQL语句

SELECT f.title as formatTitle, p.*, d.cellid, d.value FROM Product as p JOIN  Format as f ON f.format_id = p.format_id JOIN Details as d ON p.product_id = d.product_id WHERE p.product_id = ?

在此之后,您可以根据结果创建上述数组

您熟悉SQL联接吗?也许可以从搜索开始。如果我这样做
(选择GROUP_CONCAT(cast(CONCAT(d.cell_id,,:',d.cell_value)作为char)分隔符“;”)
作为详细信息表(第三个)然后我要做这句话的6-7部分,因为我必须获取数据,比如
cell\u id=>cell\u value
cell\u id=>cell\u type
等等。这会不会是一个性能问题?如果是这样的话,你能告诉我哪一个会更快-首先向数据库进行3次查询(在第三个表中大约10K行不是很大的查询)或者第二次循环php中的结果这不是一个简单的问题,取决于您的环境,您不应该在这里详细描述。如果数据库服务器与apache服务器的连接非常开放,那么它具有处理能力(aka也不用于您业务中的其他进程)这个动作不是经常执行的,那么可能使用3个单独的查询就可以了。如果不是这样,那么你可能会被迫让PHP做繁重的工作。同样,不一定是一个简单的答案。