查询名为“name”的列

发布于 2024-12-27 06:28:55 字数 1188 浏览 3 评论 0原文

我正在尝试加入几个表,但我的加入语句失败。我相信这是因为第二个连接中的列名是“name”,也许MySQL认为我正在尝试访问一个属性?我该如何解决这个问题?

SELECT surgery_city.*, s.surgeon_type, st.abbrev
FROM surgery_city 
LEFT JOIN surgery_key as s ON s.surg_id = treatment_id 
LEFT JOIN states as st ON st.name = surgery_city.state
WHERE treatment_id='10001'

问题是我引用 st.name 的第二个左连接 - 关于如何正确引用该列的任何想法?不幸的是,目前无法更改表中的列名称:(..

谢谢,

Silver Tiger

更新:

我在上面的查询中得到的错误是:

[Err] 1267 - Illegal mix of 用于操作“=”的排序规则 (utf8_unicode_ci,IMPLICIT) 和 (utf8_general_ci,IMPLICIT)

当我用反勾包围字段时,

LEFT JOIN states as st ON `st.name` = seo_surgery_city.state

得到以下结果:我得到以下结果相反:

[Err] 1054 - 'on Clause' 中的未知列 'st.name'

它也失败

LEFT JOIN states as st ON st.`name` = seo_surgery_city.state

(单引号 = 那里的反勾号,但它不会在此处正确显示)

[Err] 1267 - 非法混合排序规则(utf8_unicode_ci, IMPLICIT) 和 (utf8_general_ci,IMPLICIT) 的操作 '='

上也失败

LEFT JOIN states as st ON `st`.`name` = seo_surgery_city.state

在[Err] 1267 -操作“=”时非法混合排序规则 (utf8_unicode_ci,IMPLICIT) 和 (utf8_general_ci,IMPLICIT)

I am trying to join a few tables but it is failing on my join statement. I believe it is because the column name in the second join is "name" and perhaps MySQL thinks i am trying to access an attribute? how can i get around this?

SELECT surgery_city.*, s.surgeon_type, st.abbrev
FROM surgery_city 
LEFT JOIN surgery_key as s ON s.surg_id = treatment_id 
LEFT JOIN states as st ON st.name = surgery_city.state
WHERE treatment_id='10001'

The issue is the second left join where i reference st.name - any ideas on how i can reference that column properly? changing the column name in the table is not an option at this point unfortunately :(..

Thanks,

Silver Tiger

UPDATE:

The error I get on the query above is:

[Err] 1267 - Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='

when i surround the field by back ticks i get the following:

LEFT JOIN states as st ON `st.name` = seo_surgery_city.state

I get the following instead:

[Err] 1054 - Unknown column 'st.name' in 'on clause'

It also fails on

LEFT JOIN states as st ON st.`name` = seo_surgery_city.state

(single quotes = back ticks there, but it wont display properly here)

[Err] 1267 - Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='

Also fails on

LEFT JOIN states as st ON `st`.`name` = seo_surgery_city.state

[Err] 1267 - Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='

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

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

发布评论

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

评论(4

星光不落少年眉 2025-01-03 06:28:55

您应该使用反引号包含列名称:

st.`name`

UPD

问题是这些列具有不同的排序规则,请尝试以下操作:

SELECT surgery_city.*, s.surgeon_type, st.abbrev
FROM surgery_city 
LEFT JOIN surgery_key as s ON s.surg_id = treatment_id 
LEFT JOIN states as st ON st.name = (surgery_city.state COLLATE utf8_unicode_ci)
WHERE treatment_id='10001'

但要解决此问题,您应该更新其中一列的排序规则:states .nameseo_surgery_city.state。它们都应该具有 utf8_general_ci

You should embrace the column name with backticks:

st.`name`

UPD

The problem is that the columns have different collations, try the following:

SELECT surgery_city.*, s.surgeon_type, st.abbrev
FROM surgery_city 
LEFT JOIN surgery_key as s ON s.surg_id = treatment_id 
LEFT JOIN states as st ON st.name = (surgery_city.state COLLATE utf8_unicode_ci)
WHERE treatment_id='10001'

But to fix this you should update the collation for one of the columns: states.name or seo_surgery_city.state. They should both have utf8_general_ci.

各空 2025-01-03 06:28:55

尝试将列名称放在反引号中,例如 st.name查看文档

Try putting the column name in backquotes, like st.name. See the docs.

好倦 2025-01-03 06:28:55

您应该对所有表和列使用相同的排序规则和字符集。如果您不知道要使用什么排序规则,请使用 utf8_general_ciutf8 字符集。

ALTER TABLE seo_surgery_city CONVERT TO CHARACTER SET utf8 COLLATE 'utf8_general_ci';
ALTER TABLE seo_surgery_key CONVERT TO CHARACTER SET utf8 COLLATE 'utf8_general_ci';
ALTER TABLE states CONVERT TO CHARACTER SET utf8 COLLATE 'utf8_general_ci';

You should use same collation and charset to all table and columns. If you dont know what collation to use, use utf8_general_ci and utf8 charset.

ALTER TABLE seo_surgery_city CONVERT TO CHARACTER SET utf8 COLLATE 'utf8_general_ci';
ALTER TABLE seo_surgery_key CONVERT TO CHARACTER SET utf8 COLLATE 'utf8_general_ci';
ALTER TABLE states CONVERT TO CHARACTER SET utf8 COLLATE 'utf8_general_ci';
束缚m 2025-01-03 06:28:55

2022 年更新:

关键字“名称” 现已列在 MySQL 官方文档的“关键字和保留字”列表中。您可以在那里找到所有保留字。

参考: https:// dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-N

Update 2022:

The keyword "name" is now listed on the "keywords and reserved words" list of the official MySQL documentation. You can find all the reserved words there.

Reference: https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-N

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