Query whose result includes a field that is the result of another query in mysql

1

I have a table of people that has the following fields, among others:

+----------------------------+
|persona_id | persona_nombre |
+-----------+----------------+
| 1         | persona 1      |
| 2         | persona 2      |
+-----------+----------------+

And I have a table of patterns with the following fields:

+--------------------------+
|persona_nombre | padrones |
+---------------+----------+
| persona 1     | 1,2      |
| persona 2     | 1        |
+--------------------------+

There is some way to make a query that returns the following:

+-----------------------+
|persona_id | padron_id |
+-----------+-----------+
| 1         | 1         |
| 1         | 2         |
| 2         | 1         |
+-----------+-----------+
    
asked by Pillo 04.01.2017 в 18:32
source

1 answer

2

You can do it in the following way:

 SELECT A.persona_nombre, (
   SELECT GROUP_CONCAT(B.padron_id SEPARATOR ',')
   FROM padrones AS B
   WHERE B.persona_id = A.persona_id
   GROUP BY B.persona_id
 ) AS padrones
 FROM personas AS A

Or so too:

 SELECT A.persona_nombre, 
   GROUP_CONCAT(B.padron_id SEPARATOR ',') AS padrones
 FROM personas AS A
 INNER JOIN padrones AS B 
   ON B.persona_id = A.persona_id
 GROUP BY A.persona_id
    
answered by 04.01.2017 / 18:50
source