jimmyoneshot Posted March 21, 2012 Share Posted March 21, 2012 I know this isn't a practical example but it's a simplified version of my problem. Basically if I have this query or something:- SELECT DISTINCT COUNT(u1.id) AS u1_count, u1.id FROM users AS u1 WHERE u1.id = 1 UNION ALL SELECT DISTINCT COUNT(u1.id) AS u1_count, u1.id FROM users AS u1 WHERE u1.id = 997 I get the error #1048 - Column 'id' cannot be null The first query on it's own returns 1 result whereas the second query on it's own returns 0 results but when they are unioned this error get's thrown. Including the COUNT seems to be what causes the problem. The following query however:- SELECT DISTINCT COUNT(u1.id) AS u1_count, u1.id FROM users AS u1 WHERE u1.id = 1 UNION ALL SELECT DISTINCT COUNT(u1.id) AS u1_count, u1.id FROM users AS u1 WHERE u1.id = 2 works fine because the second query in this query returns a row. How can I get rid of this error please? Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/ Share on other sites More sharing options...
jimmyoneshot Posted March 21, 2012 Author Share Posted March 21, 2012 Hello Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1329736 Share on other sites More sharing options...
AyKay47 Posted March 21, 2012 Share Posted March 21, 2012 There is no reason to be using table references, you are using 1 table. This is occurring because the where clause in one of the queries is not being satisfied. Why not have something like this instead: select count(distinct id) as id_count, id from users where id = 1 or id = 997 Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1329809 Share on other sites More sharing options...
jimmyoneshot Posted March 22, 2012 Author Share Posted March 22, 2012 Sorry maybe the example I gave was pretty bad because I tried to simplify it which was a mistake but basically I need to show user types as in I need to show all the user that are type 1 underneath one heading on my site and then all the user's that are type 2 underneath which means using OR wouldn't be relevant so it should look like this like this:- TYPE 1 USERS John Smith Paul Jones Jill Jackson TYPE 2 USERS Gary Stevenson Steve Arnold So maybe this is a better example of why I'm using join which again produces the error of either the top or bottom query returns no results:- SELECT DISTINCT @x:='TYPE 1 USERS' AS heading, COUNT(u1.id) AS u1_count, u1.id FROM users AS u1 WHERE u1.type = 1 UNION ALL SELECT DISTINCT @x:='TYPE 2 USERS' AS heading, COUNT(u1.id) AS u1_count, u1.id FROM users AS u1 WHERE u1.type = 2 Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330049 Share on other sites More sharing options...
AyKay47 Posted March 22, 2012 Share Posted March 22, 2012 You are not using a join you are using a union. Do you know what you are actually doing by setting @x:= to a field? Because you immediately overwrite the value that you set. If you are not sure if one of these values will return a result set, use a join or subquery. select distinct `type 1 users` as type_1, count(id) as count, (select distinct `type 2 users` from users where type = 2) as type_2 where type = 1; *This query is untested, may need tweaked* Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330155 Share on other sites More sharing options...
jimmyoneshot Posted March 22, 2012 Author Share Posted March 22, 2012 OK I modified your query to this:- select distinct users.id as type_1,count(id) as count, (select distinct users.id from users where type = 2) as type_2 FROM users where type = 1 Which resulted in the error:- "#1242 - Subquery returns more than 1 row" Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330161 Share on other sites More sharing options...
AyKay47 Posted March 22, 2012 Share Posted March 22, 2012 Yeah, I failed. select distinct u1.id as type_1, count(id) as count, u2.id as type_2 left join users as u2 on (u2.type = 2) from users as u1 where u1.type = 1; Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330167 Share on other sites More sharing options...
jimmyoneshot Posted March 22, 2012 Author Share Posted March 22, 2012 Thanks. I modified it to this:- select distinct u1.id as type_1, count(u1.id) as count, u2.id as type_2 from users as u1 left join users as u2 on (u2.type = 2) where u1.type = 1 The problem is this the result is then returned as this in a single row:- type 1 | count | type 2 7 4 8 Whereas I need all users to be returned like this so that I can loop through the result and echo them out on the front end:- id | Name | count | type 7 John 4 type 1 user 8 Paul 4 type 1 user 10 Steve 4 type 1 user 9 Jack 4 type 1 user 6 Jill 2 type 2 user 4 Gary 2 type 2 user I can't see how this can be done with subqueries based on the fact that I need a list of users. My actual where criteria for the queries I have is a bit more complicated as it is actually based on another left join which defines if users are registered but as I say I tried to simplify it here. Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330180 Share on other sites More sharing options...
AyKay47 Posted March 22, 2012 Share Posted March 22, 2012 If you want to grab multiple rows, then generate the query in a way that it will grab multiple rows. Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330184 Share on other sites More sharing options...
jimmyoneshot Posted March 22, 2012 Author Share Posted March 22, 2012 OK sorry about this ha. Thanks for the help. I'll see if I can come up with a better example Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330186 Share on other sites More sharing options...
jimmyoneshot Posted March 22, 2012 Author Share Posted March 22, 2012 I think I've solved it the count was the problem. I just used a subquery to get counts Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330189 Share on other sites More sharing options...
fenway Posted March 25, 2012 Share Posted March 25, 2012 I think I've solved it the count was the problem. I just used a subquery to get counts Then please post the final working query. Quote Link to comment https://forums.phpfreaks.com/topic/259382-column-cannot-be-null/#findComment-1330970 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.