I have a json query that gives me json of a joined table of person and pets:
SELECT json_object(
'personId', p.id,
'pets', json_arrayagg(json_object(
'petId', pt.id,
'petName', pt.name
))
)
FROM person p LEFT JOIN pets pt
ON p.id = pt.person_id
GROUP BY p.id;
my issue is that person can have 0 or more pets, and when a person have 0 pets I get list with 1 empty pet, and what I would like to get in that case is empty list.
this is what I get:
{
"personId": 1,
"pets": [
{
"petId": null,
"petName": ""
}
]
}
and I need:
{
"personId": 1,
"pets": []
}
is that possible?
The problem is that LEFT JOIN still returns columns from the table you're joining with, it just sets their values to NULL.
You can use IF to test COUNT(pt.id), as this won't count null values.
SELECT json_object(
'personId', p.id,
'pets', IF(COUNT(pt.id) = 0, JSON_ARRAY(),
json_arrayagg(json_object(
'petId', pt.id,
'petName', pt.name
)
))
)
FROM person p LEFT JOIN pets pt
ON p.id = pt.person_id
GROUP BY p.id;
Another possibility is to put the aggregation in a correlated subquery and use coalesce() to replace it with an empty array if no rows exist.
SELECT json_object('personID', p.id,
'pets', coalesce((SELECT json_arrayagg(json_object('petId', t.id,
'petName', t.name))
FROM pets t
WHERE t.person_id = p.id),
json_array()))
FROM person p;
Adding another option:
select IFNULL( /*expression of select ...JSON_ARRAYAGG(
JSON_OBJECT(....*/,JSON_ARRAY()) jarrayaggAlias
Cleaner below:
IFNULL( expression, ,JSON_ARRAY()) jarrayaggAlias
Result:
/* { jarrayaggAlias: [] }*/
If you swap alt_value for IFNULL to select [], then all your results will be stringyfied.
I have this query that returns the name of the product and the array of images as a JSON string. However, when there is no image attached to a product, I would like this query to return an empty array for the images property.
Currently, it returns this when there is no product image found:
{ "name": "Product Name", "images": [{"id": null, "slug": null}]}
I tried to add an IF condition into the CONCAT method, but it returns the same response.
SELECT p.name,
CONCAT('[',
IF(i.id = NULL,
'',
GROUP_CONCAT(DISTINCT (
JSON_OBJECT(
'id', i.id,
'slug', i.slug
)
))
),
']') AS images
FROM products AS p
LEFT JOIN _product_images AS pi ON pi.pId = p.id
LEFT JOIN images AS i ON i.id = pi.iId
WHERE p.id = 4;
Thank you!
As other have mentioned, i.id = NULL will always evaluate to NULL. But your approach is needlessly complicated and would raise an error on a strictly configured server. On db-fiddle I get the following error:
ER_MIX_OF_GROUP_FUNC_AND_FIELDS: In aggregated query without GROUP BY,
expression #2 of SELECT list contains nonaggregated column
'test.i.id'; this is incompatible with sql_mode=only_full_group_by
demo
So the check i.id IS NULL needs to be done within the GROUP_CONCAT() function:
SELECT p.name,
CONCAT('[',
GROUP_CONCAT(DISTINCT (
IF (i.id IS NULL, '',
JSON_OBJECT(
'id', i.id,
'slug', i.slug
)
)
)),
']') AS images
FROM products AS p
LEFT JOIN _product_images AS pi ON pi.pId = p.id
LEFT JOIN images AS i ON i.id = pi.iId
WHERE p.id = 4
However - You can avoid the check, when you use an INNER JOIN. But the INNER JOIN will also ignore the data from the products table - So I would do that JOIN in a correlated subquery. An finally you can use JSON_ARRAYAGG() to generate a JSON array.
demo
SELECT p.name, COALESCE((
SELECT JSON_ARRAYAGG(JSON_OBJECT(
'id', i.id,
'slug', i.slug
))
FROM _product_images AS pi
JOIN images AS i ON i.id = pi.iId
WHERE pi.pId = p.id
), JSON_ARRAY()) AS images
FROM products AS p
#WHERE p.id = 4;
demo
You should be using IS NULL to check for a NULL value:
SELECT
p.name,
CONCAT('[',
GROUP_CONCAT(
IF(id IS NULL,
'',
DISTINCT JSON_OBJECT('id', i.id, 'slug', i.slug))),
']') AS images
FROM products AS p
LEFT JOIN _product_images AS pi ON pi.pId = p.id
LEFT JOIN images AS i ON i.id = pi.iId
WHERE p.id = 4
GROUP BY p.name;
As a side note, DISTINCT is not a function, and you should not be using it as such, so I removed the function parentheses which you were using.
Edit: Updated SQL to satisfy the group restrictions. The IF-Statement for checking i.id outside of the GROUP_CONCAT would fail, due to multiple image items per row.
IF(i.id = NULL, ...) returns NULL, for whatever value has i.id (even NULL), which is a falsy value.
You want to use instead IF (i.id IS NULL, ...)
From documentation :
You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL. To demonstrate this for yourself, try the following query:
mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
| NULL | NULL | NULL | NULL |
+----------+-----------+----------+----------+
I have tables:
Users
user_id
name
Posts
post_id
user_id
post
image_path
Reactions
reaction_id
post_id
reaction
user_id
Reactions have values of {1: 'like', 2: 'dislike'}
Now I want to get all the posts with the corresponding counts with each react.
So end result would be like:
{
post_id: 1,
post: "Hello World",
image_path: "public/upload_123.png",
likes: 23,
dislikes: 18,
my_reaction: 1
}
You can do it by joining the tables with a LEFT join (just in case there are no reactions for a post) and then with conditional aggregation:
select p.post_id, p.post, p.image_path,
sum(r.reaction = 1) likes,
sum(r.reaction = 2) dislikes
from posts p left join reactions r
on r.post_id = p.post_id
group by p.post_id, p.post, p.image_path
I finally got the query. It goes as follows:
SELECT `posts`.`posts_id`, `users`.`user_id`, `posts`.`post`, `posts`.`file_path`, `posts`.`created_at`, `users`.`fname`, `users`.`mname`, `users`.`lname`, `r1`.`reaction`, IF(SUBSTRING_INDEX(posts.file_path, '.', -1) IN ('jpeg', 'jpg', 'png'), 'image', 'document') AS file_type,
SUM(reactions.reaction = 1) AS likes,
SUM(reactions.reaction = 2) AS hearts,
SUM(reactions.reaction = 3) AS dislikes FROM `posts`
LEFT JOIN `users` ON `posts`.`user_id` = `users`.`user_id` LEFT JOIN `reactions` ON `reactions`.`posts_id` = `posts`.`posts_id`
LEFT JOIN `reactions` AS `r1` ON `r1`.`posts_id` = posts.posts_id AND r1.user_id = 1 WHERE `posts`.`deleted_at` IS NULL
GROUP BY `posts`.`posts_id`
ORDER BY `posts`.`created_at` DESC
Just run this code..
SELECT a.post_id, a.post,a.image_path,b.reaction,count(b.reaction)
from posts a
INNER join reactions b on a.post_id=b.post_id
group by a.post_id, a.post,a.image_path,b.reaction
I have a query that I want to optimize. Both unions executed separatley run fine, however as soon as I include inner join it takes up to 57 seconds. How do I solve this. My query is as follows
SELECT
p.PROJID,
p.StartDate,
o.ORDERNO,
p.PROJCODE,
p.PROJECT,
cat.type AS CATEGORY,
p.AREA,
p.STATE,
p.COUNTRY,
p.VALUE,
p.PROCESSOR,
p.PROJINFO,
p.NES,
p.SPECSALE,
p.OFFICE,
p.DEPTCODE,
p.INTERNLCHG,
p.INTERCOCHG,
p.LORM,
p.PERCENT,
d.COMPANY,
CONCAT(
d.LASTNAME,
", ",
d.FIRSTNAME
) AS Contact
FROM
(
(
SELECT
*
FROM
(
SELECT
`clients`.`CLIENTID` AS `CLIENTIDA`,
`clients`.`COMPANY` AS `COMPANY`
FROM
`hdb`.`clients`
UNION
SELECT
`accounts`.`id` AS `CLIENTIDA`,
`accounts`.`name` AS `COMPANY`
FROM
`sugarcrm`.`accounts`
) AS a
INNER JOIN (
SELECT
`hdb`.`contacts`.`CONTACTID` AS `CONTACTID`,
`hdb`.`contacts`.`CLIENTID` AS `CLIENTIDC`,
`hdb`.`contacts`.`FIRSTNAME` AS `FIRSTNAME`,
`hdb`.`contacts`.`LASTNAME` AS `LASTNAME`
FROM
`hdb`.`contacts`
UNION
SELECT
`sugarcrm`.`contacts`.`id` AS `CONTACTID`,
`sugarcrm`.`accounts_contacts`.`account_id` AS `CLIENTIDC`,
`sugarcrm`.`contacts`.`first_name` AS `FIRSTNAME`,
`sugarcrm`.`contacts`.`last_name` AS `LASTNAME`
FROM
`sugarcrm`.`contacts`
LEFT JOIN `sugarcrm`.`accounts_contacts` ON `sugarcrm`.`contacts`.`id` = `sugarcrm`.`accounts_contacts`.`contact_id`
) AS c ON a.CLIENTIDA = c.CLIENTIDC
) AS d
)
INNER JOIN (
(
projects AS p
INNER JOIN orders AS o ON p.ORDERNO = o.ORDERNO
)
INNER JOIN category AS cat ON p.category_id = cat.category_id
) ON d.CONTACTID = o.CONTACTID
Explain on this provides the following:
1, PRIMARY, cat, ALL, PRIMARY, , , , 10,
1, PRIMARY, p, ref, FK_orders_projects,FK_category_projects_idx, FK_category_projects_idx, 5, hdb.cat.category_id, 400, Using where
1, PRIMARY, o, eq_ref, PRIMARY, PRIMARY, 4, hdb.p.ORDERNO, 1,
1, PRIMARY, <derived2>, ALL, , , , , 18878, Using where
2, DERIVED, <derived3>, ALL, , , , , 7087,
2, DERIVED, <derived5>, ALL, , , , , 18879, Using where
5, DERIVED, contacts, ALL, , , , , 8261,
6, UNION, contacts, ALL, , , , , 10251,
6, UNION, accounts_contacts, ref, idx_contid_del_accid, idx_contid_del_accid, 111, sugarcrm.contacts.id, 1, Using index
, UNION RESULT, <union5,6>, ALL, , , , , ,
3, DERIVED, clients, ALL, , , , , 2296,
4, UNION, accounts, ALL, , , , , 4548,
, UNION RESULT, <union3,4>, ALL, , , , , ,
The original query without the union takes 0.125 seconds
SELECT p.PROJID, p.StartDate, o.ORDERNO, p.PROJCODE, p.PROJECT, cat.type AS CATEGORY, p.AREA, p.STATE, p.COUNTRY,
p.VALUE, p.PROCESSOR, p.PROJINFO, p.NES, p.SPECSALE, p.OFFICE, p.DEPTCODE, p.INTERNLCHG, p.INTERCOCHG, p.LORM,
p.PERCENT, a.COMPANY, CONCAT(c.LASTNAME, ", ", c.FIRSTNAME) AS Contact
FROM (clients AS a
INNER JOIN contacts AS c ON a.CLIENTID =c.CLIENTID)
INNER JOIN ((projects AS p INNER JOIN orders AS o ON p.ORDERNO = o.ORDERNO)
INNER JOIN category AS cat ON p.category_id = cat.category_id) ON c.CONTACTID = o.CONTACTID
ORDER BY p.PROJID, a.COMPANY;
explain on this provides following:
1, SIMPLE, cat, ALL, PRIMARY, , , , 10, Using temporary; Using filesort
1, SIMPLE, p, ref, FK_orders_projects,FK_category_projects_idx, FK_category_projects_idx, 5, hdb.cat.category_id, 400, Using where
1, SIMPLE, o, eq_ref, PRIMARY,FK_contacts_orders, PRIMARY, 4, hdb.p.ORDERNO, 1,
1, SIMPLE, c, eq_ref, PRIMARY,FK_clients_contacts, PRIMARY, 52, hdb.o.CONTACTID, 1,
1, SIMPLE, a, eq_ref, PRIMARY, PRIMARY, 52, hdb.c.CLIENTID, 1,
Query with view:
SELECT
p.PROJID,
p.StartDate,
o.ORDERNO,
p.PROJCODE,
p.PROJECT,
cat.type AS CATEGORY,
p.AREA,
p.STATE,
p.COUNTRY,
p.VALUE,
p.PROCESSOR,
p.PROJINFO,
p.NES,
p.SPECSALE,
p.OFFICE,
p.DEPTCODE,
p.INTERNLCHG,
p.INTERCOCHG,
p.LORM,
p.PERCENT,
a.COMPANY,
CONCAT(
c.LASTNAME,
", ",
c.FIRSTNAME
) AS Contact
FROM
(
view_accounts_sugar_hdb AS a
INNER JOIN view_contacts_sugar_hdb AS c ON a.CLIENTID = c.CLIENTID
)
INNER JOIN (
(
projects AS p
INNER JOIN orders AS o ON p.ORDERNO = o.ORDERNO
)
INNER JOIN category AS cat ON p.category_id = cat.category_id
) ON c.CONTACTID = o.CONTACTID
ORDER BY
p.PROJID,
a.COMPANY;
This takes over 340 secs.
This one was definitely uglier than the last one I helped you on :)... Anyhow, the same principles apply. For future, please do try to understand what I'm doing here. Write the JOIN relationships FIRST to know where your data is coming from. Also, take a look at my indentations... I am showing at each level for readability.
Orders -> Projects -> Categories...
then, the path for those in the normal clients table via
Orders -> Contacts -> Clients
finally to the SugarCRM contacts...
Orders -> Accounts_Contacts -> Accounts
So, now that you have the relationships set (and aliased), this follows similar of last answer implementing LEFT-JOIN to normal contact/client vs CRM Contacts/Accounts.
The field list is simple on the Order, Products and Category tables as those are pretty direct. That leaves just the "who/client" information where the LEFT-JOIN comes in. If the Normal client is null, use the CRM version fields, otherwise use the normal client fields...
SELECT
P.PROJID,
P.StartDate,
O.ORDERNO,
P.PROJCODE,
P.PROJECT,
cat.`type` AS CATEGORY,
P.AREA,
P.STATE,
P.COUNTRY,
P.VALUE,
P.PROCESSOR,
P.PROJINFO,
P.NES,
P.SPECSALE,
P.OFFICE,
P.DEPTCODE,
P.INTERNLCHG,
P.INTERCOCHG,
P.LORM,
P.PERCENT,
CASE when HCLIENT.ClientID IS NULL
then SCLIENT.`name`
ELSE HCLIENT.Company end as Company,
CASE when HCLIENT.ClientID IS NULL
then CONCAT( SCT.LAST_NAME, ", ", SCT.FIRST_NAME )
ELSE CONCAT( HCT.LASTNAME, ", ", HCT.FIRSTNAME ) end as Contact
FROM
orders O
JOIN projects P
ON O.OrderNo = P.OrderNo
JOIN category AS cat
ON p.category_id = cat.category_id
LEFT JOIN hdb.contacts HCT
ON O.ContactID = HCT.ContactID
LEFT JOIN hdb.clients HCLIENT
ON HCT.ClientID = HCLIENT.ClientID
LEFT JOIN sugarcrm.contacts SCT
ON O.ContactID = SCT.ID
LEFT JOIN sugarcrm.accounts_contacts SAC
ON SCT.ID = SAC.contact_id
LEFT JOIN sugarcrm.accounts SCLIENT
ON SCT.account_id = SCLIENT.ID
I'd be interested in the performance improvement too.