SQL - 为什么没有办法选择除此列之外的所有内容

发布于 2024-10-19 20:17:20 字数 362 浏览 4 评论 0原文

我的一般问题是,我想从左连接中选择所有列,并且不需要知道连接两个表的 ID。我知道没有必要选择全部,但是由于您需要除 ids 之外的所有字段,为什么没有更短的方法: SELECT * except "this column",我觉得操作时间应该通过这样做来缩短相反的方式?

T1:
aID,
c1,
c2,
c3,
c4

t2:
aID,
c1,
c2,
c3,
c4

Select * from t1 left join t2 on t1.aid = t2.aid

result: t1:aid, c1, c2, c3, c4, aid, c1, c2 ,c3 ,c4

我不想选择我想要的每一列,而是只想选择除“aid”之外的所有列。

My general question is that I want to select all columns from a left join, and I don't need to know the ID that joins the two tables. I know that it is unnecessary to select all, but since you need all the fields except the ids, why isn't there a shorter way to: SELECT * except "this column", i feel like the action time should be shorter by doing the reverse way?

T1:
aID,
c1,
c2,
c3,
c4

t2:
aID,
c1,
c2,
c3,
c4

Select * from t1 left join t2 on t1.aid = t2.aid

result: t1:aid, c1, c2, c3, c4, aid, c1, c2 ,c3 ,c4

instead of selecting each of the columns that I want, I just want to select all except "aid".

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

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

发布评论

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

评论(5

李不 2024-10-26 20:17:20

SELECT * 是一种简写形式,对于快速构建查询原型很有用,但强烈不建议用于生产代码(EXISTS 表达式之外) - 如果有任何架构更改,则任何消耗查询结果的内容都会出现意外的列。同样,您请求的表格也会有同样的问题。

一旦您适当地调整了查询​​(正确连接等),您应该返回到 SELECT 子句并显式列出您实际想要检索的那些列。检索超出您需要的列(包括意外的新列)可能会导致 SQL Server 不得不使用效率较低的计划,或者检索使用者永远不会使用的大量数据。

SELECT * is a shorthand that's useful for quickly prototyping a query, but is strongly not recommended for production code (outside of EXISTS expressions) - if there are any schema changes, then whatever is consuming the results of the query will get unexpected columns appearing. Similarly, your requested form would have the same issue.

Once you've shaped your query appropriately (gotten joins correct, etc), you should go back to your SELECT clause and explicitly list those columns you actually want to retrieve. Retrieving more columns than you need (including unexpected new ones) may cause SQL Server to have to use a less efficient plan, or retrieve massive amounts of data that the consumer is never going to use.

夜光 2024-10-26 20:17:20

我不太确定这是否真的可能。您可能想查看此实例中的动态查询,其中两个连接表中的所有列(外键除外)都将包含在查询中。

您甚至可以考虑编写一个接受 @Table1、@Table2、@PK、@FK 参数的过程,然后根据提供的参数动态构建查询。

I'm not too sure that's really possible. You might want to look at a dynamic query in this instance, where all columns from the two joining tables will be included in the query, except the foreign key.

You can even consider writing a Procedure of sorts that accepts parameters for @Table1, @Table2, @PK, @FK and then build up the query dynamically based on the parameters supplied.

铁憨憨 2024-10-26 20:17:20

过早优化是问题的根源
所有邪恶(或至少大部分)
编程。
– 唐纳德·E·高德纳

为什么你关心 id 列是否返回?

另外,是的,这可能是 SQL 设计中的一个疏忽。

Premature optimization is the root of
all evil (or at least most of it) in
programming.
– Donald E. Knuth

Why do you care if the id column is returned or not?

Also, yes this might be a bit of an oversight in the SQL design.

万劫不复 2024-10-26 20:17:20

出于同样的原因,SELECT * 也不是一个好主意。在查询中使用 SELECT * 往好了说是很烦人,往坏了说是危险的。如果您正在查询的表或数据源发生更改,那么它可能会出现意外的行为。

For the same reason that SELECT * is not a good idea. It can be annoying at best and dangerous at worst to use SELECT * in your queries. If the table or datasource you are querying changes then it can have unexpected behaviour.

没有伤那来痛 2024-10-26 20:17:20

听起来您正在寻找的是NATURAL JOIN

Sounds like what you are looking for is NATURAL JOIN.

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