This is my query:
SELECT UID, COUNT( * ) *100 AS Points
FROM `Visited`
WHERE UID = '25'
UNION ALL
SELECT UID, COUNT( * ) *1000 AS Points
FROM `Sites`
WHERE UID = '25'
UNION ALL
SELECT UID, COUNT( * ) *1000 AS Points
FROM `userTags`
WHERE UID = '25'
When I run it, and one of the subqueries finds no results I get:
#1048 - Column 'UID' cannot be null
I don’t really understand the problem, running the subquery that gives no results alone works alright and displays:
UID | POINTS
NULL 0
However the big query won’t unite it for some reason. What could be the reason?
—- EDIT
Also if the three subqueries give results the query works fine. The only problem is where there are no results in one of them.
It looks like there are no matching rows for
UID = 25, howeverCOUNT(*)still returns a row saying there are0matching rows. You can either remove the row by using an outer select where UID is not null, or more simply replace theNULL, like this:Of course, you would replace the
'25'with$uidfor running from your code.Do the same for the other tables too if you think there’s a chance there are no matching rows