select Customers.cust_name,Orders.oder_num from Customers inner join Orders on Customers.cust_id == Orders.cust_id order by Customers.cust_name desc这句SQL有什么语法错误
时间: 2024-01-13 22:05:24 浏览: 110
这句 SQL 语句看起来是正确的,但是有一个小错误。在 SQL 中,"==" 不是用于比较相等的运算符,而是 " = "。因此,正确的 SQL 语句应该是:
```sql
SELECT Customers.cust_name, Orders.oder_num
FROM Customers
INNER JOIN Orders ON Customers.cust_id = Orders.cust_id
ORDER BY Customers.cust_name DESC
```
这将按照顾客的名称(以降序排列)和订单号选择顾客和订单信息,并将它们结合在一起。
相关问题
select * from ( select row_.*, rownum rownum_ from ( select * from ( select distinct OB.BUSI_ORDER_ID, 0 as HIS_ID, OB.BUSI_CODE, OB.CUST_ID, OB.CEASE_REASON, OB.ORDER_STATE, OB.CHANNEL_TYPE, ob.user_id, OB.IS_BATCH_ORDER, OB.APPLICATION_ID, OB.CREATE_DATE, OB.DONE_DATE, OB.EFF_DATE, OB.EXP_DATE, OB.OPER_ID, OB.ORG_ID, OB.REGION_ID, OB.NOTE, OB.PROCESS_STATE, nvl(oi.cust_name, ic.cust_name) cust_name, nvl(oc.icc_id, iu.icc_id) icc_id, nvl(oc.svc_num, iu.svc_num) svc_num, icp.cust_name parent_cust_name, icp.cust_id parent_cust_id, ol.order_list_id from ord_busi ob left join ord_offer oo on oo.busi_order_id = ob.busi_order_id and ob.user_id = oo.user_id left join info_user iu on oo.user_id = iu.user_id left join info_cust ic on ob.cust_id = ic.cust_id left join ord_cust oi on ob.cust_id = oi.cust_id and ob.busi_order_id = oi.busi_order_id left join info_cust icp on nvl(ic.parent_cust_id, oi.parent_cust_id) = icp.cust_id left join ( SELECT * FROM ord_user WHERE user_order_id IN ( SELECT MAX(user_order_id) user_order_id FROM ord_user GROUP BY busi_order_id,user_id ) ) oc on ob.user_id = oc.user_id and ob.busi_order_id = oc.busi_order_id left join ord_list ol on ob.busi_order_id = ol.busi_order_id WHERE 1 = 1 and OB.CUST_ID IN( SELECT DISTINCT CUST_ID FROM (SELECT CUST_ID, PARENT_CUST_ID FROM INFO_CUST UNION SELECT CUST_ID, PARENT_CUST_ID FROM ORD_CUST) TMP START WITH TMP.CUST_ID = '10001009208' CONNECT BY TMP.PARENT_CUST_ID = PRIOR TMP.CUST_ID ) union all select distinct OB.BUSI_ORDER_ID,OB.HIS_ID, OB.BUSI_CODE, OB.CUST_ID, OB.CEASE_REASON, OB.ORDER_STATE, OB.CHANNEL_TYPE, OB.USER_ID, OB.IS_BATCH_ORDER, OB.APPLICATION_ID, OB.CREATE_DATE, OB.DONE_DATE, OB.EFF_DATE, OB.EXP_DATE, OB.OPER_ID, OB.ORG_ID, OB.REGION_ID, OB.NOTE, OB.PROCESS_STATE, nvl(oi.cust_name,ic.cust_name) cust_name , nvl(oc.icc_id, iu.icc_id) icc_id, nvl(oc.svc_num,iu.svc_num) svc_num, icp.cust_name parent_cust_name, icp.cust_id parent_cust_id, ol.order_list_id from ord_busi_his ob left join ord_offer_his oo on oo.busi_order_id = ob.busi_order_id 优化一下
可以考虑以下优化方式:
1. 使用INNER JOIN代替LEFT JOIN:如果确保两个表中都含有匹配的行,则使用INNER JOIN可以提高查询效率。
2. 使用EXISTS代替IN:EXISTS执行速度比IN更快,因为它只需要找到匹配的第一个结果,而不是找到所有匹配的结果。
3. 简化子查询:在查询中可以尝试简化子查询,以减少查询的复杂度。
4. 使用索引:使用索引可以提高查询效率,可以根据查询中经常使用的列添加索引。
5. 避免使用SELECT *:只查询需要的列,避免使用SELECT *可以减少查询的复杂度,提高查询效率。
编写存储过程 SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM JOUR1.Om_Subscriber_202301 a LEFT JOIN JOUR1.om_order_202301 b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id UNION ALL SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM JOUR1.Om_Subscriber_202302 a LEFT JOIN JOUR1.om_order_202302 b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id UNION ALL SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM JOUR1.Om_Subscriber_202303 a LEFT JOIN JOUR1.om_order_202303 b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id UNION ALL SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM JOUR1.Om_Subscriber_202304 a LEFT JOIN JOUR1.om_order_202304 b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id
你可以编写一个存储过程来执行这个查询。下面是一个示例:
```sql
CREATE OR REPLACE PROCEDURE get_data AS
BEGIN
-- 创建临时表来保存结果
CREATE GLOBAL TEMPORARY TABLE temp_data (
order_id NUMBER,
done_date DATE,
cust_id NUMBER,
subscriber_ins_id NUMBER,
access_num VARCHAR2(100),
dev_id NUMBER,
dev_name VARCHAR2(100),
corp_org VARCHAR2(100),
company_name VARCHAR2(100),
department_code VARCHAR2(100),
department_name VARCHAR2(100),
remarks VARCHAR2(100)
) ON COMMIT PRESERVE ROWS;
-- 第一个查询
INSERT INTO temp_data
SELECT a.order_id,
a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_202301 a
LEFT JOIN JOUR1.om_order_202301 b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id;
-- 后续查询使用UNION ALL连接
INSERT INTO temp_data
SELECT a.order_id,
a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_202302 a
LEFT JOIN JOUR1.om_order_202302 b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id;
INSERT INTO temp_data
SELECT a.order_id,
a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_202303 a
LEFT JOIN JOUR1.om_order_202303 b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id;
INSERT INTO temp_data
SELECT a.order_id,
a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_202304 a
LEFT JOIN JOUR1.om_order_202304 b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id;
-- 查询结果
SELECT * FROM temp_data;
-- 删除临时表
DROP TABLE temp_data;
END;
/
```
这个存储过程创建了一个临时表 `temp_data` 来保存查询结果,并将每个查询的结果插入到临时表中。最后,通过查询 `temp_data` 表来获取结果。
请注意,根据你的实际需求和数据库结构,你可能需要修改表和列的定义,以及查询的逻辑。确保在执行存储过程之前,已经创建了所需的表和索引。
阅读全文