最近使用finereport做報表,涉及到好多sql的處理,下面是用到的sql的總結,sql有好多東西都是沒用過的。
1. if
表示式:if( expr1 , expr2 , expr3 )
expr1條件,條件為true,則值是expr2 ,false,值就是expr3
示例:
if(len(buyer)==0,"","and od.buyer_customer_name ='"+buyer+"'")
2.
case when
case 列名
when 條件 then 結果
else 其它結果
end 別名
示例:
case od.`status`
when
'05'
then
'已成交'
when
'10'
then
'已租船'
when
'15'
then
'已裝船'
when
'20'
then
'已到港'
when
'25'
then
'已卸貨'
when
'30'
then
'已對賬'
when
'35'
then
'已收款'
when
'00'
then
'已取消'
end as "狀態"
3.
ifnull
ifnull( expr1 , expr2 )
在 expr1 的值不為 null的情況下都返回 expr1,否則返回 expr2
示例:
concat('$',ifnull((select format(max(statement.settlement_price), 3) from t_order_statement statement where od.id = statement.order_id and statement.statement_type = '2'
and statement.alive_flag = '1'),
(select format(max(statement.settlement_price), 3) from t_order_statement statement where od.id = statement.order_id and statement.statement_type = '1'
and statement.alive_flag = '1'))) as "結算**",
4.
concat
示例:
concat(
'+/-',
format(goods.more_less, 0),
'%') as
"溢短裝",
concat(
transport.delivery_date_start,
'至',
transport.delivery_date_end
) as "裝期",
5.排名
片段示例:
select channelname,browsemonth,
case
when @coltotal = oriinfo.browseweek then
@colnum
when @coltotal := oriinfo.browseweek then
@colnum :=@colnum + 1
when @coltotal = 0
then
@colnum :=@colnum + 1
endas monthorder
from
(select @colnum := 0 ,@coltotal := null)t,
6.
本月第一天,最後一天
函式:
select curdate();
--獲取當前日期
select last_day(curdate());
--獲取當月最後一天。
select date_add(curdate(),interval -day(curdate())+1
day);
--獲取本月第一天
select date_add(curdate()-day(curdate())+1,interval
1month);
-- 獲取下個月的第一天
select datediff(date_add(curdate()-day(curdate())+1,interval
1month ),date_add(curdate(),interval -day(curdate())+1
day)) from dual;
--獲取當前月的天數
示例:
select ct.total as
"企業總數",cd.day
as"企業當天",cm.mon as
"企業當月"
from
(select
count(distinct e.member_id) as total from
`enterprises` e where create_time > '2017-01-01'
and create_time < '2020-03-31'
andexists (select * from members m where m.id = member_id and m.`del_***` = false
and m.locked = false)
andexists (select * from
`member_credentials` c where c.`member_id`= e.member_id and c.`credentials_code`='99'
and c.`audit`)) ct,
(select
count(*) as
dayfrom
`enterprises`
where create_time = curdate()) cd,
(select
count(*) as mon from
`enterprises`
where create_time >=
date_add(curdate(),interval -day(curdate())+1
day) and create_time <=
last_day(curdate())) cm;
sql中的函式是比較有意思的,學以致用,不斷總結! mysql 遇到的問題 錯誤總結
1.can t connect to local mysql server through socket var run mysqld mysqld.sock 2 解決辦法 重啟mysql伺服器 2.密碼不符合安全策略 密碼太簡單 目的 為了設定簡單密碼 解決辦法 1 使用命令 show varia...
mac安裝mysql遇到的問題總結
1 sudo mysql.server start starting mysql error the server quit without updating pid file usr local var mysql yuandemacbook pro.local.pid last login sa...
最近遇到的問題,總結一哈
將list型別轉換成string型別,for string ordercodes orders 超出文字框的區域自動換行 debug是不進入除錯狀態 js的ajax位址url傳參的格式不正確 xml檔案寫的時候注意 和大於號,小於號的區別 用轉義字元轉換 controller入參時,用hashmap...