为什么在 MySQL 中每个有效的 SELECT 都不是有效的 VIEW?

发布于 2024-10-25 23:27:48 字数 691 浏览 0 评论 0原文

我想得到一个计数,按天分组。以下 SELECT 工作正常:

  SELECT COUNT( time_end ), 
         time_end
    FROM main
GROUP BY DAY( time_end )

我将其复制/粘贴到 phpmyadmin 的 VIEW 创建表单中,并收到此错误:

"#1064 - 您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,了解在 'count(time_end)) AS SELECT count( time_end ) , time_end 附近使用的正确语法FROM main GROUP BY d' 在第 4 行”

这是从 phpmyadmin 表单生成的完整 SQL:出了

CREATE ALGORITHM = UNDEFINED VIEW `count by day` (
  time_end,
  count( time_end )
) AS SELECT COUNT( time_end ), 
            time_end
       FROM main
   GROUP BY DAY( time_end ) 

什么问题?为什么有效的 SELECT 不会自动创建有效的 VIEW?谢谢。

I want to get a count, grouped by day. The following SELECT works fine:

  SELECT COUNT( time_end ), 
         time_end
    FROM main
GROUP BY DAY( time_end )

I copy/pasted that into the VIEW creation form of phpmyadmin, and get this error:

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'count(time_end)) AS SELECT count( time_end ) , time_end FROM main GROUP BY d' at line 4"

Here is the complete SQL generated from phpmyadmin's form:

CREATE ALGORITHM = UNDEFINED VIEW `count by day` (
  time_end,
  count( time_end )
) AS SELECT COUNT( time_end ), 
            time_end
       FROM main
   GROUP BY DAY( time_end ) 

What's wrong? Why is a valid SELECT not automatically a valid VIEW creation? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2024-11-01 23:27:48

不知道 phpMyAdmin 从哪里获取该语法。使用:

CREATE VIEW COUNT_BY_DAY AS
  SELECT COUNT( time_end ), 
         time_end
    FROM main
GROUP BY DAY( time_end )

请注意,您依赖于 MySQL 的隐藏列功能 - time_end 值是任意的(不能依赖于随着可供选择的值数量的增加而始终返回相同的值)。它也无法移植到大多数数据库,您必须重新编写它。

此外,如果您不按月进行限制,则第 28 - 31 天的计数将会出现偏差。并非所有月份都有 31 天。

Don't know where phpMyAdmin is getting that syntax. Use:

CREATE VIEW COUNT_BY_DAY AS
  SELECT COUNT( time_end ), 
         time_end
    FROM main
GROUP BY DAY( time_end )

Be aware that you are relying on MySQL's hidden column functionality - the time_end value will be arbitrary (can't be relied on to always return the same value as the number of values to choose from increases). It also can't be ported to most databases, you'll have to re-write it.

Also, you're counts are going to be skewed for days 28 - 31 if you aren't limiting by month. Not all months have 31 days.

和影子一齐双人舞 2024-11-01 23:27:48

您的查询唯一的问题是您的列名没有反引号。

创建算法 = 未定义视图按天计数 (
`count( time_end )` # 你需要将其放在反引号中
时间结束,
) AS SELECT COUNT( time_end ),
时间结束
来自主
按天分组(time_end)

就其价值而言,列名的排列方式是错误的。

您使用的语法是视图列名称的显式命名。它允许您为原本从查询列派生的列命名,例如

CREATE VIEW X AS
SELECT time_end, other1
FROM main

:: X contains the columns `time_end` and `other1`

CREATE VIEW X ( TheStopTime, DataPoint ) AS
SELECT time_end, other1
FROM main

:: X contains the columns `TheStopTime` and `DataPoint`

The only thing wrong with your query is that you have a column name that is not back-ticked.

CREATE ALGORITHM = UNDEFINED VIEW count by day (
`count( time_end )` # you needed to put this in backticks
time_end,
) AS SELECT COUNT( time_end ),
time_end
FROM main
GROUP BY DAY( time_end )

For what it's worth, you have the column names around the wrong way.

The syntax you are using is EXPLICIT NAMING of view column names. It allows you to give names to column that would otherwise be derived from the query columns, e.g.

CREATE VIEW X AS
SELECT time_end, other1
FROM main

:: X contains the columns `time_end` and `other1`

CREATE VIEW X ( TheStopTime, DataPoint ) AS
SELECT time_end, other1
FROM main

:: X contains the columns `TheStopTime` and `DataPoint`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文