order by子句,位置table中没有country_name,parents如何查询order by country_name?
the order by clause, there is no country_name in the locations table, how can the parents query order by country_name?
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)
连接表然后应用 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;
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
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
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)
连接表然后应用 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;
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
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