如果列不同,我真的需要在 JOINS 中包含表名或 AS 吗?

发布于 2024-12-29 03:33:07 字数 313 浏览 1 评论 0原文

我注意到前几天我可以通过这样做轻松地加入 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 技术交流群。

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

发布评论

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

评论(3

神爱温柔 2025-01-05 03:33:07

不,您不需要,但以我拙见,您确实应该。根据我的经验,明确说明你想要做的事情几乎总是更好。

考虑一下那些必须站在你身后的可怜的男人(或女孩)的感受,并尝试弄清楚你想要完成什么以及每列位于哪些表中。明确说明列的来源允许人们在不深入了解架构的情况下查看查询并收集该信息。

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.

策马西风 2025-01-05 03:33:07

查询 1 将起作用(只要没有不明确的列名)。

查询 2 会

  • 更清晰,
  • 更易于维护(想想那些不熟悉数据库模式的人)
  • 在其中一个表中添加不明确的列名后仍然存在

所以,不要因为保存的那可怜的几个击键而偷懒。

Query 1 will work (as long as there are no ambiguous column names).

Query 2 will

  • be clearer
  • be more maintainable (think of someone who doesn't know the database schema by heart)
  • survive the addition of an ambiguous column name to one of the tables

So, don't be lazy because of that pitiful few saved keystrokes.

山川志 2025-01-05 03:33:07

如果没有重复的列名,则没有必要。如果这样做,查询将失败。

It's not necessary if you have no duplicate column names. If you do, the query will fail.

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