1

我希望能够在查询中创建一个临时变量——而不是存储的过程或函数——不需要声明和设置它,这样我在调用它时就不需要传递查询参数。

试图朝着这个方向努力:

   Select field1,
       tempvariable=2+2,
       newlycreatedfield=tempvariable*existingfield
From
       table

远离这个:

DECLARE @tempvariable
SET @tempvariable = 2+2
Select field1,
       newlycreatedfield=@tempvariable*existingfield
From
       table

感谢您的时间

我可能使这个例子过于复杂了。更简单地说,下面给出了 Invalid Column Name QID

Select
QID = 1+1
THN = QID + 1

如果这包含在查询中,是否有解决方法?

4

4 回答 4

1

您可以使用子查询执行此操作:

Select field1, tempvariable,
       (tempvariable*existingfield) as newlycreatedfield
from (select t.*, (2+2) as tempvariable
      from table t
     ) t;

不幸的是,MySQL 倾向于为子查询实际实例化(即创建)派生表。大多数其他数据库都足够聪明,可以避免这种情况。

您可以赌以下将起作用:

Select field1, (@tempvariable := 2+2) as tempvariable,
       (@tempvariable*existingfield) as newlycreatedfield
From table t;

这是一场赌博,因为 MySQL 不保证在第三个参数之前计算第二个参数。它似乎在实践中有效,但不能保证。

于 2013-08-28T00:26:33.987 回答
1

如果您将“隐藏”赋值作为复杂 concat_ws 表达式的一部分,则可以避免派生表和子查询

由于赋值是列的最终期望值表达式的一部分,而不是坐在它自己的列中,因此您不必担心 MySQL 是否会以正确的顺序评估它。不用说,如果你想在多列中使用 temp var,那么所有的赌注都没有了:-/

警告:我在 MySQL 5.1.73 中这样做了;在以后的版本中可能会发生变化

我将所有内容都包装在concat_ws中,因为它将 null args 合并为空字符串,而 concat 没有。

我将分配给 var @stamp 包装if中,以便它被“消耗”而不是成为要连接的 arg。作为旁注,我在其他地方保证 u.status_timestamp 在首次创建用户记录时被填充。然后 @stamp 在date_format的两个地方使用,既作为要格式化的日期,又在嵌套的 if 中选择要使用的格式。最后的 concat 是一个小时范围“hh”,如果 c 记录存在,我保证在其他地方存在,否则它的 null 返回由外部 concat_ws 合并,如上所述。

SELECT
concat_ws( '', if( @stamp := ifnull( cs.checkin_stamp, u.status_timestamp ), '', '' ),
  date_format( @stamp, if( timestampdiff( day, @stamp, now() )<120, '%a %b %e', "%b %e %Y" )),
  concat( ' ', time_format( cs.start, '%l' ), '-', time_format( cs.end, '%l' )) 
) AS as_of
FROM dbi_user AS u LEFT JOIN
  (SELECT c.u_id, c.checkin_stamp, s.start, s.end FROM dbi_claim AS c LEFT JOIN
  dbi_shift AS s ON(c.shift_id=s.id) ORDER BY c.u_id, c.checkin_stamp DESC) AS cs
ON (cs.u_id=u.id) WHERE u.status='active' GROUP BY u.id ;

最后一点:虽然我在此示例中碰巧使用了派生表,但这只是因为需要获取每个用户的最新索赔记录及其关联的班次记录。如果计算临时变量时不涉及复杂连接,您可能不需要派生表。这可以通过转到@Fabien TheSolution's answer中的第一个小提琴并将右手查询更改为

Select field1, concat_ws( '', if(@tempvariable := 2+2,'','') ,
       @tempvariable*existingfield ) as newlycreatedfield
from table1

同样,第二个小提琴(似乎已损坏)的右侧将是

SELECT concat_ws( '', if(@QID := 2+2,'',''), @QID + 1) AS THN
于 2020-06-04T19:43:34.680 回答
1

为什么不只是:

SET @sum = 4 + 7;
SELECT @sum;

输出:

+------+
| @sum |
+------+
|   11 |
+------+

资源

于 2019-07-01T12:41:34.443 回答
0

你可以这样做:

SELECT field1, tv.tempvariable,
       (tv.tempvariable*existingfield) AS newlycreatedfield
FROM table1
INNER JOIN (SELECT 2+2 AS tempvariable) AS tv

请参阅 SQLFIDDLE:http ://www.sqlfiddle.com/#!2/8b0724/8/0

并参考您的简化示例:

SELECT var.QID,
(var.QID + 1) AS THN
FROM (SELECT 1+1 as QID) AS var

请参阅 SQLFIDDLE:http ://www.sqlfiddle.com/#!2/d41d8/19140/0

于 2013-08-28T18:46:26.543 回答