如果value ='E'ELSE value ='N',MySQL SELECT记录WHERE

时间:2015-07-03 14:02:26

标签: mysql sql

想象一下下表:

| my_id | my_status |        my_title |               my_desc |
|-------|-----------|-----------------|-----------------------|
|     1 |         N |     Hello World | This is a description |
|     2 |         N |     Hello again |             Blah blah |
|     3 |         N |     This is fun |  I like StackExchange |
|     3 |         E | This edited fun |  I love StackExchange |
|     4 |         N |         Goodbye |     Last record here. |

正常记录在my_status列中有“N”。编辑条目时,我创建了一个标有“E”的新记录。编辑批准后,我删除原始N记录并将E记录更新为N记录。它基本上是一个编辑审批系统。

现在我想查询该表并显示具有标记为'E'的唯一ID的记录(如果存在),否则返回标准的'N'记录。所以最终的结果应该是:

| my_id | my_status |        my_title |               my_desc |
|-------|-----------|-----------------|-----------------------|
|     1 |         N |     Hello World | This is a description |
|     2 |         N |     Hello again |             Blah blah |
|     3 |         E | This edited fun |  I love StackExchange |
|     4 |         N |         Goodbye |     Last record here. |

我确信这在一个查询中必须是可能的,但它现在正在躲避我。 (这是星期五下午,在办公室33度,我的大脑正在融化)。

我想象的是

SELECT * FROM my_table WHERE IF EXISTS (my_status='E') ELSE (my_status='N') 

(我知道这不是有效的SQL,但它有希望表明我想要实现的目标)。

SQL小提琴:http://sqlfiddle.com/#!9/1b049/1

5 个答案:

答案 0 :(得分:2)

您可以按ID分组,然后为每个ID采取最小的状态('N'大于'E')

select t1.*
from my_table t1
join
(
  SELECT my_id, min(my_status) as status 
  FROM my_table 
  GROUP BY my_id
 ) t2 on t1.my_id = t2.my_id and t1.my_status = t2.status

SQLFiddle demo

答案 1 :(得分:1)

一种可能的解决方案是确保不存在任何具有相同my_id的行和my_status =' E':

SELECT * FROM my_table t
where not (my_status = 'N' and exists (
  select 1 from my_table where my_id = t.my_id 
  and my_status = 'E')
)

SQL Fiddle

答案 2 :(得分:1)

我认为一种简单的方法是使用union all

select f.*
from following f
where f.status = 'E'
union all
select f.*
from following f
where not exists (select 1
                  from following f2
                  where f2.my_id = f.my_id and f2.status = 'E'
                 );

第一个子查询获取所有E个。第二个获得没有相应N的所有Estatusmy_id, status上的索引的效果非常好。

答案 3 :(得分:1)

分组依据和MIN函数(因为'E'<'N'):

SELECT my_id, MIN(my_status), my_title, my_desc FROM my_table group by my_id

答案 4 :(得分:1)

比juergen的回答更不优雅:

SELECT * FROM my_table AS t
INNER JOIN (SELECT my_id, COUNT(*) AS cnt FROM my_table GROUP BY 1) AS t2
  ON t.my_id = t2.my_id
WHERE my_status = "E" OR t2.cnt = 1;

这意味着基本上说“如果它是'E',否则检查是否只有一行使用此my_id,如果是,则表示只有'N'行”。