this is my first question ever, so please be patient.. 🙂
We are two developers and both have the same MySql DB with same tables and values.
One is MySql version 5.5 and works ok (apparently) as I am told by the other developer.
On my machine with MySql 5.1.44 (a basic MAMP install) I have the following weird problem.
A very huge query (not mine) fails with error "Column ‘xd’ cannot be null".
Removing pieces I slimmedi it down to this:
select xd, avg(media) from questionario_punteggi where somefield = 1 union select 1,2
Note, there is no record with somefield = 1 so the first select returns an empty set
We have a SELECT with AVG() function that returns an empty set UNION another SELECT that returns something (1,2 are just random values I put now as an example)
- If I remove the AVG() the query works.
- If I remove xd (and the 2 of 1,2 to the right) the query works.
- If I remove the UNION the query works.
- If I set some record with somefield = 1 the query works.
- On the other machine 5.5 the query works.
Otherwise the error is:
1048 – Column ‘xd’ cannot be null
Fields are:
`xd` char(3) NOT NULL DEFAULT '001',
`media` decimal(7,4) NOT NULL DEFAULT '0.0000',
`somefield` tinyint(4) NOT NULL DEFAULT '0',
Gosh. Any help? Thanks.
UPDATE
It has been reported to me as a BUG in MySql <= 5.1 that was fixed before MySql 5.5. I don’t have the details but I trust the source
try using the
SELECT IFNULL();http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull