order by子句,locations表中没有country_name,父母如何通过country_name查询订单?

时间:2018-03-23 20:01:35

标签: sql oracle subquery scalar

desc locations

Name           Null?    Type         
-------------- -------- ------------ 
LOCATION_ID    NOT NULL NUMBER(4)    
STREET_ADDRESS          VARCHAR2(40) 
POSTAL_CODE             VARCHAR2(12) 
CITY           NOT NULL VARCHAR2(30) 
STATE_PROVINCE          VARCHAR2(25) 
COUNTRY_ID              CHAR(2)      

desc countries

Name         Null?    Type         
------------ -------- ------------ 
COUNTRY_ID   NOT NULL CHAR(2)      
COUNTRY_NAME          VARCHAR2(40) 
REGION_ID             NUMBER       

select country_id, city, state_province
from locations l
order by(select country_name
         from countries c 
         where l.country_id=c.country_id)

3 个答案:

答案 0 :(得分:1)

加入表格然后应用ORDER BY:

select l.country_id, l.city, l.state_province
from locations l join countries c on c.country_id = l.country_id
order by c.country_name;

答案 1 :(得分:0)

SELECT L.COUNTRY_ID, CITY, STATE_PROVINCE 
FROM LOCATIONS L 
INNER JOIN COUNTRIES 
ON COUNTRIES.COUNTRY_ID = L.COUNTRY_ID 
ORDER BY COUNTRIES.COUNTRY_NAME

答案 2 :(得分:0)

order by子句使用相关子查询。对于选择的每一行,根据匹配的ID选择国家/地区名称,然后按此排序查询。

编写此查询的一种可以说是更优雅的方式是使用join,这可能更直观:

SELECT   l.country_id, city, state_province
FROM     locations l
JOIN     countries c ON l.country_id = c.country_id
ORDER BY c.country_name