如果列不同,我真的需要在 JOINS 中包含表名或 AS 吗?
我注意到前几天我可以通过这样做轻松地加入 mysql,
SELECT peeps, persons, friends FROM tablea JOIN tableb USING (id) WHERE id = ?
而不是使用,
SELECT a.peeps, a.persons, b.friends FROM tablea a JOIN tableb b USING (id) WHERE id = ?
它仅在没有匹配的列名时才有效,为什么我应该执行第二个而不是第一个?
I noticed te other day I can joins in mysql just as easily by doing,
SELECT peeps, persons, friends FROM tablea JOIN tableb USING (id) WHERE id = ?
In stead of using,
SELECT a.peeps, a.persons, b.friends FROM tablea a JOIN tableb b USING (id) WHERE id = ?
It only works if there is no matching column names, why should I do the second rather than the first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不需要,但以我拙见,您确实应该。根据我的经验,明确说明你想要做的事情几乎总是更好。
考虑一下那些必须站在你身后的可怜的男人(或女孩)的感受,并尝试弄清楚你想要完成什么以及每列位于哪些表中。明确说明列的来源允许人们在不深入了解架构的情况下查看查询并收集该信息。
No, you don't need to, but in my humble opinion you really should. It's almost always better in my experience to be explicit with what you're trying to do.
Consider the feelings of the poor guy (or girl) who has to come behind you and try to figure out what you were trying to accomplish and in which tables each column resides. Explicitly stating the source of the column allows one to look at the query and glean that information without deep knowledge of the schema.
查询 1 将起作用(只要没有不明确的列名)。
查询 2 会
所以,不要因为保存的那可怜的几个击键而偷懒。
Query 1 will work (as long as there are no ambiguous column names).
Query 2 will
So, don't be lazy because of that pitiful few saved keystrokes.
如果没有重复的列名,则没有必要。如果这样做,查询将失败。
It's not necessary if you have no duplicate column names. If you do, the query will fail.