SqlServer 查询表格的数据属性

USE 库名;
 
 
SELECT
     表名       = Case When A.colorder=1 Then D.name Else '' End,
     表说明     = Case When A.colorder=1 Then isnull(F.value,'') Else '' End,
     字段序号   = A.colorder,
     字段名     = A.name,
     字段说明   = isnull(G.[value],''),
     标识       = Case When COLUMNPROPERTY( A.id,A.name,'IsIdentity')=1 Then '√'Else '' End,
     主键       = Case When exists(SELECT 1 FROM sysobjects Where xtype='PK' and parent_obj=A.id and name in (
                      SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = A.id AND colid=A.colid))) then '√' else '' end,
     类型       = B.name,
     占用字节数 = A.Length,
     长度       = COLUMNPROPERTY(A.id,A.name,'PRECISION'),
     小数位数   = isnull(COLUMNPROPERTY(A.id,A.name,'Scale'),0),
     允许空     = Case When A.isnullable=1 Then '√'Else '' End,
     默认值     = isnull(E.Text,'')
 FROM
     syscolumns A
 Left Join
     systypes B
 On
     A.xusertype=B.xusertype
 Inner Join
     sysobjects D
 On
     A.id=D.id  and D.xtype='U' and  D.name<>'dtproperties'
 Left Join
     syscomments E
 on
     A.cdefault=E.id
 Left Join
 sys.extended_properties  G
 on
     A.id=G.major_id and A.colid=G.minor_id
 Left Join
 
 sys.extended_properties F
 On
     D.id=F.major_id and F.minor_id=0
     where d.name='table_name'    --如果只查询指定表,加上此条件
 Order By
     A.id,A.colorder

Mysql 百万数据limit 查询

SELECT * FROM db_storage limit 1085031,20
查询时间为0.486s

SELECT * FROM db_storage WHERE ID >= (select id from db_storage limit 1085031, 1) limit 20

SELECT a.* FROM db_storage a INNER JOIN (SELECT id FROM db_storage LIMIT 1085031,20) b ON a.id = b.id;

上面两个类似,为0.27s左右,此处需要优化pagehelper 逻辑

Mysql数据库在服务器上优化

[client]
port=3306
socket=/tmp/mysql/mysql.sock
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[mysqld]
port=3306
user=mysql
socket=/tmp/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=error.log
#允许最大连接数
max_connections=200
#允许连接失败的次数,这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10

# innodb 优化
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 缓存池
innodb_buffer_pool_size=8G
# 脏页占innodb_buffer_pool_size的比例时,触发刷脏页到磁盘
# 25%~50%
innodb_max_dirty_pages_pct=30
# 限制Innodb能打开的表的数据,如果库里的表特别多的情况,请增加这个。这个值默认是300
innodb_open_files = 500
# 此参数确定写日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据。MySQL开发人员建议设置为1-8M之间
innodb_log_buffer_size = 2M

#开启记录慢查询 设置慢查询界定时间为1秒
long_query_time=1
slow-query-log=On
slow_query_log_file="mysql_slow_query.log"

transaction_isolation = READ-COMMITTED
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
# lower_case_table_names = 1 这个不能要

#skip-grant-tables

在linux中使用yum安装mysql 安装完成后可以通过下面的语句查询各个路径。

show global variables like "%dir%";

Mysql 5.7 配置允许存入emoji

[mysqld]
character-set-server=utf8mb4

[mysql]
default-character-set=utf8mb4

只要修改my.ini(位置:C:ProgramDataMySQL)文件,然后重启mysql服务即可.

注意数据库的编码为utf8mb4

SHOW VARIABLES LIKE 'character_set%';

 

Mysql中导出表格的信息

SELECT
	COLUMN_NAME AS 列名,
	cast(COLUMN_TYPE as CHAR) AS 数据类型,
	cast(DATA_TYPE as CHAR) AS 字段类型,
	CHARACTER_MAXIMUM_LENGTH AS 长度,
	IS_NULLABLE AS 是否为空,
	COLUMN_DEFAULT AS 默认值,
	COLUMN_COMMENT AS 备注
FROM
	INFORMATION_SCHEMA. COLUMNS
WHERE
	table_schema = 'tobacco_asset'
AND 
        table_name = 'asset_base'

这样可以查出对应的表格每个字段的信息

Ubuntu 下Mysql 安装时密码问题

1.安装以后没有设置密码

sudo cat /etc/mysql/debian.cnf

通过这个查看mysql的默认用户名和密码

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = cOrWdhKDBv7LS86s
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = cOrWdhKDBv7LS86s
socket   = /var/run/mysqld/mysqld.sock

2.正常登录mysql,修改密码

 alter user'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
 flush privileges;

3.重新登陆即可

Mysql 根据时间的筛选语句

引用 https://www.cnblogs.com/shuilangyizu/p/8805384.html

--今天

select * from 表名 where to_days(时间字段名) = to_days(now());

--昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
AND TO_DAYS(NOW()) - TO_DAYS(update_time)>0
--本周 SELECT * FROM 表名 WHERE YEARWEEK( date_format( 时间字段名,'%Y-%m-%d' ) ) = YEARWEEK( now() ) ; --本月 SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) ,'%Y%m' ) --上一个月 SELECT * FROM 表名 WHERE PERIOD_DIFF(date_format(now(),'%Y%m'),date_format(时间字段名,'%Y%m') =1 --本年 SELECT * FROM 表名 WHERE YEAR( 时间字段名 ) = YEAR( NOW( ) ) --上一月 SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1 --查询本季度数据 select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now()); --查询上季度数据 select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)); --查询本年数据 select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW()); --查询上年数据 select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year)); --查询当前这周的数据 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now()); --查询上周的数据 SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1; --查询当前月份的数据 select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m') --查询距离当前现在6个月的数据 select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now(); --查询上个月的数据 select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') select * from ` user ` where DATE_FORMAT(pudate, ' %Y%m ' ) = DATE_FORMAT(CURDATE(), ' %Y%m ' ) ; select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now()) select * from user where MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now()) select * from [ user ] where YEAR (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = YEAR (now()) and MONTH (FROM_UNIXTIME(pudate, ' %y-%m-%d ' )) = MONTH (now()) select * from [ user ] where pudate between 上月最后一天 and 下月第一天 where date(regdate) = curdate(); select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now()) SELECT date( c_instime ) ,curdate( ) FROM `t_score` WHERE 1 LIMIT 0 , 30

MySql常用语句

mysql分析当前的查询线程

select * from information_schema.`PROCESSLIST` where info is not null;

或者

show processList

 

联表更新

UPDATE tobacco_product AS p, `temp_公司商品维护(通用)` AS t SET p.ONLINE_DATE_YEAR = CONVERT(STR_TO_DATE(t.`入网日期`, “%Y”),int) WHERE p.CIG_UPCS_CD = t.`条包装条形码`