mysql批模式 MySQL 批量模式

2021-10-17 17:57:57 字數 3619 閱讀 9499

peter

什麼是批量模式

•從檔案中讀取標準sql 命令

•通過互動模式進行詢問

•為什麼需要使用批量模式

–重複進行相同詢問

–可以通過管道, 詳細查詢返回結果

–可以把查詢結果輸出到檔案中

–可以很方便把指令碼分配給其他使用者

–cron job

以批量模式建立表單

•mysql>create table my_address ( first_name varchar(20), last_name varchar(20), email varchar(50), birth date )

•批量模式建立方法

create table my_address

first_name varchar(20) not null,

last_name varchar(20) not null,

email varchar(50) null,

birth date defautl 「1970-01-01」

批量模式查詢

•mysql>select pet.name, pet.species, pet.***,

->event.type, event.remark

-> from pet, event

-> where pet.name = event.name and

-> ( pet.name=「fluffy」or pet.name=「buffy」);

•製作批量模式

select pet.name, pet.species, pet.***, event.type, event.remark

from pet, event

where pet.name = event.name and ( pet.name=「fluffy」or pet.name=「buffy」);

使用批量模式

•shell 下的批量模式

shell> mysql -h host -p root my_db < create_table.sql

shell> mysql -h host -p root my_db < query_table.sql

•shell 下得返回結果與mysql 下的返回結果略有區別

•使用-t 引數可以得到mysql 下的相同結果

•mysql 下的批量模式

mysql> source /query_table.sql

mysql> \. /query_table.sql

常用計算

•目標: 用於比較當前資料庫中商品**

•建立商品資料庫

mysql> create table shop (

-> article int(4) unsigned zerofill default '0000' not null,

-> dealer char(20) default '' not null,

-> price double(16,2) default '0.00' not null,

-> primary key(article, dealer));

一般操作

•插入資料並進行普通查詢

mysql> insert into shop values

-> (1,'a',3.45),(1,'b',3.99),(2,'a',10.99),(3,'b',1.45),

-> (3,'c',1.69),(3,'d',1.25),(4,'d',19.95);

•驗證資料

mysql>select * from shop;

得到當前表單最大值

•目標一: 得到當前表單最大值

mysql> select max(article) as article from shop;

•as 作為表單資料輸出column 標題重新命名

高階應用(例一)

•要得到當前最大值的詳細資訊, 如最貴的商品

mysql> select article, dealer, price from shop where price=(select max(price) from shop);

# 注意, 只能夠在4.1 版本後使用

高階應用(例二)

•要得到當前最大值的詳細資訊, 如最貴的商品

•mysql 4.1 前可以通過兩步完成

第一步:

mysql> select max(price) from shop;

第二步mysql> select article, dealer, price

-> from shop

-> where price=19.95;

高階應用(例三)

•要得到當前最大值的詳細資訊, 如最貴的商品

mysql> select article, dealer, price from shop order by price desc limit 1;

•缺點: limit 只能夠返回乙個值, 存在多個價錢相同的商品的時候只會返回乙個

高階應用(例四)

•目的: 查詢每種貨物價錢最貴的商品

•mysql> select article, max(price) as price from shop group by article;

高階應用(例五)

•使用組比較,得到最大值, 並同時輸出其他資訊

mysql> select article, dealer, price from shop s1 where price=(select max(s2.price) from shop s2 where s1.article = s2.article);

•# 這個方法只能夠用於mysql 4.1 或以後版本

高階應用(例六)

•針對mysql 4.1 之前版本的解決方法:

•第一步,得到產品最大價錢配對

•第二步,為每個產品獲得相對應行,獲得最大值

mysql> create temporary table tmp (

article int(4) unsigned zerofill

default '0000' not null,

price double(16,2) default '0.00' not null);

mysql> lock tables shop read;

高階應用(例六續)

mysql> insert into tmp select article, max(price) from shop group by article;

mysql>select shop.article, dealer, shop.price from shop, tmp where shop.article=tmp.article and shop.price=tmp.price;

mysql> unlock tables;

mysql> drop table tmp;

高階應用(變數使用)

•特別的例子方法:

•變數的使用

mysql> select @min_price:=min(price),@max_price:=max(price) from shop;

mysql> select * from shop where price=@min_price or price=@max_price;

mysql量批修改 MySQL的批量修改資料

使用自帶的語句構建批量更新 mysql 實現批量 可以用點小技巧來實現 update tablename set orderid case id when 1 then 3 when 2 then 4 when 3 then 5 endwhere id in 1,2,3 這句sql 的意思是,更新o...

mysql 批量更新 MySQL批量更新

我有2個表 mysql data details accounts invoices 理想情況下,每個data details都應具有accounts invoices id.data details有乙個帶有accounts invoices主鍵的外來鍵 由於某種原因,有data details記...

mysql批量修復 mysql 批量修復

bin bash host name 127.0.0.1 user name user pwd database need optmize table false tables usr local webserver mysql bin mysql h host name u user name p...