查询名为“name”的列
我正在尝试加入几个表,但我的加入语句失败。我相信这是因为第二个连接中的列名是“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用反引号包含列名称:
UPD
问题是这些列具有不同的排序规则,请尝试以下操作:
但要解决此问题,您应该更新其中一列的排序规则:
states .name
或seo_surgery_city.state
。它们都应该具有utf8_general_ci
。You should embrace the column name with backticks:
UPD
The problem is that the columns have different collations, try the following:
But to fix this you should update the collation for one of the columns:
states.name
orseo_surgery_city.state
. They should both haveutf8_general_ci
.尝试将列名称放在反引号中,例如
st.name
。 查看文档。Try putting the column name in backquotes, like
st.name
. See the docs.您应该对所有表和列使用相同的排序规则和字符集。如果您不知道要使用什么排序规则,请使用
utf8_general_ci
和utf8
字符集。You should use same collation and charset to all table and columns. If you dont know what collation to use, use
utf8_general_ci
andutf8
charset.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