これを試して:
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;
Update2:
更新されたサンプルデータに基づく:
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
に、2番目のテーブルにもう1つ結合する必要があります。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 |