I have one case where I want to use group_contact value as a condition with other column.
please have a look the following code which I applied but return reference to group function.
take an one example as following.
SELECT
GROUP_CONCAT(id) AS loca_id,
(SELECT
SUM(loc.total_qty)
FROM
locations_assign AS loc
WHERE
id IN (loca_id)) AS pend_qty
FROM
locations_assign
WHERE
putaway_type = 3
Is there any way where I can achieve this?
Please help me for the same.
For the IN clause you don't need a GROUP_CONCAT a SELECT sufficed
SELECT
GROUP_CONCAT(id) AS loca_id,
(SELECT
SUM(loc.total_qty)
FROM
locations_assign AS loc
WHERE
id IN (SELECT
id
FROM
locations_assign
WHERE
putaway_type = 3)) AS pend_qty
FROM
locations_assign
WHERE
putaway_type = 3
GROUP_CONCAT()
returns a comma separated list of ids which is a string and can not be used with the operator IN
.
Instead use FIND_IN_SET()
:
SELECT
t.loca_id,
SUM(loc.total_qty) AS pend_qty
FROM locations_assign AS loc
CROSS JOIN (
SELECT GROUP_CONCAT(id) AS loca_id
FROM locations_assign
WHERE putaway_type = 3
) t
WHERE FIND_IN_SET(loc.id, t.loca_id)
IN
doesn't work with a comma-separated strings, but with a list of separate values. As the list is just the IDs you are selecting anyway, it seems you only want a common aggregation: the list of IDs and the quantity sum:
SELECT
GROUP_CONCAT(id) AS loca_id,
SUM(total_qty) AS pend_qty
FROM locations_assign
WHERE putaway_type = 3;
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.