MySQL常用查詢

2021-07-25 04:47:18 字數 1247 閱讀 3303

sudo service mysql start;

mysql -u root;

create databae test;

usetest;

#建立表

create table shop(

article int(4) unsigned zerofill default

'0000' not null,

dealer varchar(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);

使用max() 求article的最大值:

select

max(article) as articlemax from shop;

求某列最大值所在的行:

#顯示price最大值的行

select * from shop where price = (select

max(price) from shop);

另一種方法:將按照price排序, 讓後使用mysql中的limit子句來獲取第一行即最大行:

#按照**降序排序

select * from shop order

by price desc;

#使用limit 獲取指定行

select * from shop order

by price desc limit 1;

注:如果有多項物品的**都是19.95(最貴的物品不止乙個),那麼limit的顯示結果也只會顯示其中乙個

select article, max(price) as

maxfrom shop

group

by article;

mysql 查詢 常用 mysql常用查詢

一.group concat函式,以指定的分割符合並列,與group by 一起用 例 selectgroup concat c.columnname separator group by 二.preparedstatement.return generated keys 得到剛剛插入記錄的id p...

mysql常用的查詢 MySQL常用查詢

資料庫 1.查詢所有資料的大小 data length 資料大小 index length 索引大小 select concat round sum data length index length 1024 1024,2 mb as data from information schema.tab...

mysql常用的查詢 MySQL常用查詢

select from unixtime create time,y m as time,sum sales amount sales amount sum from sales group by time 執行結果如下 查詢每年的銷售額 select from unixtime create ti...