mysql: 两个字段合并,字符时间转时间戳,别名字段作为where条件查询

有字段,a,b:

a存的是:2016-10-10

b存的是:10:15:30

mysql将字段合并:

concat(a, ' - ', b)  或者 concat(a, ' ', b)

字符时间转时间戳

unix_timestamp( concat(a, ' ', b) )

别名字段作为where条件:

在MySQL中有个特殊的规定,即不允许使用列别名作为查询条件。比如有下面一个表:

select 
    ID, 
    title, 
    concept, 
    conceptLength, 
    addUserId, 
    modifyTime
from collections_wisdom

将SQL修改如下:

select 
    ID+1 as newID
    title, 
    concept, 
    conceptLength, 
    addUserId, 
    modifyTime
from collections_wisdom
where newID>2

那么,执行起来就会出现异常:StatementCallback; bad SQL grammar

实在要执行,只好把新字段的组成在条件里再实现一遍,如下:

select 
    ID+1 as newID, 
    title, 
    concept, 
    conceptLength, 
    addUserId, 
    modifyTime
from collections_wisdom
where (ID+1)>2

之所以MySQL中不允许使用列别名作为查询条件,据说是因为MySql中列的别名本来是返回结果的时候才显示的,不在SQL解析时候使用。在没有更令人信服的解释出现前,权且当做这样吧。

或者可以这样,将合并的字符直接放在where里面

unix_timestamp(concat(a,' ', b)) BETWEEN :startTime: AND :endTime:
原文地址:https://www.cnblogs.com/achengmu/p/5979923.html