我想选择具有不同行但具有相同id的数据。

| Codenum | Letter | description |
|     001 | A      | APPLES      |
|     002 | A      | BANANA      |
|     001 | B      | RED         |
|     001 | B      | GREEN       |
|     002 | B      | BLUE        |
|     002 | B      | YELLOW      |

我想得到的是分类为字母B的行,以及具有相同代码的字母A的描述。
不完整的查询是SELECT description FROM table where letter = B,我想添加与Codenum对应的字母A的描述
预期结果:
001    B   RED     APPLES
001    B   GREEN   APPLES
002    B   BLUE    BANANA
002    B   YELLOW  BANANA

提前多谢了

最佳答案

试试这个

select a.codenum, a.letter, a.description, b.description
from table a join table b
   on b.codenum = a.codenum
      and b.letter = 'A'
where a.letter = 'B'

10-04 19:43