PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] When I create VIEW there is a wrong result, while simple SQL query gives a correct result

 October 11, 2022     mysql, phpmyadmin, sql-view     No comments   

Issue

This is how I define a rule for my VIEW:

SELECT `yearByWeek`, `week`, ( SELECT MIN(dolphin_day.date) ) AS 'start', ( SELECT SUM(dolphin_day.countHour)) AS 'countHours'

FROM `dolphin_day`
GROUP BY `yearByWeek`, `week`
ORDER BY `yearByWeek` DESC, `week` DESC

❌ wrong result for VIEW is the following:

enter image description here

✅ correct result for SQL query:

enter image description here

Why result for view is totally wrong?


Solution

The aggregations shouldn't be in subqueries.

SELECT `yearByWeek`, `week`, MIN(date) AS 'start', SUM(countHour) AS 'countHours'
FROM `dolphin_day`
GROUP BY `yearByWeek`, `week`
ORDER BY `yearByWeek` DESC, `week` DESC


Answered By - Barmar
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing