mysql 从具有 where 条件的 View 中选择给出的结果与使用 where 条件执行 View 定义的结果不同

标签 mysql database view

我有一个具有以下定义的 View :

    select 
        `cb_trans_detail`.`numero_partida` AS `numero_partida`,
        `cb_trans_head`.`fecha_partida` AS `fecha_partida`,
        `cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
        `cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
        `cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
        `cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
        `cb_mayor`.`categoria` AS `categoria`,
        `cb_categoria`.`nombre` AS `nombre`,
        `cb_categoria`.`presentacion` AS `presentacion`,
        `cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
        sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
        sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
        `cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
        ((`cb_cuenta`.`saldo_inicial` + 
       sum(`cb_trans_detail`.`debito_partida`)) - 
       sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
        concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
 AS `Codigo` 
      from 
        ((((`cb_trans_detail` join `cb_cuenta`
      on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`) 
      and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`) 
     and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`)))) 
     join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`) 
     and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`)))) 
     join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`) 
     and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`)))) 
     left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` = 
     `cb_trans_head`.`numero_partida`))) 
      where 
        (`cb_categoria`.`presentacion` = '1')
        group by  concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,
        `cb_trans_detail`.`codigo_cuenta`)

如果我从此 View 中选择如下:

SELECT 
  `balance_general_view`.`numero_partida`,
  `balance_general_view`.`fecha_partida`,
  `balance_general_view`.`concepto_partida`,
  `balance_general_view`.`nombre_cuenta`,
  `balance_general_view`.`codigo_mayor`,
  `balance_general_view`.`nombre_mayor`,
  `balance_general_view`.`categoria`,
  `balance_general_view`.`nombre`,
  `balance_general_view`.`presentacion`,
  `balance_general_view`.`codigo_cuenta`,
  `balance_general_view`.`Debitos`,
  `balance_general_view`.`Creditos`,
  `balance_general_view`.`saldo_inicial`,
  `balance_general_view`.`Saldo`,
  `balance_general_view`.`Codigo`
FROM
  `balance_general_view`
WHERE
  `balance_general_view`.`fecha_partida` BETWEEN '2014-01-01' AND '2014-01-31'

这会产生与我按如下方式执行查询不同的结果:

select 
    `cb_trans_detail`.`numero_partida` AS `numero_partida`,
    `cb_trans_head`.`fecha_partida` AS `fecha_partida`,
    `cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
    `cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
    `cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
    `cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
    `cb_mayor`.`categoria` AS `categoria`,
    `cb_categoria`.`nombre` AS `nombre`,
    `cb_categoria`.`presentacion` AS `presentacion`,
    `cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
    sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
    sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
    `cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
    ((`cb_cuenta`.`saldo_inicial` + sum(`cb_trans_detail`.`debito_partida`)) - sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
    concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`) AS `Codigo` 
  from 
    ((((`cb_trans_detail` join `cb_cuenta` on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`) and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`) and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`)))) join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`) and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`)))) join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`) and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`)))) left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` = `cb_trans_head`.`numero_partida`))) 
  where 
    (`cb_categoria`.`presentacion` = '1') and `cb_trans_head`.`fecha_partida` BETWEEN '2014-01-01' and '2014-01-31'
  group by 
    concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)

我的问题是:如何以编程方式使用 View 和过滤来获取我需要的结果,而不是硬编码 where 条件?谢谢。如果您需要单独的表定义,请告诉我。非常感谢。

最佳答案

如果您使用表 X 的左联接以及该表 X 的 WHERE 子句中的条件,则可以将左联接更改为内部联接。如果您想通过此左连接表的值来限制结果,则必须在左连接的 ON 子句中使用此条件:

...
LEFT JOIN 
    cb_trans_head 
ON
    cb_trans_detail.numero_partida = cb_trans_head.numero_partida
AND
    cb_trans_head.fecha_partida BETWEEN '2014-01-01' and '2014-01-31'
...

如果还有一个我忽略的地方,请以同样的方式处理。

关于mysql 从具有 where 条件的 View 中选择给出的结果与使用 where 条件执行 View 定义的结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233504/

相关文章:

php - 使用 AJAX 和 PHP 将 <options> 附加到 <select>

sql - 生成数据库指标的简单查询?

iOS:这个初始 View 从哪里来以及如何删除它?

android - 如何正确移动 View ?

php - 连续检查mysql数据库中的数据并使用php

mysql - 将一个表的列添加到mysql中的另一个表

MySQL round() 无法按预期使用 sum()

java - 映射文件中的 Hibernate 'Inverse'

java - 如何在hibernate中将条件结果设置为另一个pojo类变量

android - 无法使用此堆栈跟踪找到我崩溃的原因