I have a MySQL table field sometimes with NULL or empty values when
there is no data. When I wrote the following to count data in this
field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the
other from "TNAM=''", and also thought they should have been combined
with "group" function... Thoughts without actually update table to
turn "empty" to NULL or vise versa?
Joe
I am unable to duplicate this effect.
What is the full structure of your table, and what version of MySQL are you using?
Have you tried using either the IFNULL() or COALESCE() functions
instead of the CASE WHEN block?
On Tuesday, August 18, 2020 at 5:34:12 PM UTC-5, Lyle H. Gray wrote:
I am unable to duplicate this effect.
What is the full structure of your table, and what version of MySQL
are you using?
UNIX> mysql --version
Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper
MYSQL> desc jcrosstbl;
| Field | Type | Null | Key | Default | Extra
| |
+----------------+--------------+------+-----+---------+--------------- --------------+
| gn_ID | varchar(12) | NO | PRI | |
| | TNAM | varchar(96) | YES | | NULL |
| | Descrptn | varchar(512) | YES |
| | NULL | | created |
| datetime | YES | | NULL | |
| updated | datetime | YES | | NULL | on update
| CURRENT_TIMESTAMP | status | int(1) | YES | | NULL
| |
It also duplicates when I port the table to an older server where
MySQL is Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (x86_64) using readline 5.1.
If I do
mysql> update jcrosstbl set TNAM=NULL where TNAM='';
the duplicates get combined.
joe
"Lyle H. Gray" <lylefitzw@no.spam.gmail.com.invalid> wrote in news:XnsAC1E8D2852FE4graynoibisspamcsumas@216.166.97.131:
Have you tried using either the IFNULL() or COALESCE() functions
instead of the CASE WHEN block?
Of course, that would only work if chose to convert a NULL to a blank to match the other blanks, rather than 'default', so that might not be an
option for you...
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise versa?
Joe
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise versa?
On 18/08/2020 22.33, Joe wrote:
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |Maybe you should look at what values you have, try a "select distinct
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise versa?
select concat('"',TNAM,'"') from ITEMS", it could show that you have a
TNAM which begins with default which could hold an extra space
(depending on mysql version, the extra space could be treated differently).
--
//Aho
On Wednesday, August 19, 2020 at 5:23:21 PM UTC-5, J.O. Aho wrote:
On 18/08/2020 22.33, Joe wrote:
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to count data in this field:Maybe you should look at what values you have, try a "select distinct
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise versa?
select concat('"',TNAM,'"') from ITEMS", it could show that you have a
TNAM which begins with default which could hold an extra space
(depending on mysql version, the extra space could be treated differently).
I did:
mysql> select distinct (select concat('"',TNAM'"')) from ITEMS order by 1; and here is output:
+-----------+
| NULL |
| "" |
+-----------+
among other values. So 'group' treats NULL and "nothing" ('') as separate values - perhaps that's where the mystery is.
I already translated them both to "default"; now all I need to do is to combine the two "default"s.
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to
count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should
have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise
versa?
In article <6266a024-14d7-43a9-b1bd-0730725130dan@googlegroups.com>,
Joe <zhilianghu@gmail.com> wrote:
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to
count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should
have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise
versa?
What does "GROUP BY 1" even mean???
On 20.08.2020 11:28, Tony Mountifield wrote:
In article <6266a024-14d7-43a9-b1bd-0730725130dan@googlegroups.com>,
Joe <zhilianghu@gmail.com> wrote:
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to
count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should
have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise
versa?
What does "GROUP BY 1" even mean???
It's a MySQL extension. In ORDER BY and GROUP BY you can refer to the n-th field in the SELECT list by saying just n. A clean formulation of the query would have been to give an alias to the calculated first field and then use that alias name in the GROUP BY clause.
But I doubt that this is the reason for the observed behavior. For me this looks like an ordinary bug. This is confirmed by the fact that others cannot reproduce it and that it is only observed in versions from bronze age (5.6) or even stone age (5.0). 5.6 will become EOL next February.
On 20-8-2020 12:47, Axel Schwenke wrote:
On 20.08.2020 11:28, Tony Mountifield wrote:
In article <6266a024-14d7-43a9-b1bd-0730725130dan@googlegroups.com>,
Joe <zhilianghu@gmail.com> wrote:
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to
count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should
have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise
versa?
What does "GROUP BY 1" even mean???
It's a MySQL extension. In ORDER BY and GROUP BY you can refer to the n-th field in the SELECT list by saying just n. A clean formulation of the query would have been to give an alias to the calculated first field and then use that alias name in the GROUP BY clause.
But I doubt that this is the reason for the observed behavior. For me this looks like an ordinary bug. This is confirmed by the fact that others cannot
reproduce it and that it is only observed in versions from bronze age (5.6) or even stone age (5.0). 5.6 will become EOL next February.
3 options to get the correct result, followed by the 4th way...
In article <6266a024-14d7-43a9...@googlegroups.com>,
Joe <zhili...@gmail.com> wrote:
I have a MySQL table field sometimes with NULL or empty values when there is no data. When I wrote the following to
count data in this field:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
I got:
| default | 2929 |What does "GROUP BY 1" even mean???
| default | 139 |
| item A | 347 |
| item B | 831 |
....
-- why "default" repeats? I humble one is from "TNAM is NULL" and the other from "TNAM=''", and also thought they should
have been combined with "group" function... Thoughts without actually update table to turn "empty" to NULL or vise
versa?
Or you could say:
SELECT IFNULL(NULLIF(TNAM, ''), 'default') AS tnam2, COUNT(*)
FROM ITEMS
GROUP BY tnam2;
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
On 08/18/2020 10:33 PM, Joe wrote:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
What happens if you do:
select CASE WHEN (TNAM is NULL || TNAM=''|| TNAM='default') THEN 'default'
ELSE TNAM END, count(1)
from ITEMS group by 1;
On 08/18/2020 10:33 PM, Joe wrote:
select CASE WHEN (TNAM is NULL || TNAM='') THEN 'default'What happens if you do:
ELSE TNAM END, count(1)
from ITEMS group by 1;
select CASE WHEN (TNAM is NULL || TNAM=''|| TNAM='default') THEN 'default' ELSE TNAM END, count(1)
from ITEMS group by 1;
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 185 |
Nodes: | 16 (1 / 15) |
Uptime: | 04:59:25 |
Calls: | 3,676 |
Calls today: | 2 |
Files: | 11,149 |
Messages: | 3,447,353 |