mysql在视图中通过列更改组名称,返回结果不正确
我在MySQL数据库中有一个视图的问题。当我选择视图并在其中运行某个子句时,我只能使用列的直接名称,并且不能将其更改为使用AS的其他内容 - 否则内容不会正确显示:
CREATE OR REPLACE
ALGORITHM = UNDEFINED VIEW `View_Donations` AS
select
concat('$', sum(`donators_animal_linking`.`donation_amount`)) AS
`Donation Total`,
`donators_animal_linking`.`animal_type` AS `animal_type`
from
`donators_animal_linking`
group by
`donators_animal_linking`.`animal_type`
SELECT * FROM View_Donations
WHERE 'Animal Type' IS NOT NULL;
即使设置为null的Where子句,null数据仍会显示出来。当将其设置为“动物类型”之类的东西===“海豚”时,就什么都不会出现。我可以更改列名,还是应该离开?
I have an issue with a View in my MySQL Database. When I select the view, and run a WHERE clause on it, I can only use the direct name of the column, and cannot change it to something else using AS - otherwise the content does not get displayed right:
CREATE OR REPLACE
ALGORITHM = UNDEFINED VIEW `View_Donations` AS
select
concat('
Even when the WHERE clause is set to IS NOT NULL, the null data still gets shown. And when it's set to something like 'Animal Type' === 'dolphin', then nothing shows up. Am I able to change the column name, or should I just leave it?
, sum(`donators_animal_linking`.`donation_amount`)) AS
`Donation Total`,
`donators_animal_linking`.`animal_type` AS `animal_type`
from
`donators_animal_linking`
group by
`donators_animal_linking`.`animal_type`
SELECT * FROM View_Donations
WHERE 'Animal Type' IS NOT NULL;
Even when the WHERE clause is set to IS NOT NULL, the null data still gets shown. And when it's set to something like 'Animal Type' === 'dolphin', then nothing shows up. Am I able to change the column name, or should I just leave it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
“动物类型”
中的引号错误。引擎认为它是字符串,而不是列的名称。这就是为什么
不是null
始终是 true (动物类型
是一个定义明确的字符串,因此它不是null)。和
'动物类型'='Dolphin'
将永远是错误的,因为 - 显然 - 两个字符串是不同的。像上面一样,使用反击。You have the wrong kind of quotes in your
'Animal Type'
. The engine considers it a string, not a name of a column.Which is why
IS NOT NULL
is alwaystrue
(Animal Type
is a well-defined string, so it's not null).And
'Animal Type' = 'dolphin'
will be always false, because - obviously - the two strings are different. Use backticks, like you do above.