-- 查詢賣最好的商品名稱
select goods_name from indent_detail group by goods_id order by sum(goods_num) limit 1;
-- 查詢張三購買過的商品名稱
select goods_name
from user join indent on indent.user_id=`user`.id
join indent_detail on indent.id=indent_detail.indent_id
where username='zhansan'
-- 查詢消費金額最多的使用者名稱
select username
from indent join user on `user`.id=indent.user_id
group by indent.user_id
order by sum(total_price) desc
limit 1
-- 查詢訂單量最多的省份名稱
select region_name from indent join region
on indent.province = region.region_id
group by indent.province
order by count(*) desc limit 1
-- 查詢2023年各個省份的訂單數量以及訂單總金額 顯示格式 省份名稱 訂單數量 訂單總金額
select region_name ,count(*) as number,sum(total_price) as total from indent join region
on indent.province = region.region_id
where year(add_time)=2014
group by indent.province
-- 查詢山東省最近三個月的訂單數量
select count(*) as number from indent join region
on indent.province = region.region_id
where region_name='安徽'
and date_sub(curdate(),interval 3 month)<=add_time
-- 查詢最近三天賣的最好的前十件商品名稱
select goods_name
from indent_detail join indent on indent.id=indent_detail.indent_id
where to_days(now())-to_days(add_time)<=3
group by goods_id order by sum(goods_num) desc limit 10;
select goods_name
from indent_detail join indent on indent.id=indent_detail.indent_id
where date_sub(curdate(),interval 3 day)<=add_time
group by goods_id order by sum(goods_num) desc limit 10;
-- 查詢安徽省滁州市最近三天的訂單
select * from indent join region
on indent.city = region.region_id
where region_name='滁州'
and date_sub(curdate(),interval 3 month)<=add_time
MySQL的一些常用SQL語句
備份表 create table 新錶 select from 舊表 建立臨時表 create temporary table tablename id varchar 100 name varchar 100 age varchar 100 刪除臨時表 drop temporary table i...
關於一些常用的SQL語句
增加欄位並標明備註 alter table test add test name varchar 220 comment 名稱 刪除的話,在實際情況中最好不要刪除資料,可以增加棄用的狀態去控制 表備份 備份test 表生成test new 新錶的命名根據你們的具體要求來 create table t...
記錄一些常用的sql語句
select name,submittime from enterprise where yearweek date format submittime,y m d yearweek now select name,submittime from enterprise where yearweek ...