MySQL当记录不存在时insert,当记录存在时update

如果指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE。

下面举例说明:

设备使用表equipment_used_log,主键id、代理商、设备、使用日期。
下图三个列定义成唯一索引
68467-chs9mznwb2f.png

- 业务需求是记录每个代理商的某一设备每天的使用量

按照最一般的方法是先查询是否存在,存在则更新不存在则插入。这样会有并发问题。加同步块会影响性能。
使用下列语句即可实现:

INSERT INTO equipment_used_log(id,agent_info_id,equipment_id,log_date,USED_NUM)
VALUES ('1238','1','1','2018-12-18',0)
ON DUPLICATE KEY UPDATE USED_NUM=USED_NUM+1;

Typecho获取显示更新时间 日期格式化 显示发布日期更新日期 时间戳

显示2018-12-15

$this->date();

显示 2018-12-15 09:00

$this->date('Y-m-j H:i');

显示 March 22nd , 2018 at 09:45 am

$this->date('F jS , Y \\a\t h:i a');

获取文章的更新时间时间戳

$this->modified();

显示 2018-12-16 13:11,格式化,注意后面没有括号()

<?php echo date('Y-m-d H:m' , $this->modified); ?>

在使用python的时需使用pip安装各种模块,但是由于国外官方pypi经常被墙导致不可用或下载很慢,所以更换下自己的pip源,下载速度就会很快。

推荐清华大学的pip源,它是官网pypi的镜像,每隔5分钟同步一次,具体说明查看:
https://mirrors.tuna.tsinghua.edu.cn/help/pypi/

pypi 镜像使用帮助
pypi 镜像每 5 分钟同步一次。

临时使用

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
注意,simple 不能少, 是 https 而不是 http
如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tushare

设为默认

升级 pip 到最新的版本 (>=10.0.0) 后进行配置:

pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

更多其他镜像:

https://mirrors.tuna.tsinghua.edu.cn/

Nginx动静分离核心配置

  #动静分离
  server{
    listen 80;
    server_name cc.com;
    access_log off;

    location ~* \.(png|jpg|js|css)$ {
        proxy_pass http://127.0.0.1:8080;
        #所有以.png .html .js .css结尾的url进入此路径  (png|html|js|css)
    }

    location /{
         index index.html index.htm;
         proxy_pass http://127.0.0.1:8080/m/;#注意这里的/的含义见下文
    }

  }

    在Nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

完整配置参考

server {
        listen       80;
        server_name   aaa.com;
    
        #access_log  logs/web.log  main;
        #access_log  off;
        location ~ ^/(WEB-INF)/ {  
            deny all;  
        }
     #rewrite ^(.*)$  https://$host$1 permanent; #用于将http页面重定向到https页面

     #动静分离
     location ~* \.(gif|jpg|png|bmp|swf|js|css)$ {
        expires 30d;  #缓存30天
        proxy_pass http://localhost:8080;
        #所有以.png .html .js .css结尾的url进入此路径  (png|html|js|css)
     }
     
     location / {           
         root   /cycy/web/site/m;

         index index.jsp index.html index.htm;
         proxy_pass   http://localhost:8080/m/;
         proxy_set_header   Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_redirect off;
         client_max_body_size 10m;      
         client_body_buffer_size 128k;  
         proxy_connect_timeout 90;      
         proxy_read_timeout 90;         
         proxy_buffer_size 4k;
         proxy_buffers 6 32k;
         proxy_busy_buffers_size 64k;  
         proxy_temp_file_write_size 64k;     
    }
    
        gzip on;
        gzip_min_length 1k;
        gzip_buffers    4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascripttext/css application/xml;
        gzip_vary on;
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    

扩展阅读

基本语法: location [=|~|~*|^~]/uri/{...}
= 严格匹配, 如果这个查询匹配,将停止搜索并立即处理此请求
~ 区分大小写匹配(可用正则表达式)
~* 不区分大小写匹配(可用正则表达式)
!~ 区分大小写匹配
!~* 不区分大小写匹配
^~ 如果把这个前缀用于一个常规字符串
免责声明
本博客部分内容来自于互联网,不代表作者的观点和立场,如若侵犯到您的权益,请联系[email protected]。我们会在24小时内进行删除。