简体   繁体   中英

Join and have child table go into new columns on the parent table

I am wondering if this is possible; To be able to have two tables then join them, but have the names (or any field that I choose) on the child table, go into the parent table, but in new columns

So say I have table 1 is 'Site'. It designates the sites of certain programs.
Table 2 is attendees, it records the people who were at the site at this day.

+--------+--------------------------+-------------+-------+-------+------------+
| p2p_id | address                  | city        | state | zip   | date       |
+--------+--------------------------+-------------+-------+-------+------------+
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |
|    45  | 1641 whatever  Ave       | Santa Ana   | CA    | 92704 | 2014-05-16 |
|    46  | 1710 reterrr St          | Denver      | CO    | 80202 | 2014-07-10 |
|    47  | 6401 fdgdfdffffAve       | Raleigh     | NC    | 27617 | 2014-07-16 |
|    48  | East dfgdfgdf  Street    | San Antonio | TX    | 76107 | 2014-05-13 |
|    126 | 3100 fgdfgffgf           | Fort Worth  | TX    | 76107 | 2014-05-14 |
|    127 | 1001 dfgdfgdffff         | Houston     | TX    | 77002 | 2014-05-20 |
|    128 | 303 fdgdfgfgfgf          | Atlanta     | GA    | 30308 | 2014-05-22 |
|    129 | 2525 W End Ave           | Nashville   | TN    | 37203 | 2014-05-22 |
|    13  | 2041 S xzcdfdf           | Anaheim     | CA    | 92802 | 2014-05-28 |
+--------+--------------------------+-------------+-------+-------+------------+

And then we have the 'Attend' table or table 2

+------------+-------------+--------+
| first_name | last_name   | p2p_id |
+------------+-------------+--------+
    |bara    | Edgar       | 44
    |        | Estelle     | 44
    |chi     | NG          | 44
    |nhar    | Poon        | 44
    |ie      | Byrd        | 48
    |nie     | Gilet       | 48
    |nie     | Hawley      | 48
    |helle   | Hewlett     | 48
    |orah    | Siler       | 48
    |hy      | Sommerville | 48
+------------+-------------+--------+

The p2p_id is the primary key on the 'Site' table and p2p_id is the foreign key on the 'Attend' table.

The question is could I do a join

SELECT * FROM site s 
JOIN attend a
ON s.p2p_id=a.p2p_id


+--------+--------------------------+-------------+-------+-------+------------+ ------------+-------------+--------+
| p2p_id | address                  | city        | state | zip   | date       |  first_name | last_name   | p2p_id |
+--------+--------------------------+-------------+-------+-------+------------+ ------------+-------------+--------+
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |     bara    | Edgar       | 44
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |             | Estelle     | 44
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |     chi     | NG          | 44 
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |     nhar    | Poon        | 44
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |      ie     | Byrd        | 48
|    48  | East dfgdfgdf  Street    | San Antonio | TX    | 76107 | 2014-05-13 |     nie     | Gilet       | 48
|    48  | East dfgdfgdf  Street    | San Antonio | TX    | 76107 | 2014-05-13 |     helle   | Hewlett     | 48
|    48  | East dfgdfgdf  Street    | San Antonio | TX    | 76107 | 2014-05-13 |     orah    | Siler       | 48
|    48  | East dfgdfgdf  Street    | San Antonio | TX    | 76107 | 2014-05-13 |     hy      | Sommerville | 48

But instead of multiple rows of the same site popping up, is it possible for SQL (MySQL) to put it in this format?

+--------+--------------------------+-------------+-------+-------+------------+ ------------+-------------+ ------------+-------------+------------+-------------+
| p2p_id | address                  | city        | state | zip   | date       |  first_name | last_name   |  first_name | last_name   | first_name | last_name   |
+--------+--------------------------+-------------+-------+-------+------------+ ------------+-------------+ ------------+-------------+------------+-------------+
|    44  | 435 S                    | Los Angeles | CA    | 90048 | 2014-05-13 |     Barb    | Edgar       |             | Estelle     |    Chi     | Ngu         |
|    48  | East dfgdfgdf  Street    | San Antonio | TX    | 76107 | 2014-05-13 |     Helle   | Hewlett     |     Sarah   | Siler       |    Barbara | Walters     |

And if you are wondering why I don't just create new columns in the parent table with those names...Well I don't want to store my data that way, but some other people in my company need me to export data in that format.

Thanks!!

As far as I know it isn't possible without some crazy temporary tables. I think the best you can do is to process the results with some programming after use the group_concat function like this:

SELECT a.p2p_id, a.address,a.city, a.state,a.zip, a.date, group_concat(concat(first_name,' ', last_name)) as name
FROM site s 
JOIN attend a
ON s.p2p_id=a.p2p_id 
group by a.p2p_id, a.address,a.city, a.state,a.zip, a.date, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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