简体   繁体   English

为什么 MySQL View 和同一个 View 的底层 SELECT 查询返回不同的结果?

[英]Why does MySQL View and the same View's underlying SELECT query return different results?

I could do with some help here with this issue in which I'm trying to generate a set of results with balances brought forward and balances carried forward.我可以在此问题上提供一些帮助,在此问题中,我试图生成一组结果,将余额提前并结转余额。

The MySQL script: MySQL 脚本:

CREATE OR REPLACE VIEW vwbalances AS
SELECT th.user_id usrid
     , YEAR(th.date_created) year 
     , IFNULL(
              (SELECT SUM(tx.amount) 
                 FROM transdetail tx 
                 JOIN transhdr th 
                   ON th.thdr_id = tx.thdr_id
                 JOIN users u 
                   ON th.user_id = u.user_id 
                 JOIN transtype tt 
                   ON tt.ttype_id = tx.ttype_id 
                WHERE tx.ttype_id in (2,9,11) 
                  AND u.user_id = usrid 
                  and YEAR(tx.date_created) < year
              )
           ,0) bal_bfwd
     , SUM(td.amount) bal_ytd
     , ifnull(
              (SELECT SUM(ty.amount) 
                 FROM transdetail ty 
                 JOIN transhdr th
                   ON th.thdr_id = ty.thdr_id
                 JOIN users u 
                   ON th.user_id = u.user_id 
                 JOIN transtype tt 
                   ON tt.ttype_id = ty.ttype_id 
                WHERE ty.ttype_id in (2,9,11) 
                  AND u.user_id = usrid 
                  AND YEAR(ty.date_created) <= year
              )
           ,0) bal_cfwd
  FROM transdetail td 
  JOIN transhdr th 
    ON th.thdr_id = td.thdr_id
  JOIN users u 
    ON th.user_id = u.user_id 
  JOIN transtype tt 
    ON tt.ttype_id = td.ttype_id 
 WHERE td.ttype_id in (2,9,11) 
 GROUP 
    BY th.user_id
     , YEAR(th.date_created)

I get this (incorrect results) when I run SELECT * FROM vwbalances:当我运行 SELECT * FROM vwbalances 时,我得到了这个(错误的结果):

从视图中得出错误的结果:

And I get this (correct results) when I run the full SELECT statement that I used to create the VIEW:当我运行用于创建 VIEW 的完整 SELECT 语句时,我得到了这个(正确的结果):

来自底层 Select 语句的正确结果

Thanks in advance.提前致谢。

The query you posted shouldn't even work.您发布的查询甚至不应该工作。

  • The usrid is an alias in your most outer select. usrid 是最外层选择中的别名。 It is not available in your subqueries.它在您的子查询中不可用。
  • Unless you have a column called year things like year( tx . date_created ) < year shouldn't work either除非你有一列名为year东西year( tx . date_created ) < year也不应该工作
  • You use the same table alias in your outer query and in your subqueries.您在外部查询和子查询中使用相同的表别名。 This should also not be possible.这也应该是不可能的。 If it is, that's probably why you get weird results.如果是,那可能就是你得到奇怪结果的原因。

Apart from that, you don't have to do the basically same query multiple times.除此之外,您不必多次执行基本相同的查询。 You can shorten your query to something like this:您可以将查询缩短为如下所示:

SELECT `th`.`user_id` AS usrid
    , year(`th`.`date_created`) AS 'year'
sum(if(year(td.date_created`) < year, amount, 0)) as bal_ytd,
sum(if(year(td.date_created`) <= year, amount, 0)) as bal_cfwd
FROM `transdetail` `td`
JOIN `transhdr` `th` ON `th`.`thdr_id` = `td`.`thdr_id`
JOIN `users` `u` ON `th`.`user_id` = `u`.`user_id`
JOIN `transtype` `tt` ON `tt`.`ttype_id` = `td`.`ttype_id`
WHERE `td`.`ttype_id` IN (2, 9, 11)
GROUP BY `th`.`user_id`
    , year(`th`.`date_created`)

(given of course, that this weird year thing works for you) (当然,这个奇怪的year对你有用)

The following script has worked correctly.以下脚本已正常工作。 All help and tips appreciated:感谢所有帮助和提示:

CREATE OR REPLACE VIEW vwbalances AS
SELECT tho.user_id AS usrid, YEAR(td.date_created) AS 'year' 

, ROUND(IFNULL((SELECT sum(tx.amount) FROM transdetail tx 
JOIN transhdr th ON th.thdr_id = tx.thdr_id
JOIN users u ON th.user_id = u.user_id 
JOIN transtype tt ON tt.ttype_id = tx.ttype_id 
WHERE tx.ttype_id IN (2,9,11) AND u.user_id = tho.user_id AND 
YEAR(tx.date_created) < YEAR(td.date_created) ),0),2) AS 
bal_bfwd

, round(sum(td.amount),2) AS bal_ytd

, ROUND(IFNULL((select SUM(ty.amount) FROM transdetail ty 
JOIN transhdr th on th.thdr_id = ty.thdr_id
JOIN users u ON th.user_id = u.user_id 
JOIN transtype tt ON tt.ttype_id = ty.ttype_id 
WHERE ty.ttype_id IN (2,9,11) AND u.user_id = tho.user_id AND 
YEAR(ty.date_created) <= YEAR(td.date_created)),0),2) AS 
bal_cfwd

FROM transdetail td JOIN transhdr tho ON tho.thdr_id = 
td.thdr_id
JOIN users u ON tho.user_id = u.user_id 
JOIN transtype tt on tt.ttype_id = td.ttype_id 
WHERE td.ttype_id IN (2,9,11) 
GROUP BY tho.user_id, YEAR(td.date_created);

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

相关问题 MySQL:与直接使用视图的底层 JOIN 的查询相比,为什么使用 VIEW 的查询效率较低? - MySQL: why is a query using a VIEW less efficient compared to a query directly using the view's underlying JOIN? 为什么mysql在同一查询的2个不同服务器上显示2个不同结果 - Why does mysql show 2 different results on 2 different servers for the same query 为什么MySQL的ENCRYPT会在每次调用时返回不同的结果? - Why does MySQL's ENCRYPT return different results on each call? 为什么相同的确切查询会产生2个不同的MySQL解释结果? - Why does the same exact query produce 2 different MySQL explain results? MySQL SELECT到VIEW返回不同的结果 - MySQL SELECT to VIEW returns different results MySQL View的性能比底层查询的性能差 - Performance of MySQL View is Worse than that of Underlying Query 为什么这个MySQL查询返回0结果 - Why does this MySQL query return 0 results mysql多表查询返回排序结果后再发送查看 - mysql multi table query return sort results before sending to view 为什么使用DAYOFYEAR(DATE)的mysql查询将不同的结果返回DAY(DATE)? - Why Does a mysql Query Using DAYOFYEAR(DATE) Return Different Results to DAY(DATE)? Java JPA和Java JDBC对于相同的MySQL查询返回不同的结果 - Java JPA and Java JDBC return different results for the same MySQL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM