SELECT t1.CNNAME, t2.CNNAME AS PCNNAME FROM bo_act_dict_kv_item t1 RIGHT JOIN bo_act_dict_kv_item t2 ON t1.EXTTEXT1 = t2.ITEMNO WHERE t1.DICTKEY = 'PLM_INITIALBOM_STATUS' AND t1.EXTTEXT1 != '' ;这条sql怎么改
时间: 2024-03-02 13:52:33 浏览: 50
要根据具体的需求来改写这条 SQL 语句,以下是几种可能的修改方式:
1. 如果要查询所有的 t1.CNNAME 和对应的 t2.CNNAME,无论是否存在对应关系,可以改成左连接,如下所示:
```
SELECT t1.CNNAME, t2.CNNAME AS PCNNAME
FROM bo_act_dict_kv_item t1
LEFT JOIN bo_act_dict_kv_item t2 ON t1.EXTTEXT1 = t2.ITEMNO
WHERE t1.DICTKEY = 'PLM_INITIALBOM_STATUS' AND t1.EXTTEXT1 != '';
```
2. 如果只需要查询存在对应关系的 t1.CNNAME 和对应的 t2.CNNAME,可以改成内连接,如下所示:
```
SELECT t1.CNNAME, t2.CNNAME AS PCNNAME
FROM bo_act_dict_kv_item t1
INNER JOIN bo_act_dict_kv_item t2 ON t1.EXTTEXT1 = t2.ITEMNO
WHERE t1.DICTKEY = 'PLM_INITIALBOM_STATUS' AND t1.EXTTEXT1 != '';
```
3. 如果需要查询 t1.CNNAME 和对应的 t2.CNNAME,并且 t2.CNNAME 不为空,可以在内连接的基础上增加一个条件,如下所示:
```
SELECT t1.CNNAME, t2.CNNAME AS PCNNAME
FROM bo_act_dict_kv_item t1
INNER JOIN bo_act_dict_kv_item t2 ON t1.EXTTEXT1 = t2.ITEMNO
WHERE t1.DICTKEY = 'PLM_INITIALBOM_STATUS' AND t1.EXTTEXT1 != '' AND t2.CNNAME IS NOT NULL;
```
需要根据实际情况来选择适合的修改方式。
阅读全文