简体   繁体   English

通过消除条件显示结果的 SQL 查询

[英]SQL Query on displaying a result by eliminating on a condition

I am trying to write a SQL query for this sample input and output:我正在尝试为此示例输入和输出编写 SQL 查询:这里 with the following condition: The hierarchy will be the same as below具有以下条件:层次结构将与以下相同

  1. Remove the records where code = PK and filter is not blank删除 code = PK 且 filter 不为空的记录
  2. Update the records having code = PK and filter as blank to 'XXXX'更新代码 = PK 的记录并将空白过滤为“XXXX”

Can someone help me on this?有人可以帮我吗?

I could try only the following code for 1st condition, but this is removing all the records and giving incorrect result:对于第一个条件,我只能尝试以下代码,但这会删除所有记录并给出不正确的结果:

 select *,
 from table t1
 where code='PK' and length('filter')=0
SELECT T1.KEY,
       T1.CODE,
       CASE
           WHEN CODE = 'PK' AND COALESCE(FILTER,0) = 0  THEN 'XXXX'
           ELSE T1.FILTER 
       END AS FILTER
FROM TABLE T1
WHERE CODE != 'PK' OR (CODE='PK' AND COALESCE(FILTER,0) = 0)

In your query length('filter') returns 6在您的查询中length('filter')返回6

select key, code, IF(code = 'PK', 'XXXX', filter) as filter
from table_t1
where (
    code != 'PK' 
    OR 
    code = 'PK' AND (filter IS NULL OR filter = '')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM