[英]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'. 假设我的表1是“站点”。 It designates the sites of certain programs.
它指定某些程序的站点。
Table 2 is attendees, it records the people who were at the site at this day. 表2是与会者,它记录了当天在现场的人员。
+--------+--------------------------+-------------+-------+-------+------------+
| 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 然后我们有“参加”表或表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. p2p_id是“站点”表上的主键,而p2p_id是“参与者”表上的外键。
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? 但是,不是弹出同一站点的多行,而是SQL(MySQL)可以将其以这种格式显示吗?
+--------+--------------------------+-------------+-------+-------+------------+ ------------+-------------+ ------------+-------------+------------+-------------+
| 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:
我认为您最好的办法是在使用group_concat函数后,通过一些编程来处理结果:
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,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.