A small collection of My SQL commonly used methods


Preface

Tips: The small methods or functions used in my sql will be kept as records for everyone to see.


Tip: The following is the content of this article, the following cases are for reference

One, MY SQL function

Check your seat according to your needs,

1.MY SQL查询表结构
SHOW CREATE TABLE   
  
 2.查询条数限制
select * from sss  limit 84

3.限制条件是3000-4999
SELECT IFNULL(MAX(数字字段) + 1, 3000) AS USR_ID FROM 表名 WHERE 数字字段 BETWEEN '3000' AND '4999'

4.Mysql 配合group by 对相同的列进行整合
 group_concat(字段)
 
5.Mysql  中拼接字符串
concat( 字段1, '-' , 字段2)

6.获取当前时间并且格式化
SELECT date_format(NOW(), '%Y%m%d%H%i%S');

7.去除字段中的特殊字符
SELECT replace(ROLE,'_','')  from tant_mst

8.mysql中会出现超出最大值,加上一下语句即可
SET GLOBAL group_concat_max_len=1024000;
SET SESSION group_concat_max_len=1024000;
**(配合验证的语句)**
解除 Group_concat(nec.TYUUMON_ID)长度限制
SHOW VARIABLES LIKE "group_concat_max_len"; #查询最大值

9. 一个字段多个值使用方法拆分
    SELECT a.*, SUBSTRING_INDEX( SUBSTRING_INDEX( 需要循环的字段(a.ro), ',', b.help_topic_id + 1 ), ',',- 1 ) AS ROLE_ID
    FROM  表a   AS a
    LEFT JOIN mysql.help_topic AS b ON b.help_topic_id < ( length( 需要循环的字段(a.ro) ) - length( REPLACE ( 需要循环的字段(a.ro), ',', '' ) ) + 1 )
    
10.页面用到分页时后台使用的方法(此方法在mysql中不可用,需要在项目后台上方有查询sql并带着
 SQL_CALC_FOUND_ROWS 标识才可 在同一方法下用第二条语句查询全部数据 用于加载到分页工具上)
 首先查询数据
 select SQL_CALC_FOUND_ROWS  * from x Limit 0,20;
 其次查询上条数据的所有值
 SELECT FOUND_ROWS() 'TOTAL';
11.删除自增历史数据 
 TRUNCATE TABLE 表名

Second, use steps

1. It can also be used together, if you need to disassemble the value of the association table, you can use it together

The code is as follows (example):

 SELECT  c.*,
     group_concat(表b中需要集合的字段(b.sss)) as ROLE_NAME
    FROM
    (
    SELECT a.*, SUBSTRING_INDEX( SUBSTRING_INDEX( 需要循环的字段(a.ro), ',', b.help_topic_id + 1 ), ',',- 1 ) AS X2
    FROM  表a   AS a
    LEFT JOIN mysql.help_topic AS b ON b.help_topic_id < ( length( 需要循环的字段(a.ro) ) - length( REPLACE ( 需要循环的字段(a.ro), ',', '' ) ) + 1 )
    ) c
    LEFT JOIN 表b as b
    ON 表b.x1= c.X2
    GROUP BY 表c中的唯一字段 (c.w1)

2. Concat() is used flexibly

The code is as follows (example):

concat(字段1,字段2,字段3)
显示为:字段1字段2字段3

3. MY sql fill numbers or characters

The code is as follows (example):

填充数字
SELECT LPAD(@num:=需要填充字段+1, 6, 0) as s FROM 表 ,(SELECT @num:=0) r
填充字符
SELECT LPAD(@num:=需要填充字段+1, 6, 'q') as s FROM 表 ,(SELECT @num:=0) r

to sum up

Tip: Here is a summary of the article: The
above is a small collection of MY sql by this Xiaobai, other Xiaobai will come over, please give pointers.

Guess you like

Origin blog.csdn.net/weixin_45235159/article/details/114087945