Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL array aggregate function like PostgreSQL array_agg

Tags:

mysql

I got two tables on MYSQL, I wonder if there is any aggregate function on MYSQL as array_agg() FROM postgreSQL.

TABLE 1 properties Only have 8 records TABLE 2 records who captured the property, so sometimes can be 1 or n times for the same property, and I got this Qry:

SELECT p.id, pcb.users_admin_id as uid
FROM properties p
INNER JOIN prop_captured_by pcb ON p.id = pcb.property_id
-- GROUP BY p.id

id    uid
200   1
200   80
202   1
202   80
211   1
211   10
211   81
215   10 ...

If I use the GROUP BY part I get this:

id    uid
200   1
202   1
211   1
215   10 ...

Losing any other data than the first value for users_admin_id. I know that I can achieve my desired result with array_agg() function from postgreSQL but I can't figured out how to do it on MYSQL.

This is my desire Result:

id    uid
200   1,80 //an array, I don't mind about the separator, could be anything.
202   1,80
211   1,10,81
215   10 ...

I have tried UNION, GROUP BY, INNER JOIN... no luck... Any pointers?

UPDATE

I am using the many to many relations from this guy. Hopes it is useful for someone else. I needed to add the user's name from a third table, so the final query looks like this:

SELECT p.id, group_concat(pcb.users_admin_id) as uid, group_concat(ua.name) as uin
FROM properties p
INNER JOIN prop_captured_by pcb ON p.id = pcb.property_id
INNER JOIN users_admin ua ON ua.id = pcb.users_admin_id
group by p.id;
like image 651
ryangus Avatar asked Aug 19 '16 02:08

ryangus


People also ask

What is ARRAY_AGG in PostgreSQL?

PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array.

What aggregation functions are supported by MySQL?

MySQL supports all the five (5) ISO standard aggregate functions COUNT, SUM, AVG, MIN and MAX. SUM and AVG functions only work on numeric data. If you want to exclude duplicate values from the aggregate function results, use the DISTINCT keyword.

Is coalesce an aggregate function?

The coalesce function can be used to substitute zero or an empty array for null when necessary. Here ANY can be considered either as introducing a subquery, or as being an aggregate function, if the subquery returns one row with a Boolean value.

Can we use aggregate function with GROUP BY clause in MySQL?

The GROUP BY clause is often used with an aggregate function to perform calculations and return a single value for each subgroup.


2 Answers

You want to use GROUP_CONCAT() like

SELECT p.id, group_concat(pcb.users_admin_id) as uid
FROM properties p
INNER JOIN prop_captured_by pcb 
ON p.id = pcb.property_id
group by p.id;
like image 129
Rahul Avatar answered Oct 09 '22 00:10

Rahul


MySQL 5.7.22 introduced JSON_ARRAYAGG() and JSON_OBJECTAGG(). Since you want the user name as well, you could use the latter:

SELECT p.id, JSON_OBJECTAGG(pcb.users_admin_id, ua.name) as uin
FROM properties p
INNER JOIN prop_captured_by pcb ON p.id = pcb.property_id
INNER JOIN users_admin ua ON ua.id = pcb.users_admin_id
group by p.id;

DB Fiddle

like image 45
Aryeh Leib Taurog Avatar answered Oct 09 '22 02:10

Aryeh Leib Taurog