开发者

Query same thing in multiple columns?

I have a table where there are some scores for pairs.

Like this

P1    P2    Score   Date
John  Mark  43      2011-01-01
Sinan Ash   53      2011-02-03
...
John  Suzie 34      2011-10-10
Ash   Sinan 54      2011-11-11
sinan suzie 42      2011-12-12
...

So what I want is to get all the scores for Sinan and his partner. What I am trying 开发者_运维知识库to get is something llike:

partner - score
ash       53
ash       54
suzie     42

I'm trying to do it with te query below. Is there a better way to query than

select * from table WHERE P1 = 'sinan' OR P2 = 'sinan'

Is this efficient? Maybe there is a better way to store the data in the first place. Any suggestions?


The real trick is alternating the partner between P1 and P2. The simplest approach might be:

SELECT P2 AS partner, Score
    FROM table
    WHERE P1 = 'sinan'
UNION ALL
SELECT P1 AS partner, Score
    FROM table
    WHERE P2 = 'sinan'


Here's an example using the CASE statement

SELECT CASE WHEN P1 = 'Sinan' THEN P2 ELSE P1 END AS Partner, Score
FROM ScoreTable
WHERE P1 = 'Sinan' OR P2 = 'Sinan'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜