MySQL:《学习笔记与实战》之查询实例(3)更改月份

1.更新表名时的简写

UPDATE store_info_final_new  a, traffic_grade_final b,SET a.hospital_score = b.grade
WHERE a.id=b.id;


2.连接表要比update快

 select a.*,b.id from cudata_yuan a  left join  b where a.id =b.id

3.查询结果作为子表 再进行查询,查询每个月公司有多少人

SELECT mon,count(new_name) from 
(SELECT month as mon ,name as new_name from year_month_report_only_user 
WHERE type_name=3 GROUP BY month,name ORDER BY month)A 
GROUP BY mon ORDER BY mon;

#统计去重的每月人数
SELECT mon,count(new_name) from 
(SELECT month as mon ,name as new_name from year_month_my_user_copy 
WHERE type_name=3 GROUP BY month,name ORDER BY month)A 
GROUP BY mon ORDER BY mon;


#更改月份 2 变成02
update year_month_my_user_copy  
set month = (  
case   
  when (LENGTH(month))<2 then concat('0',month)
  else month
end);


4.数据类型转换函数

    ①:CAST(value as type)   Cast(字段名 as 转换的类型 )

          date:2015-11-03 15:31:26 select cast(date as signed) as date from table1;

            结果如下: date:20151103153126

②:CONVERT(value, type) 

例一

mysql> SELECT CONVERT('23',SIGNED);
+----------------------+
| CONVERT('23',SIGNED) |
+----------------------+
|                   23 |
+----------------------+
1 row in set
例二


mysql> SELECT CAST('125e342.83' AS signed);
+------------------------------+
| CAST('125e342.83' AS signed) |
+------------------------------+
|                          125 |
+------------------------------+
1 row in set

例三


mysql> SELECT CAST('3.35' AS signed);
+------------------------+
| CAST('3.35' AS signed) |
+------------------------+
|                      3 |
+------------------------+
1 row in set

5.多增加一列month 作为去重用

    从DealDate截取月 用作去重用;同一字段去中间条件部门多用between

    SELECT * ,substring(DealDate, 6, 2)as month FROM old 
    WHERE (DealDate BETWEEN '2018-02-26' and '2018-06-17') and DealStatus='D'
    GROUP BY MemberName,month,phone;

6.删除表用

    DELETE语句和TRUNCATE TABLE语句功能相似,但是二者究竟有何区别呢?
    https://www.cnblogs.com/lmaster/p/6373148.html

7.利用元组查询

     select name ,count(name) where name in ('张三','李四')
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值