美文网首页
MySQL报错分析

MySQL报错分析

作者: LucasOoo | 来源:发表于2019-04-07 20:13 被阅读0次

    1、

    Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.  To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
    

    在使用mysql执行update的时候,如果不是用主键当where语句,会报以上错误,使用主键用于where语句中正常。

    2、
    分组条件下必须通过having对分组进行限制

    3、distinct必须放在要查询字段的开头
    错误:select Id,distinct Email from person

    4、leetcode 原题:
    地址:https://leetcode.com/problems/trips-and-users/

    select t2.Request_at as Day,t2.cancel_count/t3.status_sum as 'Cancellation Rate'
    from
    (select t1.Request_at ,count(*)as cancel_count
    from 
    (select Trips.*
    from Trips
    left join Users 
    on Trips.Client_Id = Users.Users_Id
    where Users.Banned = 'No') as t1 #建立嵌套表1,两个表join
    where t1.Status like 'cancelled%'
    group by t1.Request_at) as t2,# 计算指定时间取消的单数
    (select Trips.Request_at , count(*) as status_sum
    from Trips
    left join Users 
    on Trips.Client_Id = Users.Users_Id
    where Users.Banned = 'No'
    group by Trips.Request_at) as t3计算指定时间内的总单数
    where t2.Request_at = t3.Request_at
    

    错误分析:1、输出无2013-10-2的取消率,自动筛选掉无取消订单的日期
    2、嵌套表太多且复杂

    另一解决方案

    select Trips.Request_at as Day,
    round(sum(if (Trips.Status = 'completed',0,1))/count(*) ,2)as 'Cancellation Rate'#使用sum、if函数计算取消的订单数
    from Trips
    where Trips.Client_Id not in (select Users_Id from Users where Banned = 'yes')#建立筛选表
            and Trips.Request_at between '2013-10-01' and '2013-10-03'
    group by Trips.Request_at
    
    SELECT ROUND(column_name,decimals) FROM table_name;
    
    • column_name需要四舍五入的字段
    • decimals保留的小数点位数

    相关文章

      网友评论

          本文标题:MySQL报错分析

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