开发者

Get rows from table in DB using SQL

I want to get rows from database. I am using the following query:

select * from Table where  columnB = '1' AND '3';

I want to get rows in which column A has the same value and columnB is either 1 and 3. I don't want any single row of columnA.

I want to retrieve rows in pairs on the basis of value in column A. There should no single row with value of 开发者_JS百科column A


If I understood you correctly

SELECT columnA, columnB 
FROM Table
WHERE columnA = 'SOME_VALUE'
  AND columnB IN ('1', '3')


EDIT: Still not sure I understand your question, but...

if you want all rows with B='1' such that there's also another row with B='3' and both rows have the same value of A, as well as all rows with B='3' such that there's also another row with B='1' and both rows have the same value of A, then something like this should work:

select * from Table as t1
where columnB = '1'
  and exists (
    select * from Table as t2
    where t2.columnA = t1.columnA
      and t2.columnB = '3'
  )
UNION
select * from Table as t3
where columnB = '3'
  and exists (
    select * from Table as t4
    where t4.columnA = t3.columnA
      and t4.columnB = '1'
  )

Your output will be something like this:

| ColumnA | ColumnB |
|   foo   |    1    |
|   foo   |    3    |
|   bar   |    1    |
|   bar   |    3    |
|   cat   |    1    |
|   cat   |    3    |
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜