尝试这个:
SELECT
t.Post_Name AS Name,
MAX(CASE WHEN t2.meta_key = 'Address' THEN t2.meta_value END) AS 'Address',
MAX(CASE WHEN t2.meta_key = 'Phone' THEN t2.meta_value END) AS 'Phone',
MAX(CASE WHEN t2.meta_key = 'Sex' THEN t2.meta_value END) AS 'Sex',
GROUP_CONCAT(DISTINCT p.Post_Content SEPARATOR ',') AS Photo
FROM Table1 p
INNER JOIN table1 t ON p.parent_ID = t.ID
INNER JOIN Table2 t2 ON t.Id = t2.post_id
GROUP BY p.Post_Name;
这会给你:
| NAME | ADDRESS | PHONE | SEX | PHOTO |
--------------------------------------------------------------------------------------------------
| John Sample | Sample Street,Country | 10000000000000 | Male | image3.jpg,image2.jpg,image1.jpg |
然后使用 PHP,您可以按照您想要的方式显示它们。
更新
如果您想将 的值显示user comment
为注释,您可以这样做:
SELECT
t.Post_Name AS Name,
MAX(CASE WHEN t2.meta_key = 'Address' THEN t2.meta_value END) AS 'Address',
MAX(CASE WHEN t2.meta_key = 'Phone' THEN t2.meta_value END) AS 'Phone',
MAX(CASE WHEN t2.meta_key = 'Sex' THEN t2.meta_value END) AS 'Sex',
GROUP_CONCAT(DISTINCT p.Post_Content SEPARATOR ',') AS Photo,
GROUP_CONCAT(DISTINCT t.post_Content) comment
FROM Table1 p
INNER JOIN table1 t ON p.parent_ID = t.ID
INNER JOIN Table2 t2 ON t.Id = t2.post_id
GROUP BY p.Post_Name;
更新2:
基于更新的样本数据:
SELECT
p.Post_Name AS Name,
MAX(CASE WHEN t.meta_key = 'Address' THEN t.meta_value END) AS 'Address',
MAX(CASE WHEN t.meta_key = 'Phone' THEN t.meta_value END) AS 'Phone',
MAX(CASE WHEN t.meta_key = 'Sex' THEN t.meta_value END) AS 'Sex',
GROUP_CONCAT(DISTINCT c.Post_Content SEPARATOR ',') AS Photo,
GROUP_CONCAT(DISTINCT p.post_Content) comment
FROM
(
SELECT *
FROM Table1
WHERE parent_id = 0
) AS p
INNER JOIN table1 AS c ON p.ID = c.parent_ID
INNER JOIN table2 AS t ON t.post_id = p.id
GROUP BY p.post_Name;
这会给你:
| NAME | ADDRESS | PHONE | SEX | PHOTO | COMMENT |
-----------------------------------------------------------------------------------------------------------------
| John Sample | Sample Street,Country | 10000000000000 | Male | image2.jpg,image1.jpg,image3.jpg | Content Text |
| Mark Sample | Mark Street,Country | 20000000000000 | Male | image3.jpg,image1.jpg,image2.jpg | Mark Text |
更新 2
对于更新的数据,试试这个:
SELECT
t1.*,
t2.meta_value AS 'Thumbnail'
FROM
(
SELECT
p.Post_Name AS Name,
MAX(CASE WHEN t.meta_key = 'Address' THEN t.meta_value END) AS 'Address',
MAX(CASE WHEN t.meta_key = 'Phone' THEN t.meta_value END) AS 'Phone',
MAX(CASE WHEN t.meta_key = 'Sex' THEN t.meta_value END) AS 'Sex',
MAX(CASE WHEN t.meta_key = 'Primary pic' THEN t.meta_value END) AS 'Primary_pic',
GROUP_CONCAT(DISTINCT c.Post_Content SEPARATOR ',') AS Photo,
GROUP_CONCAT(DISTINCT p.post_Content) comment
FROM
(
SELECT *
FROM Table1
WHERE parent_id = 0
) AS p
INNER JOIN table1 AS c ON p.ID = c.parent_ID
INNER JOIN table2 AS t ON t.post_id = p.id
GROUP BY p.post_Name
) AS t1
INNER JOIN table2 t2 ON t1.Primary_pic = t2.post_id
AND t2.meta_key = 'Thumbnail';
这会给你:
| NAME | ADDRESS | PHONE | SEX | PRIMARY_PIC | PHOTO | COMMENT | THUMBNAIL |
----------------------------------------------------------------------------------------------------------------------------------------------------
| John Sample | Sample Street,Country | 10000000000000 | Male | 2 | image3.jpg,image1.jpg,image2.jpg | Content Text | John_thumbnail.jpg |
| Mark Sample | Mark Street,Country | 20000000000000 | Male | 6 | image1.jpg,image2.jpg,image3.jpg | Mark Text | Mark_thumbnail.jpg |
更新 3
对于这个新meta_data
列,您将需要对第二个表再进行一次连接,就像我们对这样的操作所做的primary pic
那样:
SELECT
t1.Name,
t1.Address,
t1.Phone,
t1.Sex,
t1.Photo,
t1.Comment,
t2.meta_value AS 'Thumbnail',
t3.meta_value AS 'meta_data'
FROM
(
SELECT
p.Post_Name AS Name,
MAX(CASE WHEN t.meta_key = 'Address' THEN t.meta_value END) AS 'Address',
MAX(CASE WHEN t.meta_key = 'Phone' THEN t.meta_value END) AS 'Phone',
MAX(CASE WHEN t.meta_key = 'Sex' THEN t.meta_value END) AS 'Sex',
MAX(CASE WHEN t.meta_key = 'Primary pic' THEN t.meta_value END) AS 'Primary_pic',
GROUP_CONCAT(DISTINCT c.Post_Content SEPARATOR ',') AS Photo,
GROUP_CONCAT(DISTINCT p.post_Content) comment
FROM
(
SELECT *
FROM Table1
WHERE parent_id = 0
) AS p
INNER JOIN table1 AS c ON p.ID = c.parent_ID
INNER JOIN table2 AS t ON t.post_id = p.id
GROUP BY p.post_Name
) AS t1
INNER JOIN table2 t2 ON t1.Primary_pic = t2.post_id
AND t2.meta_key = 'Thumbnail'
INNER JOIN table2 t3 ON t1.Primary_pic = t3.post_id
AND t3.meta_key = 'meta_data';
这会给你:
| NAME | ADDRESS | PHONE | SEX | PHOTO | COMMENT | THUMBNAIL | META_DATA |
-------------------------------------------------------------------------------------------------------------------------------------------------------
| John Sample | Sample Street,Country | 10000000000000 | Male | image1.jpg,image2.jpg,image3.jpg | Content Text | John_thumbnail.jpg | data_data_data |
| Mark Sample | Mark Street,Country | 20000000000000 | Male | image2.jpg,image3.jpg,image1.jpg | Mark Text | Mark_thumbnail.jpg | data_data_data |