sql中的concat列名

nimxete2  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(319)

是否有方法将列名合并为t26。 crea + ted_at ?

SELECT t0.id, t0.user, t26.`crea`+`ted_at` FROM users t0
RIGHT OUTER JOIN user_logs t26 ON (t0.id = t26.aid)
WHERE ((t26.`crea`+`ted_at` >= '2018-01-01 00:00:00') AND (t26.`crea`+`ted_at` <= '2019-01-31 23:59:59'));

我只需要从某个特定日期获取数据,但系统正在过滤created\u at列,因为它包含关键字 create

xurqigkl

xurqigkl1#

当连接某个列时,它将变成字符串,并且不能使用sql运算符(>=,<=,>,<)来比较字符串值。
但您可以尝试在两个日期之间提取数据:

SELECT t0.id, t0.user, t26.`crea` `ted_at` 
FROM users t0
RIGHT OUTER JOIN user_logs t26 ON (t0.id = t26.aid)
WHERE (     
            (t26.`crea` >= '2018-01-01' AND ted_at >= '00:00:00') 
            AND 
            (t26.`crea` <= '2019-01-31' AND ted_at <= '23:59:59') 
      )
8fq7wneg

8fq7wneg2#

请使用下面的查询,

SELECT t0.id, t0.user, concat(t26.crea,ted_at) FROM users t0
RIGHT OUTER JOIN user_logs t26 ON (t0.id = t26.aid)
WHERE ((concat(t26.crea,ted_at) >= '2018-01-01 00:00:00') AND (concat(t26.crea,ted_at) <= 
'2019-01-31 23:59:59'));

你也可以用between子句,

SELECT t0.id, t0.user, concat(t26.crea,ted_at) FROM users t0
RIGHT OUTER JOIN user_logs t26 ON (t0.id = t26.aid)
WHERE concat(t26.crea,ted_at) between '2018-01-01 00:00:00' AND '2019-01-31 
23:59:59';
xkftehaa

xkftehaa3#

可以构造一个查询。但必须在存储的例程中执行。参见 PREPARE (带 CONCAT 就像你一样, EXECUTE ,和 DEALLOCATE PREPARE .

相关问题