-
列与列之间要用[
,
]隔开 -
日期加减,是将日期 向前 或 向后 推进。但是mysql内无法将30天以上的日期 向月份进位
-
进行数值的更改后,为了让列名看起来易读,可以用
AS
语句进行 新列的名称设置
select *,日付 + 30 ,出金額 + 100 AS 百円増やしの出金額 from 家計簿;
-
上面的日期进行加减,可以用mysql内封装函数进行,列和新增的列之间也要加括号。
select * , date_add(日付,interval 30 day)from 家計簿;
-
case
演算子 变更值 -
类似JAVA的
if else
语句 要用[,
]间隔开,所选的列名。
select 費目,出金額, case 費目 when '水道光熱費' then '固定费' when '居住费' then '固定费' else '変動費' end as 出費の分類 from 家計簿 where 出金額 > 0;
-
case
演算子 利用条件式进行 筛选
select 費目,入金額, case when 入金額 between 500 and 5000 then 'お小遣い' when 入金額 <10000 then '一時収入' else '口止め料' end as 収入の分類 from 家計簿 where 入金額 > 0;
-
函数
-
length()
查看的是字节。汉字为3 数字为1
select 費目,length (費目),メモ,length(メモ) from 家計簿;
-
character_length()
查看字数
select 費目,length (費目),メモ,character_length(メモ) from 家計簿;
-
去除空白
-
trim(列)
去除左右的空白 -
ltrim(列)
去除左侧空白 -
rtrim(列)
去除右侧空白 -
文字列的一部分 取出
select メモ,substring(費目,1,3) from 家計簿;
-
文字列的一部分 替换
select メモ,replace(メモ,'購入','買った') from 家計簿;
-
文字列的连接
select 費目,メモ,concat(費目,メモ) from 家計簿;
select 費目,メモ,concat(費目,':',メモ) from 家計簿;
-
数字的向上向下取整
select *,
round(出金額,-2) as 百円単位の結果①,
truncate(出金額,-2)as 百円単位の結果②
from 家計簿 where 出金額 > 0;
-
取得日期的三种方法
select current_timestamp from dual;
虚空表格
select current_date();
select current_time();
-
组函数
INT | STRING | DATE | |
---|---|---|---|
SUM | 数值的合计 | 🙅♂️ | 🙅♂️ |
MAX | 最大 | 列表排在最后的值 | 最新日期 |
MIN | 最小 | 列表排在最前的值 | 最久日期 |
AVG | 平均 | 🙅♂️ | 🙅♂️ |
COUNT | 行数 | 行数 | 行数 |
- 和
select sum(降水量)from 気象観測;
- 平均值(null不计算)
select avg(降水量)from 気象観測;
- 最大值
select max(最高気温)from 気象観測;
- 最小值
select min(最低気温)from 気象観測;
Count(列)
计算有多少数据null
不计算Count(*)
单纯计算行数 包含有null
的行数
select count()from 気象観測;
分组
- 如果要查询一个列,和这个列的组函数,且在一起的视图,必须要对这个列进行分组。
select 費目, sum(出金額) from 家計簿アーカイブ group by 費目;
- 组函数的条件判断式
having
在 SQL 中增加HAVING
子句原因是,WHERE
关键字无法与聚合函数一起使用。
HAVING
子句可以让我们筛选分组后的各组数据。
select 費目, sum(出金額) from 家計簿アーカイブ group by 費目 having sum(出金額)>0;
- 顺序
select 选择列LIST from 表table where 条件式 group by 分组名 having 组函数结果的条件式 order by 排序方式;
- 组函数可以在
select / order by / having
的后面书写