I’m wanting to optimize a query using a union as a sub query.
Im not really sure how to construct the query though.
I’m using MYSQL 8.0.12
Here is the original query:
---------------
| c1 | c2 |
---------------
| 18182 | 0 |
| 18015 | 0 |
---------------
2 rows in set (0.35 sec)
I’m sorry but the question doesn’t stored if I paste the sql query as text and format using ctrl+k
Output expected
---------------
| c1 | c2 |
---------------
| 18182 | 167 |
| 18015 | 0 |
---------------
As a output I would like to have the difference of rows between the two tables in UNION ALL
.
I processed this question using the wizard https://stackoverflow.com/questions/ask
2
Answers
There are several ways to go for this, including
UNION
, but I wouldn’t recommend it, as it is IMO a bit ‘hacky’. Instead, I suggest you use subqueries or useCTE
s.With subqueries
With
CTE
s, also known asWITH
sIn a practical sense, they are the same. Depending on the needs, you might want to define and join the results though, and in said cases, you could use a single number as a "pseudo id" in the select statement.
Since you only want to know the differences, I used the
ABS
function, which returns the absolute value of a number.Let me know if you want a solution with
UNION
s anyway.Edit: As @Rick James pointed out,
COUNT(*)
should be used in the subqueries to count the number of rows, asCOUNT(id_***)
will only count the rows with non-null values in that field.Since a parenthesized
SELECT
can be used almost anywhere a expression can go:Also, MySQL is happy to allow a
SELECT
without aFROM
.