mysql在视图中通过列更改组名称,返回结果不正确

发布于 2025-01-21 14:52:27 字数 748 浏览 2 评论 0原文

我在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('

Receiving all Data regardless of the WHERE clausew

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;

Receiving all Data regardless of the WHERE clausew

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 技术交流群。

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

发布评论

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

评论(1

傾旎 2025-01-28 14:52:27

您的“动物类型”中的引号错误。引擎认为它是字符串,而不是列的名称。

这就是为什么不是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 always true (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.

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