繁体   English   中英

MySQL子句在哪里子句

[英]Mysql variable in where clause

当我添加最后一行“ 和@summ> 0 (任何条件)”时,它将返回空结果。 我做错了什么? 如何正确地将@summ传递给where子句?

 select
      nore.nis as nore_curent,
      field_funds.value,
     (select @summ :=
        sum(field_amount.value - field_amount_tip.value)
          from
        nore
        inner join field_project on nore.nis = field_project.id
        inner join field_funds on field_funds.id = field_project.target_id
        inner join field_amount on nore.nid = field_data_field_amount.id
        inner join field_amount_tip on nore.nis = field_amount_tip.id
          where
        nore.`s` = 1
          and
        nore.`t` = 'p'
          and
        field_project.target_id = nore_curent) as amount
      from
       nore
      inner join field_funds on nore.nis = field_funds.id
       where
      nore.`s` = 1
       and
      nore.`t` = 'pp'
       and @summ > 0

这是值得在这里remaind相关的概念(或逻辑)一个选择陈述书的评价为了一些基础知识: http://tinman.cs.gsu.edu/~raj/sql/node22.html

顺序是:

  1. 哪里
  2. 通过...分组
  3. 拥有
  4. 订购
  5. 选择


在SELECT子句之前先评估(执行)WHERE子句。
@summ的值是在(主查询的)SELECT子句中计算的,因此当DMBS评估WHERE子句时,该值是未知的。

未知= NULL

因此,此查询将无法正常工作。

将查询重写为:

select
      nore.nis as nore_curent,
      field_funds.value,
     (select sum(field_amount.value - field_amount_tip.value)
          from
        nore
        inner join field_project on nore.nis = field_project.id
        inner join field_funds on field_funds.id = field_project.target_id
        inner join field_amount on nore.nid = field_data_field_amount.id
        inner join field_amount_tip on nore.nis = field_amount_tip.id
          where
        nore.`s` = 1
          and
        nore.`t` = 'p'
          and
        field_project.target_id = nore_curent) as amount
      from
       nore
      inner join field_funds on nore.nis = field_funds.id
       where
      nore.`s` = 1
       and
      nore.`t` = 'pp'
       and 0 < (select sum(field_amount.value - field_amount_tip.value)
                  from nore
                  inner join field_project on nore.nis = field_project.id
                  inner join field_funds on field_funds.id = field_project.target_id
                  inner join field_amount on nore.nid = field_data_field_amount.id
                  inner join field_amount_tip on nore.nis = field_amount_tip.id
                  where
                    nore.`s` = 1
                    and
                    nore.`t` = 'p'
                    and
                    field_project.target_id = nore_curent
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM