简体   繁体   中英

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:

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:

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

And I get this (correct results) when I run the full SELECT statement that I used to create the VIEW:

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

Thanks in advance.

The query you posted shouldn't even work.

  • The usrid is an alias in your most outer select. 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
  • 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)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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