我有一个具有指定ID,值1,值2和值3的表

MyTable1看起来像这样:

+------+--------+--------+--------+
| ID   | VALUE1 | VALUE2 | VALUE3 |
+------+--------+--------+--------+
| 1    | 103    | 202    | 3212   |
| 2    | 103    | 202    | 7667   |
| 3    | 103    | 202    | 7567   |
| 4    | 103    | 203    | 6545   |
| 5    | 103    | 203    | 9787   |
| 6    | 106    | 203    | 6545   |
| 7    | 106    | 203    | 6575   |
| 8    | 106    | 202    | 8909   |
| 9    | 106    | 206    | 4656   |
| 10   | 106    | 205    | 5676   |
| 11   | 106    | 208    | 9088   |
| 12   | 107    | 202    | 8998   |
| 13   | 107    | 205    | 5675   |
| 14   | 107    | 204    | 9787   |
| 15   | 107    | 204    | 6454   |
| 16   | 107    | 206    | 9089   |
| 17   | 107    | 202    | 1233   |
| 18   | 107    | 205    | 8765   |
| 19   | 108    | 208    | 8900   |
| 20   | 103    | 205    | 6543   |
+------+--------+--------+--------+


MyTable2看起来像这样:

+------+--------+--------+---------+
| ID   | VALUE1 | VALUE2 | STATUS  |
+------+--------+--------+---------+
| 1    | 103    | 209    | OK      |
| 2    | 105    | 203    | DEAD    |
| 3    | 107    | 205    | GOOD    |
| 4    | 104    | 202    | WRONG   |
| 5    | 103    | 203    | WOW     |
+------+--------+--------+---------+


当我想要结果时。我正在使用此代码。


  从MyTable1的SELECT DISTINCT value1,value2,其中value1 = 103


结果看起来像这样。

+--------+---------+
| VALUE1 | VALUE2  |
+--------+---------+
| 103    | 202     |
| 103    | 203     |
| 103    | 205     |
+--------+---------+


但是我想添加一个状态列,这就是MyTable2

我尝试了类似的方法,但没有得到结果。


  选择DISTINCT MyTable1.value1,MyTable1.value2,MyTable2.status从
  MyTable1,MyTable2,其中MyTable1.value1 = 103 AND MyTable2.value1 =
  MyTable1.value1和MyTable2.value2 = MyTable1.value2


无论如何,我想要这个结果。

+--------+--------+---------+
| VALUE1 | VALUE2 | STATUS  |
+--------+--------+---------+
| 103    | 202    |         |
| 103    | 203    | WOW     |
| 103    | 205    |         |
+--------+--------+---------+


谢谢!!!

最佳答案

这样的事情应该起作用:

SELECT DISTINCT
    tb1.value1,
    tb1.value2,
    tb2.status
FROM
    table1 tb1
LEFT JOIN
   table2 tb2 on tb1.value1 = tb2.value1
   and tb1.value2 = tb2.value2
WHERE
  tb1.value1 = 103

09-27 16:34