Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

And + Or in one Select-Statement

In my select statement I chain different conditions with AND. Now I need for one condition an OR. How can I add this for just this one Attribute without affecting the other AND-statements before? That's my coding:

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio WHERE pdatv GE pa_begda AND pdatb LE pa_endda AND abrec EQ '2'.

For the last condition abrec EQ '2' I need an OR as well, like abrec EQ '2' OR '3'. How can I add this the best way without affecting the other ANDs?

Thanks for your hints!

like image 667
Dyrdek Avatar asked Sep 16 '25 09:09

Dyrdek


2 Answers

Use IN clause instead:

SELECT pernr reinr pdatv pdatb pdvrs abrec 
  FROM PTRV_PERIO
  INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio
 WHERE pdatv GE pa_begda
   AND pdatb LE pa_endda
   AND abrec IN ('2', '3').

The other valid alternative is:

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO
  INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio
 WHERE pdatv GE pa_begda
   AND pdatb LE pa_endda
   AND ( abrec EQ '2' OR abrec EQ '3' ).
like image 50
Suncatcher Avatar answered Sep 19 '25 05:09

Suncatcher


Use parentheses:

EDIT: Seems like you need to add spaces after and before the brackets See this related question.

SELECT pernr reinr pdatv pdatb pdvrs abrec FROM PTRV_PERIO
INTO CORRESPONDING FIELDS OF TABLE lt_ptrv_perio
WHERE pdatv GE pa_begda
AND   pdatb LE pa_endda
AND   ( abrec EQ '2' OR abrec EQ '3' )
like image 29
pixelarbeit Avatar answered Sep 19 '25 06:09

pixelarbeit