开发者

MySQL: Queries for comparison

I'd like to get a record set where I can make comparison result with a relational table:

character_abilities
id    ability_id    character_id
1     1             43
2     2             43
3     5             43
4     7             43
5     8             43
6     9             43
7     1             75
8     2             75
9     3             75
10    5             75

I would like to get a result like this:

ability_id_1    character_id_1    ability_id_2    character_id_2
1               43                1               75
2               43                2               75
NULL            NULL              3             开发者_运维知识库  75
5               43                5               75
7               43                NULL            NULL
8               43                NULL            NULL
9               43                NULL            NULL

For this result I thought to do something like this but without success:

SELECT character_1.ability_id AS ability_id_1, character_1.character_id AS character_id_1, character_2.ability_id AS ability_id_2, character_2.character_id AS character_id_2
FROM character_abilities AS character_1
RIGHT JOIN character_abilities AS character_2
ON character_2.character_id = 2 AND character_1.ability_id = character_2.ability_id
WHERE character_1.character_id = 1 AND character_1.ability_id = character_2.ability_id

Unfortunately, this returns only the abilities that each character has in common.

My questions are:

Can I code a query to get each characters props like the result example? Should I make a query per character and then merge them?


If I understand the question, sounds like a candidate for a FULL OUTER JOIN, which MySQL does not have. See this question for alternatives.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜