SQL select statement in where clause

假装没事ソ 提交于 2019-12-12 02:57:08

问题


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.


回答1:


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' 



回答2:


No need for the select in the where clause

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



回答3:


Try this:

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



回答4:


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')



回答5:


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


来源:https://stackoverflow.com/questions/30234247/sql-select-statement-in-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!