SQL航空網的幾個航班查詢題

2021-06-02 06:21:23 字數 2068 閱讀 7872

表結構如下:

flight

city{cityid, cityname)

實驗環境:

create table city(cityid int auto_increment primary

key,cityname varchar(20));

create table flight (flightid int auto_increment primary key,

startcityid int references city(cityid),

endcityid int references city(cityid),

starttime timestamp);

//航班本來應該沒有日期部分才好,但是下面的題目當中涉及到了日期

insert into city values(null,'北京'),(null,'上海'),(null,'廣州');

insert into flight values

(null,1,2,'9:37:23'),(null,1,3,'9:37:23'),(null,1,2,'10:37:23'),(null,2,3,'10:37:23');

1、查詢起飛城市是北京的所有航班,按到達城市的名字排序

參與運算的列是我起碼能夠顯示出來的那些列,但最終我不一定把它們顯示出來。各個表組合出來的中間結果欄位中必須包含所有運算的字段。

select * from flight f,city c

where f.endcityid = c.cityid and startcityid =

(select c1.cityid from city c1 where c1.cityname = "北京")

order by c.cityname asc;

mysql> select flight.flightid,'北京' startcity, e.cityname from

flight,city e wh

ere flight.endcityid=e.cityid and flight.startcityid=(select

cityid from city wh

ere cityname='北京');

mysql> select flight.flightid,s.cityname,e.cityname from

flight,city s,city e wh

ere flight.startcityid=s.cityid and s.cityname='北京' and

flight.endcityid=e.cit

yid order by e.cityname desc;

2、查詢北京到上海的所有航班紀錄(起飛城市,到達城市,起飛時間,航班號)

select c1.cityname,c2.cityname,f.starttime,f.flightid

from city c1,city c2,flight f

where f.startcityid=c1.cityid

and f.endcityid=c2.cityid

and c1.cityname='北京'

and c2.cityname='上海'

3、查詢具體某一天(2005-5-8)的北京到上海的的航班次數

select count(*) from

(select c1.cityname,c2.cityname,f.starttime,f.flightid

from city c1,city c2,flight f

where f.startcityid=c1.cityid

and f.endcityid=c2.cityid

and c1.cityname='北京'

and c2.cityname='上海'

and 查幫助獲得的某個日期處理函式(starttime) like '2005-5-8%'

mysql中提取日期部分進行比較的示例**如下:

select * from flight where

date_format(starttime,'%y-%m-%d')='1998-01-02'

常用的幾個SQL 查詢語句

記錄新增 insert into 表名 欄位1,欄位2 values 值1,值2 insert into tbclass classname values 測試1 insert into tbclass classname values 測試2 insert into tbclass classna...

sql的學習 查詢的幾個注意點

以上的表為查詢所用的表 between的用法,限制 select from emp where sal 1000 and sal 100 select from emp where sal between 100 and 1000 in的用法 in 操作符允許我們在 where 子句中規定多個值。屬...

幾個我做過最複雜的SQL查詢語句

string sql update user set user login date getdate user dzb user dzb isnull select top 1 datediff day,user login date,getdate wage temp money wage tem...