2
表格1
| 身份证 | 职位名称 | Post_Status | 父ID | 发布_内容 | Post_Type |
+----+------------+-------------+------------+-- ---------+------------+
| 1 | 约翰样品| 发表 | 0 | 内容文本 | 人 |
| 2 | | 继承 | 1 | 图片1.jpg | 附件|
| 3 | | 继承 | 1 | 图片2.jpg | 附件|
| 4 | | 继承 | 1 | 图片3.jpg | 附件|
| 5 | 标记样品| 发表 | 0 | 标记文字 | 人 |
| 6 | | 继承 | 5 | 图片1.jpg | 附件|
| 7 | | 继承 | 5 | 图片2.jpg | 附件|
| 8 | | 继承 | 5 | 图片3.jpg | 附件|

表 2
| 元ID | post_id | 元密钥 | 元值 |
+---------+----------+----------------+------------ ------------+
| 1 | 1 | 地址 | 样品街,国家 |
| 2 | 1 | 电话 | 10000000000000 |
| 3 | 1 | 性别 | 男 |
| 4 | 1 | 主图 | 2 |
| 5 | 2 | 缩略图 | John_thumbnail.jpg |
| 6 | 2 | 元数据 | 数据数据数据 |
| 7 | 5 | 地址 | 马克街,国家 |
| 8 | 5 | 电话 | 20000000000000 |
| 9 | 5 | 性别 | 男 |
| 10 | 5 | 主图 | 6 |
| 11 | 6 | 缩略图 | Mark_thumbnail.jpg |
| 12 | 6 | 元数据 | 数据数据数据 |

我想这样显示

名称:John 示例
评论:内容文本
地址:Sample Street, Florida
电话:10101010101010
性别:男性
缩略图:John_thumbail.jpg
照片:images1.jpg、images2.jpg、images3.jpg

名称:Mark 示例
评论:Mark 文本
地址:Mark Street , 佛罗里达州
电话:20101010101010
性别:男性
缩略图:Mark_thumbail.jpg
照片:images1.jpg、images2.jpg、images3.jpg

查看缩略图如何链接

4

3 回答 3

3

尝试这个:

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;

SQL 小提琴演示

这会给你:

|        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;

更新的 SQL 小提琴演示


更新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;

SQL 小提琴演示

这会给你:

|        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';

更新的 SQL Fiddle 演示

这会给你:

|        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 |
于 2013-02-04T13:16:55.663 回答
1

试试下面的代码

SELECT    Table1.ID,
Table1.POST_NAME,
Table1.Post_Status,
Table1.Parent_ID,
Table1.Post_Content,
Table1.Post_Type,
Table2.meta_id,
Table2post_id,  
Table2meta_key,
Table2.meta_value       
FROM TABLE2,TABLE1
WHERE TABLE1.id= TABLE2.post_id 
于 2013-02-04T13:32:53.860 回答
0

我会研究 SQL 中的 JOINS,如下所示:

SELECT post.post_name, 
       phone.meta_value 
FROM   table_1 AS post 
       LEFT JOIN table_2 AS phone 
              ON post.id = phone.id 
                 AND phone.meta_key = "phone" 

根据需要进行编辑。

于 2013-02-04T13:17:58.317 回答