MySQL常用SQL语句(续)

* 建表语句

 
  1. CREATE TABLE `config` (

  2.   `id` int(11) NOT NULL AUTO_INCREMENT,

  3.   `name` varchar(255) NOT NULL COMMENT '配置项',

  4.   `value` varchar(255) NOT NULL COMMENT '配置值',

  5.   PRIMARY KEY (`id`),

  6.   UNIQUE KEY `uniq_config_name` (`name`)

  7. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

* 添加索引

alter table `config` add index idx_config_name(`name`);

* 删除索引

alter table `config` drop index idx_config_name;

* explain 查看是否使用到了索引

insert into config(name, value, info) values('SMS_ENABLED', '0', '0表示禁用发短信功能, 1表示启用发短信功能');

explain select * from config where name='SMS_ENABLED';

* 查看mysql版本号

> select version();
+-----------------+
| version()       |
+-----------------+
| 10.1.34-MariaDB |
+-----------------+

* 修改某个字段
alter table `rules` change `mobile` `mobile` varchar(128) DEFAULT NULL COMMENT '告警通知手机号码';

alter table easy_eyes.warn modify column request_time int(11) not null comment '请求时长(ms)';

*把第二个开始的各字符用第一个字符拼接在一起,结果为goo,le

select concat_ws(",","goo","le"); 

*查看当前时间(xx时.xx分.xx秒) 

select curdate();

*查看当前日期(xx年.xx月.xx日) 

select curtime();

*ROUND(23.298,-1)   四舍五入为20

*TRUNCATE(122.999,1)  122.9(不考虑四舍五入)

*select 

使用group_concat()

1、功能:将group by产生的同一个分组中的值连接起来,返回一个字符串结果。

2、语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )

说明:通过使用distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。

3、举例:

例7:使用group_concat()和group by显示相同名字的人的id号:

猜你喜欢

转载自blog.csdn.net/qq_42000661/article/details/108579516