gpt4 book ai didi

mysql - 左/右连接引用另一个表

转载 作者:行者123 更新时间:2023-11-29 06:50:46 30 4
gpt4 key购买 nike

我正在尝试访问三个表以获得类似这样的内容:

+------+------+------+
| ITEM | PCS | CSS |
+------+------+------+
| 1099 | 2 | 11 |
| 1098 | 2 | 2 |
| 1097 | 2 | 1 |
| 1096 | NULL | NULL |
| 1095 | NULL | NULL |
| 1094 | NULL | NULL |
| 1093 | NULL | NULL |
| 1092 | NULL | NULL |
+------+------+------+

但是,我得到的只是:

+------+------+------+
| ITEM | PCS | CSS |
+------+------+------+
| 1099 | 2 | 11 |
| 1098 | 2 | 2 |
| 1097 | 2 | 1 |
+------+------+------+

//我对我的查询有点困惑所以这里是我尝试做的事情的分割:

SELECT CODE
FROM SET2
WHERE MO = MONTH(SYSDATE())
AND YR = YEAR(SYSDATE())

//and then:

SELECT ITEM, PCS, CSS
FROM SET3
WHERE S3.CODE=S2.CODE

//then:

SELECT ITEM
FROM SET1
WHERE S1.ITEM=S3.ITEM

我的 table

SET1

+------+
| ITEM |
+------+
| 1099 |
| 1098 |
| 1097 |
| 1096 |
| 1095 |
| 1094 |
| 1093 |
| 1092 |
+------+

第二组

+------+-----+------+
| YR | MO | CODE |
+------+-----+------+
| 2013 | 1 | 1 |
| 2013 | 2 | 2 |
+------+-----+------+

第三组

+------+-----+------+------+
| PCS | CSS | CODE | ITEM |
+------+-----+------+------+
| 2 | 11 | 1 | 1099 |
| 2 | 2 | 1 | 1098 |
| 2 | 1 | 1 | 1097 |
+------+-----+------+------+

最佳答案

你必须使用左连接才能工作:

SELECT SET1.ITEM, PCS, CSS
FROM SET1
LEFT JOIN SET3 ON SET3.ITEM = SET1.ITEM
LEFT JOIN SET2 ON SET2.CODE = SET3.CODE
AND SET2.MO = MONTH(SYSDATE())
AND SET2.YR = YEAR(SYSDATE())

关于mysql - 左/右连接引用另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15539065/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com