mysql - Values of generated column not appearing in table -


i'm using mysql version 5.1.41-3ubuntu12.10 (ubuntu).

mysql> show create table tt\g *************************** 1. row ***************************        table: tt create table: create table `tt` (   `pz` int(8) default null,   `os` varchar(8) default null,   `uz` int(11) not null,   `p` bigint(21) not null default '0',   `c` decimal(23,0) default null,   key `pz` (`pz`),   key `uz` (`uz`),   key `os` (`os`),   key `pz_2` (`pz`,`uz`) ) engine=myisam default charset=latin1 1 row in set (0.00 sec)  mysql> select pz,uz,pz*uz,     -> if(pz*uz,1,.5),     -> left(pz,2) pl,left(lpad(uz,5,0),2) ul,     -> p tt limit 10; +-------+----+-------+----------------+--------+----+--------+ |   pz  | uz | pz*uz | if(pz*uz,1,.5) |   pl   | ul |    p   | +-------+----+-------+----------------+--------+----+--------+ |  null |  0 |  null |            0.5 | null   | 00 |   4080 | |  null |  0 |  null |            0.5 | null   | 00 | 323754 | | 89101 |  0 |     0 |            0.5 | 89     | 00 |   6880 | |     0 |  0 |     0 |            0.5 | 0      | 00 |  11591 | | 89110 |  0 |     0 |            0.5 | 89     | 00 |     72 | | 78247 |  0 |     0 |            0.5 | 78     | 00 |     27 | | 90062 |  0 |     0 |            0.5 | 90     | 00 |      5 | | 63107 |  0 |     0 |            0.5 | 63     | 00 |      4 | |  null |  0 |  null |            0.5 | null   | 00 |  54561 | | 94102 |  0 |     0 |            0.5 | 94     | 00 |  12499 | +-------+----+-------+----------------+--------+----+--------+ 

so far good. see, 0.5 appears value of if(pz*uz,1,.5). problem is:

mysql> select os,     -> if(pz*uz,left(pz,2)<=>left(lpad(uz,5,0),2),.5) uptwo,     -> if(pz*uz,left(pz,3)<=>left(lpad(uz,5,0),3),.5) upthree,     -> sum(p) p,sum(c) c     -> tt t     -> group os,uptwo,upthree order null;  +----+-------+---------+---------+-------+ | os | uptwo | upthree |    p    |   c   | +----+-------+---------+---------+-------+ | u  |     1 |       1 |   52852 |   318 | |  |     1 |       1 | 7046563 | 21716 | | m  |     1 |       1 | 1252166 |  7337 | |  |     0 |       0 | 1830284 |  4033 | | m  |     0 |       0 |  294612 |  1714 | |  |     1 |       0 |  911486 |  3560 | | m  |     1 |       0 |  145182 |  1136 | | u  |     0 |       0 |   12144 |    23 | | u  |     1 |       0 |    1571 |     8 | +----+-------+---------+---------+-------+ 

although group uptwo, 0.5 doesn't appear in column. happened 0.5 values?


edit: noted in comments todd gibson's answer, tried with
if(pz*uz,cast(left(pz,2)<=>left(lpad(uz,5,0),2) decimal),.5) instead of
if(pz*uz,left(pz,2)<=>left(lpad(uz,5,0),2),.5), it, too, didn't work.

instead of .5 false condition of if(), use 0.5.

if(pz*uz,left(pz,2)<=>left(lpad(uz,5,0),2),0.5) uptwo  if(pz*uz,left(pz,3)<=>left(lpad(uz,5,0),3),0.5) upthree 

i believe happening after group by evaluated, values in conditional column must of same data type. since select evaluated after group by, if() converting returned values in favor of integer (from boolean expression), , 0.5 gets rounded 1 if explicitly put 0 in front of decimal place, if() treat returned values decimal including result of boolean expression (i.e. 1.0 or 0.0).

or put single quotes around .4 column values treated strings, values appear integers , decimals. values should automatically converted when used in numerical contexts (i.e. select ('2.5' * 3.5) test #8.75).


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -