[英]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. 所以我想要获得Sinan和他的伴侣的所有分数。 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. 我正在尝试使用下面的te查询来做到这一点。 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. 真正的诀窍是在P1和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 这是使用CASE语句的示例
SELECT CASE WHEN P1 = 'Sinan' THEN P2 ELSE P1 END AS Partner, Score
FROM ScoreTable
WHERE P1 = 'Sinan' OR P2 = 'Sinan'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.