美文网首页
2019-03-05

2019-03-05

作者: zhu_ming | 来源:发表于2019-03-05 19:22 被阅读0次

    有关数据库的查询,基于mysql

    1.js有关数据库时间查询问题

    如你的服务器和数据库时间为伦敦标准时间,而你查询的时间为美国洛杉矶时间,可以用以下代码

    startTime:toUTC(moment().subtract(8,'hour').startOf('day').toDate().valueOf()),
    //subtract( , )函数是在取得的时间减掉一个时间,第一个参数为数量,第二个可选为'day','week'等
    endTime:toUTC(moment().subtract(8,'hour').toDate().valueOf())
    

    2.使用sql语句查询数据库时候,当你查出来数据为1 2并且数据表示另外意思,如‘是’或者’否‘,你就可以用以为sql语句

    case when status=0 then 'A'
      when status=1 then 'B'
      when status=2 then 'C'
      when status=3 then 'D'
      else 'E' end
    case when status_id=1 then 'A1' else 'B1' end 
    

    3.ISNULL() 函数的使用

    isnull(check_expression , replacement_value )
    //如果 check_expression 不为 null,那么返回该表达式的值;否则返回 replacement_value
    //使用方法:
    select isnull(price,0) from table where id='id';
    

    4.JSON格式的转换

    5 数据库时间函数的使用,时间的格式化

    DATE_FORMAT(CONVERT_TZ(user.create_time,'+0:00','-8:00'),'%Y-%m-%d %T') createTime,
    //将时间转化为类似于2019-3-8 23:45:13的形式
    

    相关文章

      网友评论

          本文标题:2019-03-05

          本文链接:https://www.haomeiwen.com/subject/pyaouqtx.html