1

First of all I'm using mysql 5.6

I want to select all the elements that satisfy at least one of these request:

  1. Do not have a certain field.
  2. A certain field has an empty value (for empty I mean: len(trim(value)) == 0)

I'll make an example to be more clear

table2
id|field|value
1 |x    |12
1 |y    |23
1 |z    |34
2 |x    |45
2 |y    |56
2 |z    |  <---- is an empty string with 0/1 spaces
3 |x    |67

So what I want is for example if I want all field that do not have field z I should obtain (2,3) In case of all fields without y I should obtain (3)

This is what I tried without much success:

SELECT t12.id 
FROM table2 AS t12
WHERE NOT IN ( 
    SELECT t2.id 
    FROM table2 as t2
    WHERE t2.field = 'y' 
        AND t2.value <> '' AND t2.value IS NOT NULL
)
Timmy
  • 693
  • 2
  • 8
  • 26

1 Answers1

0
select id from t group by id
having min(len(value)) = 0 or count(case when field = 'x' then 1 end) = 0

Combine multiple conditions in the case if the criteria must be met by a single row rather than the whole group.

shawnt00
  • 16,443
  • 3
  • 17
  • 22