mysql操作记事本

猿掌柜
2024-01-11 / 0 评论 / 11 阅读 / 正在检测是否收录...
查询近一段时间数据(近一周、一个月、半年、一年)
//查询一周
格式:select * from 表名称 where DATE_SUB(CURDATE(), INTERVAL 6 DAY) <= date(表内时间字段);
语句:select * from garbage_collect  where DATE_SUB(CURDATE(), INTERVAL 6 DAY) <= date(collection_time);

//查询一个月
 格式:select * from 表名称 where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(表内时间字段);
 语句:select * from garbage_collect  where DATE_SUB(CURDATE(),  INTERVAL 1 MONTH) <= date(collection_time);

//查询近半年
格式:select * from 表名称 where DATE_SUB(CURDATE(),  INTERVAL 6 MONTH) <= date(表内时间字段);
语句:select * from garbage_collect  where DATE_SUB(CURDATE(),  INTERVAL 6 MONTH) <= date(collection_time);
查询指定时间段数据(开始时间-结束时间)–包含结束时间加DATE_ADD()函数
格式:SELECT * FROM 表名称 WHERE 表字段 BETWEEN "开始时间" AND "结束时间";
语句:SELECT * FROM garbage_collect WHERE collection_time  BETWEEN "2020-02-01" AND "2020-03-31";

//查询指定时间段数据(开始时间-结束时间)包含结束时间
语句:SELECT * FROM garbage_collect WHERE  collection_time  BETWEEN "2020-03-17" AND DATE_ADD("2020-03-17",INTERVAL 1 DAY)
查询指定时间数据(年、月、日)
//查询2020年的数据:
格式:SELECT * FROM 表名称 WHERE year(表名称.表字段)="某年" 
语句:SELECT * FROM garbage_collect WHERE year(garbage_collect.collection_time)="2020" 
//查询9月份的数据:
格式:SELECT * FROM 表名称 WHERE year(表名称.表字段)="某月" 
语句:SELECT * FROM garbage_collect WHERE month(garbage_collect.collection_time)="09" 
//查询时期是08的数据:
格式:SELECT * FROM 表名称 WHERE year(表名称.表字段)="某日" 
语句:SELECT * FROM garbage_collect WHERE day(garbage_collect.collection_time)="08" 
//查询2020年03月份的数据:
SELECT * FROM garbage_collect WHERE year(garbage_collect.collection_time)="2020" and month(garbage_collect.collection_time)="03"
//查询2020年03月06日的数据:
SELECT * FROM garbage_collect WHERE year(garbage_collect.collection_time)="2020" and month(garbage_collect.collection_time)="03" AND DAY(garbage_collect.collection_time)="06";
MYsql 触发器更新
create trigger trigger1 
  after update on boxinfo_lida2024_66064f814e7f9 for each row 
  update switch_boxids 
  set box_status=NEW.box_status,original_info=NEW.original_data 
  where box_id = NEW.box_id; 

查看触发器 Show Triggers;
删除触发器 DROP TRigger XXX;

其他触发器样例
CREATE TRIGGER t2
AFTER
INSERT
ON ord
FOR EACH ROW

 UPDATE goods SET num=num-new.much WHERE gid=new.gid;

 
 
CREATE TRIGGER t3
AFTER
DELETE
ON ord
FOR EACH ROW

 UPDATE goods SET num=num+old.much WHERE gid=old.gid;

 ///更新前操作
CREATE TRIGGER t4
BEFORE
UPDATE
ON ord
FOR EACH ROW

 UPDATE goods SET num=num+old.much-new.much WHERE gid = 1;
批量修改时间
UPDATE hl_box68 set land_time = ADDTIME(DATE('2024-01-10') + interval 0 hour,time(land_time)) ORDER BY id desc limit 228
某个时间范围
select * from 表名 where 时间字段 BETWEEN '开始时间' AND '结束时间';
今天
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) = 1;
一周(7天)
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名);
一月(30天)
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名);
本月
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 QUARTER(create_date)=QUARTER(now());
查询上季度数据
select * from 表名 where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
查询本年数据
select * from 表名 where YEAR(create_date)=YEAR(NOW());
查询上年数据
select * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year));
查询当前这周的数据
SELECT name,submittime FROM 表名 WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查询上周数据
SELECT name,submittime FROM 表名 WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查询上个月数据
select name,submittime from 表名 where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m');
select * from 表名 where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; 
select * from 表名 where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now());
select * from 表名 where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now()); 
select * from 表名 where pudate between  上月最后一天  and 下月第一天; 
查询当前月份的数据
select name,submittime from 表名 where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m');
查询距离当前现在6个月的数据
select name,submittime from 表名 where submittime between date_sub(now(),interval 6 month) and now()
防注入格式化字符串
  public static string ReplaceSQLChar(string str)
        {
            if (str == String.Empty)
                return String.Empty;
            str = str.Replace("'", "");
            str = str.Replace(";", "");
            str = str.Replace(",", "");
            str = str.Replace("?", "");
            str = str.Replace("<", "");
            str = str.Replace(">", "");
            str = str.Replace("(", "");
            str = str.Replace(")", "");
            str = str.Replace("@", "");
            str = str.Replace("=", "");
            str = str.Replace("+", "");
            str = str.Replace("*", "");
            str = str.Replace("&", "");
            str = str.Replace("#", "");
            str = str.Replace("%", "");
            str = str.Replace("$", "");

            //删除与数据库相关的词
            str = Regex.Replace(str, "select", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "insert", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "count", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "asc", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "char", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "or", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "net", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "-", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "delete", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "drop", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "script", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "update", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "chr", "", RegexOptions.IgnoreCase);    
            str = Regex.Replace(str, "master", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "declare", "", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);

            return str;
        }
3

评论 (0)

取消