Datawhale MySQL基礎練習(4)

2021-09-11 20:19:35 字數 3052 閱讀 1411

資料的匯入匯出:

將excel檔案匯入mysql表

mysql匯出表到excel檔案

操作步驟

1、開始》輸入cmd,進入到命令提示符視窗,cd到mysql的安裝目錄下bin資料夾(才能使用mysql.exe和mysqldump.exe等命令),這裡為c:\program files (x86)\mysql\mysql server 5.0\bin

2、匯出資料庫:

mysqldump -u 資料庫使用者名稱 -p 資料庫名稱 > 匯出的資料庫檔案

這裡輸入以下命令,並回車

mysqldump -uroot -p123456 course > d:\course.sql 【將course這個資料庫的表結構和資料匯出在course.sql這個檔案中】

成功後,在d盤中可以看到course.sql這個檔案

3、匯入資料庫:

1)先建立空的資料庫

2)使用命令:mysql -u 資料庫使用者名稱 –p 資料庫名稱 < 匯入的資料庫檔案

這裡為:

mysql -u root -p course create table employee (

id int primary key auto_increment not null,

name varchar(20) not null,

salary int not null,

departmentid int

)engine=innodb charset=utf8;

insert into employee values (1,『joe』,70000,1),(2,『henry』,80000,2),(3,『sam』,60000,2),(4,『max』,90000,1);

create table department(

id int primary key auto_increment not null,

name varchar(20) not null

)engine=innodb charset=utf8;

insert into department values(1,『it』),(2,『sales』);

編寫乙個 sql 查詢,找出每個部門工資最高的員工。例如,根據上述給定的**,max 在 it 部門有最高工資,henry 在 sales 部門有最高工資。

select d.nameas department,e.nameas employee,e.salary as salary

from employee e, department d

where e.departmentid = d.id

and e.salary = (

select max(salary) from employee

where departmentid = d.id);

換座位(難度:中等)

小美是一所中學的資訊科技老師,她有一張 seat 座位表,平時用來儲存學生名字和與他們相對應的座位 id。

其中縱列的 id 是連續遞增的

小美想改變相鄰倆學生的座位。

你能不能幫她寫乙個 sql query 來輸出小美想要的結果呢?

針對id進行更改交換,最後進行排序

分三類id進行處理,奇數+1,偶數-1,最後一位不變,然後連線

union 操作符用於連線兩個以上的 select 語句的結果組合到乙個結果集合中。多個 select 語句會刪除重複的資料

組合成乙個新錶設定別名進行計算

select * from

(select id-1 as id, student from seat where id%2 = 0

union

select id+1 as id, student from seat where id%2 = 1 and id != (select count() from seat)

union

select id as id, student from seat where id = (select count() from seat)

) as a

order by a.id

select (case

when id % 2 != 0 and id != (select count() from seat) then id+1

when id % 2 != 0 and id = (select count() from seat) then id

else id - 1 end) as id,student

from seat

order by id;

分數排名(難度:中等)

編寫乙個 sql 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名(rank)相同。請注意,平分後的下乙個名次應該是下乙個連續的整數值。換句話說,名次之間不應該有「間隔」。

建立以下score表:

create table score (

id int unsigned auto_increment,

score float not null,

primary key (id)

) engine = innodb charset = utf8;

insert into score (score)

values

(3.50),

(3.65),

(4.00),

(3.85),

(4.00),

(3.65);

根據上述給定的 scores 表,你的查詢應該返回(按分數從高到低排列)

select score from score order by score desc;

1、可以採取表連線的方法,左表與右表比較,若左表資料小於右表資料,對右表計數

2、再按照id分組

3、倒序排列

select s.score,count(distinct s.score) as rank

from scores a join scores s on a.score<=s.score

group by a.id order by a.score desc;

Optimized Purchasing基礎知識

pr的三要素 物料 數量 採購日期 日期是會影響價錢的 blanket po fo框架訂單。a 成本中心,i 限制 專案的限制標籤 miro支票不管數量,從帳戶分配輸入金額。產生pr三種 me51n,me25,mrp 產生po四種 me21n,me25,me57,me59n evo 物料管理 採購 ...

Phabricator Arcanist基本用法

git clone git git clone git 獲取到arcanist的原始碼後,進行相關配置 開啟.bash profile open e bash profile在環境變數中新增 export path path somewhere arcanist bin 命令列中輸入arc命令檢視是...

HttpServletResponse基本功能

request是請求物件,而response是響應物件。response物件的功能分為以下四種 設定響應頭資訊 addheader refresh 5 url x 傳送狀態碼 senderror 404 設定響應正文 getwriter print fdsfdsa 重定向 sendredirect ...