MySQL:查询比较

时间:2011-02-02 23:03:08

标签: mysql comparison

我想获得一个记录集,我可以用关系表来比较结果:

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

我想得到这样的结果:

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

对于这个结果,我想做这样的事情,但没有成功:

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

不幸的是,这只返回每个角色的共同能力。

我的问题是:
我可以编写查询来获取每个字符道具,如结果示例吗? 我应该为每个字符进行查询然后将它们合并吗?

1 个答案:

答案 0 :(得分:0)

如果我理解这个问题,听起来像是一个完整的外部联接的候选者,MySQL没有。 See this question for alternatives.