简体   繁体   English

将多行中多个MySql表中的数据合并到SELECT语句的一行中

[英]Combining data from multiple MySql tables, in multiple rows, into a single row for a SELECT statement

Note: This is not a duplicate of: mySQL - Create a New Table Using Data and Columns from Three Tables . 注意:这不是以下项的重复: mySQL-使用三个表中的数据和列创建新表 That question is about taking data from multiple tables and actually creating a new table with that data. 这个问题是关于从多个表中获取数据并实际使用该数据创建一个新表。 I'm just looking to select already existing data. 我只是想选择已经存在的数据。 The answer on that question is very specific to the question that was asked, and doesn't really help me since the code in the answer is not explained. 该问题的答案非常特定于所提出的问题,并且由于未解释答案中的代码,因此对我没有任何帮助。 So no, this is not "the exact same question" at all. 所以不,这根本不是“完全相同的问题”。

I have a question about joining two MySQL tables that I cannot seem to find an answer to. 我有一个关于连接两个MySQL表的问题,我似乎找不到答案。 The usual types of joins do not seem to be what I'm after. 普通的连接类型似乎不是我想要的。 perhaps this is not possible, I'm not sure. 也许这是不可能的,我不确定。

So I have my first table like this (tables simplified for example purposes): 因此,我有了第一个这样的表(出于示例目的而简化的表):

ID    Name    Email               Phone
1     John    john@example.com    000-123-4567
2     Jane    jane@example.com    000-890-1234

My second table is arranged this way: 我的第二张桌子是这样安排的:

ID   UserID    meta_key            meta_value
1    1         location_review     4
2    1         condition_review    1
3    1         price_review        5
4    2         location_review     4
5    2         condition_review    2
6    2         price_review        4

I am working with some existing software, just making some modifications, so ideally I can do what I need without needing to change how the software is written too much. 我正在使用一些现有软件,只是进行了一些修改,因此理想情况下,我可以做我需要的事情,而无需过多地编写软件。

I am trying to be able to join these tables so they look like this: 我试图能够加入这些表,以便它们看起来像这样:

    ID    Name    Email               Phone          location_review    condition_review    price_review
    1     John    john@example.com    000-123-4567   4                  1                   5
    2     Jane    jane@example.com    000-890-1234   4                  2                   4

So I need to add the rows from Table2 that have the UserID to the data already in Table1. 因此,我需要将Table2中具有UserID的行添加到Table1中已有的数据中。

Since this is existing software there is already some code in place to join these two tables, it is here: 由于这是现有软件,因此已经有一些代码可以连接这两个表,因此位于此处:

SELECT * 
FROM wpnf_comments as comment 
    INNER JOIN wpnf_commentmeta AS meta 
WHERE comment.comment_post_ID = $prop_ID 
AND meta.meta_key = 'rating' 
AND meta.comment_id = comment.comment_ID 
AND ( comment.comment_approved = 1 
    OR comment.user_id = $userID 
    )

What I am doing is: There used to be only a single "rating" value, stored in the meta table. 我正在做的是:过去只有一个“评级”值存储在元表中。 You can see it in the above Join where it says: 您可以在上面的Join中看到它的内容:

AND meta.meta_key = 'rating'

However, I am trying to replace that with 3 separate ratings. 但是,我试图用3个单独的评分代替它。 So the join above, that used to work when there was only a single rating, doesn't really work when I have multiple ratings. 因此,上面的联接在只有一个评级的情况下才起作用,而在我拥有多个评级的情况下,它并没有真正起作用。

I would very much appreciate any insight into what I need to do to fix this. 对于解决此问题所需的任何见解,我将非常感谢。 I am also using PHP for this, if that matters. 如果这很重要,我也正在使用PHP。

EDIT 编辑

This is the code I have so far. 这是我到目前为止的代码。 it's showing all of the correct column names I need now, but the actual values/data is not showing up. 它显示的是我现在需要的所有正确的列名,但未显示实际值/数据。 It's returning 0 rows. 它返回0行。

SELECT 
  * 
FROM 
  wpnf_comments t1 
  INNER JOIN 
  (SELECT 
      comment_id as comment_id2, 
      MAX(IF(meta_key = 'location-rating', meta_value, NULL)) AS location_rating, 
      MAX(IF(meta_key = 'condition-rating', meta_value, NULL)) AS condition_rating, 
      MAX(IF(meta_key = 'maintenance-rating', meta_value, NULL)) AS maintenance_rating, 
      MAX(IF(meta_key = 'professionalism-rating', meta_value, NULL)) AS professionalism_rating, 
      MAX(IF(meta_key = 'contact-rating', meta_value, NULL)) AS contact_rating, 
      MAX(IF(meta_key = 'availability-rating', meta_value, NULL)) AS availability_rating, 
      MAX(IF(meta_key = 'responsiveness-rating', meta_value, NULL)) AS responsiveness_rating, 
      MAX(IF(meta_key = 'price-rating', meta_value, NULL)) AS price_rating 
   FROM 
      wpnf_commentmeta 
   GROUP BY comment_id) temp_t 
      ON t1.comment_post_ID = temp_t.comment_id2

Using MySQL pivot tables 使用MySQL数据透视表

SELECT 
      temp_t.user_id, t1.Name, t1.Email, t1.Phone, temp_t.location_review, temp_t.condition_review, temp_t.price_review 
    FROM 
      table_1st t1 
      INNER JOIN 
      (SELECT 
          UserID as user_id, 
          MAX(IF(meta_key = 'location_review', meta_value, NULL)) AS location_review, 
          MAX(IF(meta_key = 'condition_review', meta_value, NULL)) AS condition_review, 
          MAX(IF(meta_key = 'price_review', meta_value, NULL)) AS price_review 
       FROM 
          table_2nd 
       GROUP BY UserID) temp_t 
          ON t1.ID = temp_t.user_id 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM