mysql高级函数

mysql高级函数,第1张

1.select concat(name, money) from account:拼接字段

2.select length(name) from account:查询字节长度(根据编码集utf-8,一个汉字占三个字节)

3.select ifnull(money, 10) from account:如果money为null,显示10

4.select round(money,1) from account:保留一位小数四舍五入

5.select floor(money) from account:向下取整

6.select ceil(money) from account:向上取整

7.select truncate(money, 1) from account:截断(截取一位小数)

8.select mod (10, 3):取余(相当于select 10%3)

9.select upper(name) from account:将名字变成大写

10.select lower(name) from account:将名字变成小写

11.select substring(name, 1) from account:截取名字下标从1开始的所有字段(注意:mysql的下标都是从1开始)

12.select substring(name, 1, 4) from account:截取名字下标从1开始,长度为4的字段

13.select now():返回当前系统的日期和时间

14.select curdate():返回当前系统的日期

15.select curtime():返回当前系统的时间

16.select date_format(now(), '%Y年%m月%d日') as '当前时间':将时间转换成字符串

17.select count(name) from account:计算name的个数(忽略null)

18.select count( ) from account:计算个数(不忽略null,类似:select count(1) from account)

19.select lpad(name, 10, ' '), money from account:指定字段在左边填充到指定长度(rpad:右边填充)

20.select replace(name, 'an', '*'), money from account:替换指定字段

21.select * from user limit 0,5:查询前5条数据(下标0开始,数量:(page - 1) * size, size)

22.select * from boy union select * from girl:两个结果合成一个(会自动去重,不去重用:union all)

1.select sum(money) from account:求和(忽略null,null和任何值相加都为null)

2.select sum(money) from account:求平均数(忽略null)

3.select max(money) from account:求最大值(忽略null)

4.select min(money) from account:求最小值(忽略null)

5.select name, money, if(money is null, '呵呵', '哈哈') 备注 from account:if语句

6.case条件语句

1.create table copy like user:复制user表(只复制表的字段)

2.create table copy select * from user:复制user表(字段数据一起复制)

3.create table copy select username,age from user:复制user表(复制指定的字段,数据一起复制)

4.create table copy select username,age from user where 0:复制user表(复制指定的字段,数据不复制)

5.alter table 表名 add|drop|modify|change column 列名【列类型 约束】:修改表

1.等值连接:select s.studen, t.teacher from study s, teacher t where s.t_id = t.id(求交集部分)

mysql>CREATE TABLE time_account (

-> account VARCHAR(10),

-> `time` DATETIME

->)

Query OK, 0 rows affected (0.08 sec)

mysql>

mysql>INSERT INTO time_account

-> SELECT 'a' , '2000-01-01 00:00:00' UNION ALL

-> SELECT 'b' , '2011-01-01 00:00:00'

Query OK, 2 rows affected (0.02 sec)

Records: 2 Duplicates: 0 Warnings: 0

mysql>

mysql>SELECT * FROM time_account

+---------+---------------------+

| account | time|

+---------+---------------------+

| a | 2000-01-01 00:00:00 |

| b | 2011-01-01 00:00:00 |

+---------+---------------------+

2 rows in set (0.00 sec)

mysql>UPDATE

-> time_account

->SET

-> `time` = ADDTIME(`time`, '50:00:00')

->WHERE

-> account IN ('a','b')

Query OK, 2 rows affected (0.05 sec)

Rows matched: 2 Changed: 2 Warnings: 0

mysql>SELECT * FROM time_account

+---------+---------------------+

| account | time|

+---------+---------------------+

| a | 2000-01-03 02:00:00 |

| b | 2011-01-03 02:00:00 |

+---------+---------------------+

2 rows in set (0.00 sec)

余额可以单独用一个余额记录表,这样如果要查询每次消费记录的时候,也能查出来每次消费后还有多少余额。余额记录表里面主要是三个字段:用户账号、每次消费后的余额、时间点。

CREATE TABLE account

(

id integer NOT NULL DEFAULT nextval('trade_id_seq'::regclass),

no character varying(10) NOT NULL, -- 账号

balance money NOT NULL DEFAULT 0.00, -- 余额

datetime timestamp without time zone NOT NULL DEFAULT (now())::timestamp(0) without time zone,

CONSTRAINT account_pkey PRIMARY KEY (id)

)

通过每次的余额变化就知道每次消费后的余额情况

select acc.*, (select sum(balance)+acc.balance from account as ac where ac.id <acc.id) as profit from account as acc

id | no | balance | datetime | profit

----+------+----------+---------------------+---------

1 | 1000 |$0.00 | 2013-10-09 10:51:10 |

2 | 1000 | $12.60 | 2013-10-09 10:51:22 | $12.60

4 | 1000 | $16.80 | 2013-10-09 10:51:42 | $29.40

5 | 1000 | $100.00 | 2013-10-09 10:51:49 | $129.40

6 | 1000 | $200.00 | 2013-10-09 10:56:35 | $329.40

7 | 1000 | $50.45 | 2013-10-09 10:57:23 | $379.85

8 | 1000 | $75.50 | 2013-10-09 10:57:31 | $455.35

9 | 1000 | -$55.30 | 2013-10-09 10:59:28 | $400.05

10 | 1000 | -$200.00 | 2013-10-09 10:59:44 | $200.05

(9 rows)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7552694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存