[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

How to write SQL with and or

Hi

I have a query need to select the result with
transactiontypeid = 1,2,3,4 and status=0 and alltranid=0

But, the following query select status=1 and alltranid=1

How should i solve it?

select [id]=t.id, [alltranid], [transactiontypeid], [type]=r.type, [clientid]=t.clientid, [nickname]=c.nickname, [currency]=t.currency, [ucurrency]=u.currency,[status]
from alltransaction t inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency 
inner join transactiontype r on t.transactiontypeid = r.id 
where alltranid = 0 and status=0 and transactiontypeid=1 or transactiontypeid=2 
or transactiontypeid=3 or transactiontypeid=4
order by [id] desc

Open in new window

0
techques
Asked:
techques
  • 2
1 Solution
 
RiteshShahCommented:
try this.
select [id]=t.id, [alltranid], [transactiontypeid], [type]=r.type, [clientid]=t.clientid, [nickname]=c.nickname, [currency]=t.currency, [ucurrency]=u.currency,[status]
from alltransaction t inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency 
inner join transactiontype r on t.transactiontypeid = r.id 
where alltranid = 0 and status=0 and transactiontypeid in (1,2,3,4)
order by [id] desc

Open in new window

0
 
RiteshShahCommented:
this may work as well.



select [id]=t.id, [alltranid], [transactiontypeid], [type]=r.type, [clientid]=t.clientid, [nickname]=c.nickname, [currency]=t.currency, [ucurrency]=u.currency,[status]
from alltransaction t inner join client c on t.clientid = c.id 
inner join currency u on t.currency = u.currency 
inner join transactiontype r on t.transactiontypeid = r.id 
where alltranid = 0 and status=0 and (transactiontypeid=1 or transactiontypeid=2 
or transactiontypeid=3 or transactiontypeid=4)
order by [id] desc

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now