如何显示来自不同表的数据?

时间:2013-02-04 13:12:50

标签: php mysql sql

Table 1
| ID | Post_Name  | Post_Status | Parent_ID | Post_Content | Post_Type |
+----+------------+-------------+-----------+--------------+-----------+
| 1  | John Sample| publish     | 0         | Content Text | person    |
| 2  |            | inherit     | 1         | image1.jpg   | attachment|
| 3  |            | inherit     | 1         | image2.jpg   | attachment|
| 4  |            | inherit     | 1         | image3.jpg   | attachment|
| 5  | Mark Sample| publish     | 0         | Mark  Text   | person    |
| 6  |            | inherit     | 5         | image1.jpg   | attachment|
| 7  |            | inherit     | 5         | image2.jpg   | attachment|
| 8  |            | inherit     | 5         | image3.jpg   | attachment|

Table 2
| meta_id | post_id  |    meta_key    |      meta_value       |
+---------+----------+----------------+-----------------------+
|   1     |    1     |  Address       | Sample Street,Country |
|   2     |    1     |  Phone         | 10000000000000        |
|   3     |    1     |  Sex           | Male                  |
|   4     |    1     |  Primary pic   | 2                     |
|   5     |    2     |  Thumbnail     | John_thumbnail.jpg    |
|   6     |    2     |  meta_data     | data_data_data        |
|   7     |    5     |  Address       | Mark Street,Country   |
|   8     |    5     |  Phone         | 20000000000000        |
|   9     |    5     |  Sex           | Male                  |
|   10    |    5     |  Primary pic   | 6                     |
|   11    |    6     |  Thumbnail     | Mark_thumbnail.jpg    |
|   12    |    6     |  meta_data     | data_data_data        |

我想像这样显示

姓名:John Sample
评论:内容文本
地址:佛罗里达州Sample Street 电话:10101010101010
性别:男性 缩略图:John_thumbail.jpg
照片:images1.jpg,images2.jpg,images3.jpg

名称:标记样品
评论:标记文字
地址:佛罗里达州马克街 电话:20101010101010
性别:男性 缩略图:Mark_thumbail.jpg
照片:images1.jpg,images2.jpg,images3.jpg

see how the thumbnail links

3 个答案:

答案 0 :(得分: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 Fiddle Demo

这会给你:

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

Updated SQL fiddle Demo


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;

SQL Fiddle Demo

这会给你:

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

Updated SQL Fiddle Demo

这会给你:

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

Like this

这会给你:

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

答案 1 :(得分: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 

答案 2 :(得分: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" 

根据需要进行编辑。