一个炒鸡简单的 SQL 为什么执行没返回结果?? - V2EX
请不要在回答技术问题时复制粘贴 AI 生成的内容
cway

一个炒鸡简单的 SQL 为什么执行没返回结果??

  •  
  •   cway Jul 7, 2019 3853 views
    This topic created in 2516 days ago, the information mentioned may be changed or developed.

    SELECT * FROM LAOHU WHERE time <= 'DATE('2012-12-31')' 测试有返回结果,但是:

     SELECT * FROM LAOHU WHERE time >= 'DATE(\'2012-12-31\')' 就没返回结果了,可两个语句几乎一毛一样啊??究竟怎么一回事,有知道的给解析下么? 
    9 replies    2019-07-17 16:58:13 +08:00
    az402
        1
    az402  
       Jul 7, 2019 via Android
    首先要看数据量
    Coande
        2
    Coande  
       Jul 7, 2019 via Android
    一个小于等于,一个大于等于,条件不一样吧
    congeec
        3
    congeec  
       Jul 7, 2019
    两个语句几乎一毛一样

    几乎

    做程序员要严谨

    首先 date 外的单引号拿掉,其次二楼说的对
    watzds
        4
    watzds  
       Jul 7, 2019 via Android
    这瞎写能用就怪了
    tomczhen
        5
    tomczhen  
       Jul 7, 2019 via Android
    列出所有大于 2 的正整数,这个问题很简单。按楼主的逻辑,列出所有小于 2 的正整数,也应该一样简单,麻烦回答一下吧。
    Juszoe
        6
    Juszoe  
       Jul 7, 2019
    不懂时间和一个字符串比较有什么意义,不是应该 time >= DATE('2012-12-31')
    skull
        7
    skull  
       Jul 7, 2019
    请尽量让自己的回复能够对别人有帮助
    littlewing
        8
    littlewing  
       Jul 7, 2019 via iPhone   1
    The following rules describe how conversion occurs for comparison operations:

    If one or both arguments are NULL, the result of the comparison is NULL, except for the NULL-safe <=> equality comparison operator. For NULL <=> NULL, the result is true. No conversion is needed.

    If both arguments in a comparison operation are strings, they are compared as strings.

    If both arguments are integers, they are compared as integers.

    Hexadecimal values are treated as binary strings if not compared to a nmber.

    If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. This is not done for the arguments to IN(). To be safe, always use complete datetime, date, or time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.

    A single-row subquery from a table or tables is not considered a constant. For example, if a subquery returns an integer to be compared to a DATETIME value, the comparison is done as two integers. The integer is not converted to a temporal value. To compare the operands as DATETIME values, use CAST() to explicitly convert the subquery value to DATETIME.

    If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value.

    In all other cases, the arguments are compared as floating-point (real) numbers.

    For information about conversion of values from one temporal type to another, see Section 11.3.7, “ Conversion Between Date and Time Types ”.
    cway
        9
    cway  
    OP
       Jul 17, 2019
    首先感谢大家 回去我测试了下 @az402
    @Coande
    @congeec 跟条件貌似没关系,单引号也是没影响,只是发现用非日期字符串例如非 yyyy-MM-dd 之类的字符串识别不了,虽然<=2012 但是返回值把所有记录全返回了。但是<='2012-12-31'就没问题
    @watzds 也不是瞎写的,去掉转义符跟外面引号肯定可以,mybatis plus 中 gt 条件传 Date 类型数据会自动转成这种带转义的形式,后来我直接传字符串了
    @tomczhen
    @Juszoe 亲自试验下会发现 time <= '2018-02-01' 这种也是可以的 之前我也怀疑,
    @skull @littlewing 感谢
    About     Help     Advertise     Blog     API     FAQ     Solana     3106 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 13:47 PVG 21:47 LAX 06:47 JFK 09:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86