简体   繁体   English

带有subString的sql

[英]sql with subString

I have 2 fields in DB as follows: 我在DB中有2个字段,如下所示:

table: t_doc_met
t_doc   t_num
PI  200923712291920BM
OI  200923712291920BM
OD  200923712301921OP
PD  200923712301921MO
MR  200923712301921F
BR  200923712305622BM
MR  200923712305622F
OB  200923712305622M1
OR  200923712305622D
MR  200923712314324M1
MR  200923712314324MO
BR  200923712314324BM
PI  200923712314325OP
OI  20092371231433MO

I want to pick all records in this table whose t_doc is in ('PD','OB','OD','OR','MR'). 我想选择此表中t_doc位于的所有记录(“ PD”,“ OB”,“ OD”,“ OR”,“ MR”)。 Also pick t_doc IN ('BR','PI','OI') whose t_num ends with 'BM' 还选择t_doc IN('BR','PI','OI'),其t_num以'BM'结尾

select * from t_doc_met where 
t_doc IN ('PD','OB','OD','OR','MR') AND 
( t_doc IN ('BR','PI','OI') AND SUBSTR(t_num, LENGTH(t_num) - 1, 2) IN ('BM'))

You have an AND in place of OR : 您用AND代替OR

SELECT *
FROM t_doc_met
WHERE
    (t_doc IN ('PD','OB','OD','OR','MR'))
    OR 
    (t_doc IN ('BR','PI','OI') AND SUBSTR(t_num, -2) IN ('BM'))

Note that Oracle's SUBSTR takes negative indexes to mean "start from the back". 请注意,Oracle的SUBSTR采用负索引表示“从后开始”。 Hence I rewrote your 因此,我改写了你的

SUBSTR(t_num, LENGTH(t_num) - 1, 2) IN ('BM')

as

SUBSTR(t_num, -2) IN ('BM')

I am going to interpret the question as saying: You want all t_docs 我将问题解释为:您需要所有t_docs

  • that do not end in bm 不以bm结尾
  • that have a row with t_doc IN ('PD', 'OB', 'OD', 'OR', 'MR') 带有t_doc IN('PD','OB','OD','OR','MR')的行
  • that have a row with t_doc IN ('BR','PI','OI') 带有t_doc IN('BR','PI','OI')的行

If this is correct: 如果正确的话:

select dm.t_num
from t_doc_met dm
where t_num LIKE '%BM'
group by t_num
having sum(case when t_doc IN ('PD', 'OB', 'OD', 'OR', 'MR') then 1 else 0 end) > 0 and
       sum(case when t_doc IN ('BR','PI','OI') then 1 else 0 end) > 0;

Alternatively, you might just want or : 或者,您可能只想or

select dm.t_num
from t_doc_met dm
where (t_doc IN ('PD', 'OB', 'OD', 'OR', 'MR')) or
      (t_num LIKE '%BM' and ('BR', 'PI', 'OI'));

You are looking for rows where t_doc is in 'PD','OB','OD','OR','MR'; 您正在寻找t_doc位于'PD','OB','OD','OR','MR'的行; and at the same time is in 'BR','PI','OI'. 并且同时位于“ BR”,“ PI”,“ OI”中。 Those are mutually exclusive. 这些是互斥的。

You appear to just want the first AND to be an OR : 您似乎只希望第一个ANDOR

select * from t_doc_met where 
t_doc IN ('PD','OB','OD','OR','MR') OR 
( t_doc IN ('BR','PI','OI') AND SUBSTR(t_num, -2) IN ('BM'));

T_ T_NUM            
-- -----------------
PI 200923712291920BM
OI 200923712291920BM
OD 200923712301921OP
PD 200923712301921MO
MR 200923712301921F 
BR 200923712305622BM
MR 200923712305622F 
OB 200923712305622M1
OR 200923712305622D 
MR 200923712314324M1
MR 200923712314324MO
BR 200923712314324BM

12 rows selected.

I've also simplified your substr's second argument, as dasblinkenlight mentioned you could do. 正如dasblinkenlight提到的,我还简化了substr的第二个参数。

If this is part of a bigger query then you need to make sure that the conditions are enclosed in parentheses appropriately, so the parser can interpret the overall logic you intend: 如果这是较大查询的一部分,则需要确保将条件适当地括在括号中,以便解析器可以解释您想要的总体逻辑:

select * from t_doc_met where 
-- some other conditions
AND (t_doc IN ('PD','OB','OD','OR','MR') OR 
  (t_doc IN ('BR','PI','OI') AND SUBSTR(t_num, -2) IN ('BM')));

I have created the sample database for you with same data records that you have provided. 我已经使用您提供的相同数据记录为您创建了示例数据库。

Table: 表: 在此处输入图片说明

Hope this query helps you, modify the "AND/OR" based on your requirements. 希望此查询对您有所帮助,根据您的要求修改“ AND / OR”。

SELECT * FROM categories WHERE categoryName in  ('PD','OB','OD','OR','MR')  OR (categoryName in ('BR','PI','OI') and description like '%BM' )

I have used OR condition to get both the record set of t_doc is in ('PD','OB','OD','OR','MR'). 我已经使用OR条件来获取t_doc的两个记录集都在('PD','OB','OD','OR','MR')中。 Also pick t_doc IN ('BR','PI','OI') whose t_num ends with 'BM' 还选择t_doc IN('BR','PI','OI'),其t_num以'BM'结尾

Note: I have modified the Table Name and column names 注意:我已经修改了表名和列名

在此处输入图片说明

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

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