user3456401
user3456401

Reputation: 81

SQL select statement in where clause

Hi there I am trying to execute a query but cannot seem to get it right.

SELECT *
FROM   table
WHERE  id IN (SELECT *
              FROM   table
              WHERE  description = 'A')
AND description = 'B' 

Above is the query that I have got, the select * from table where description = A works as expected when ran alone I just need to make the where clause to work so I can see any id that has a description of A and B.

Upvotes: 0

Views: 7365

Answers (5)

murti
murti

Reputation: 1

SELECT distinct AnaTablo.Id , AnaTablo.FirmaAdi , AnaTablo.FirmaId , AnaTablo.KayitTarihi ,
users.Email Personel,   (SELECT top 1 sabitler.Ayar from tblSabitAyarlar sabitler WHERE sabitler.Tur = 29 and sabitler.Deger in 
(SELECT top 1 IslemId from tblEFaturaTakipIslem Islem WHERE AnaTablo.Id = Islem.EFaturaTakipId order by KayitTarihi desc))YapilanIslem,
AnaTablo.Eposta , AnaTablo.Aciklama
from tblEFaturaTakip AnaTablo left join AspNetUsers users on AnaTablo.PersonelId  = users.Id

Upvotes: 0

SimonPJ
SimonPJ

Reputation: 766

You will be getting multiple columns from the sub query when I assume you only want the id column:

SELECT *
FROM   table
WHERE  id IN (SELECT id
          FROM   table
          WHERE  description = 'A')
   AND description = 'B' 

Upvotes: 3

Darian
Darian

Reputation: 348

Try this:

SELECT *
FROM table
WHERE description IN ('A', 'B')

Upvotes: 2

diufanman
diufanman

Reputation: 201

it should be:

select * from table where id in (select id from table where description = 'A') and description = 'B'

but this query will give you zero result as you select records with description = 'A' and description = 'B', if you want to get records with either description of A or B, then you should write as

select * from table where description = 'A' or description = 'B'

or

select * from table where description in ('A','B')

Upvotes: 0

Matt
Matt

Reputation: 15071

No need for the select in the where clause

SELECT *
FROM table
WHERE id IN ('A', 'B')

Upvotes: 2

Related Questions