IT.COM

Strange result of SQL query

Spaceship Spaceship
Watch

gee

Established Member
Impact
10
Guys, I'm fighting with this problem several hours. Can someone tell me why this query gives me 4 rows (as it should):

SELECT SUM(18 * 4) AS price
UNION
SELECT SUM(7 * 4 * 2) AS price
UNION
SELECT SUM(6 * 4 * 2) AS price
UNION
SELECT SUM(15 * 0.445179 * 4) AS price


And this query gives me only 3 rows:

SELECT SUM(18 * 4) AS price
UNION
SELECT SUM(7 * 4 * 2) AS price
UNION
SELECT SUM(9 * 4 * 2) AS price
UNION
SELECT SUM(15 * 0.668134 * 4) AS price


It is tested on mysql database.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Let me know if you tried and failed.
 
0
•••
No one even tried to run the query?
 
0
•••
I just tried it, indeed, you get 4 results with the first and 3 results with the second.
Cannot explain why though.

Thanks,
Cristian
 
0
•••
Both query below returns 72. I guess they were combined because of UNION

SELECT SUM(18 * 4) AS price
and
SELECT SUM(9 * 4 * 2) AS price
 
0
•••
@NamePrime good observation.

I found this: "By default, the UNION operator removes duplicate rows even if you don’t specify the DISTINCT operator explicitly."

"If you use the UNION ALL explicitly, the duplicate rows, if available, remain in the result. Because UNION ALL does not need to handle duplicates, it performs faster than UNION DISTINCT."

Thanks guys
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back