是否可以在SELECT之前使用SELECT而不使用SET?
不知怎的'@period'变量并不总是正常工作。例如,在MySQL Workbench(v5.2)中,结果仅在第二次调用后显示,如果更改了变量,结果将仅在第二次运行时才正确。 phpMyAdmin显示正确的行数,但没有条目,有人可以解释一下使用变量这样的错误:
select sql_no_cache @num := @num + 1 as `#`, l.* from (
select s.* from (
select
@num := 0 as 'Label 1',
@period := 31 as 'LAbel 2',
'' as 'Label 3'
union
select concat(users.first_name, ' ',users.last_name),u.type, u.date, u.description from (
select c.user_id as uguid, 'type1' as type, c.date_start as date, c.description from table1 c
where deleted = 0
and date_start > SUBDATE(CURRENT_DATE,@period)
union
...
) as u) as l
答案 0 :(得分:0)
发现了一个错误。 原因是变量在定义之前使用。 '@period'在查询中定义,但它首先在首先执行的子查询中出现。 因此,解决方案是将变量定义与其使用的子查询放在同一级别上。
类似的东西:
select sql_no_cache @num := @num + 1 as `#`, l.* from (
select s.* from (
select
@num := 0 as 'Label 1',
'' as 'LAbel 2',
'' as 'Label 3'
union
select concat(users.first_name, ' ',users.last_name),u.type, u.date, u.description from (
select o2.* from
(select @period:=31) as o1
join (
select c.user_id as uguid, 'type1' as type, c.date_start as date, c.description from table1 c
where deleted = 0
and date_start > SUBDATE(CURRENT_DATE,@period)
union
...
) as o2
) as u
) as l