M
M
Max Payne2018-06-22 16:13:37
MySQL
Max Payne, 2018-06-22 16:13:37

How to use GROUP BY to group two columns, one of which can be empty?

What is the correct way to use GROUP BY to group two columns, one of which can be empty?
For example, the structure:

command | command_text
привет | 
привет | иван

If I write GROUP BY `command` + `command_text`or GROUP BY `command`, `command_text`-- the results disappear with an empty `command_text`
`command` - varchar
`command_text` - varchar

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vapaamies, 2018-06-22
@vapaamies

On normal fields it seems to work without problems.

select count(*) cnt, hello, name from (
  select 'привет' hello, null name
  union all
  select 'привет', 'иван'
  union all
  select 'привет', 'иван'
) tab
group by
  hello, name;

It's strange to me that BLOB fields can participate in group by at all .

N
nozzy, 2018-06-22
@nozzy

select
command,
command_text
from your_table
where length(command_text) <> 0
group by command, command_text
union
select
command,
command_text
from your_table
where length(command_text) = 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question