Answer the question
In order to leave comments, you need to log in
How to add a JOIN to a UNION of two SELECTs of the same table?
There is a table units with columns id, user, group, amount, multi_id, x, y.
There is a multi table with columns id, value.
There is a query to fetch records from units:
(SELECT id, amount FROM units WHERE group_id = 0 AND x=? AND y=?) UNION (SELECT MIN(id), SUM(amount) as amount FROM units WHERE group!= 0 AND x=? AND y=? GROUP BY group) LIMIT ?,?
Answer the question
In order to leave comments, you need to log in
SELECT
t.id, t.amount, t.amount*multi.value AS total_power
FROM (
SELECT id, amount, multi_id FROM units WHERE group_id = 0 AND x=? AND y=?
UNION (
SELECT g.id, g.amount , units.multi_id
FROM (
SELECT MIN(id) AS id, SUM(amount) as amount FROM units WHERE group!= 0 AND x=? AND y=? GROUP BY group
) g, units
WHERE
g.id = units.id
) t
LEFT JOIN
multi
ON
t.multi_id = multi.id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question