为什么在 MySQL 中每个有效的 SELECT 都不是有效的 VIEW?
我想得到一个计数,按天分组。以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道 phpMyAdmin 从哪里获取该语法。使用:
请注意,您依赖于 MySQL 的隐藏列功能 - time_end 值是任意的(不能依赖于随着可供选择的值数量的增加而始终返回相同的值)。它也无法移植到大多数数据库,您必须重新编写它。
此外,如果您不按月进行限制,则第 28 - 31 天的计数将会出现偏差。并非所有月份都有 31 天。
Don't know where phpMyAdmin is getting that syntax. Use:
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.
您的查询唯一的问题是您的列名没有反引号。
就其价值而言,列名的排列方式是错误的。
您使用的语法是视图列名称的显式命名。它允许您为原本从查询列派生的列命名,例如
The only thing wrong with your query is that you have a column name that is not back-ticked.
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.