本文介绍了从表中选择1是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了很多查询,内容如下.

I have seen many queries with something as follows.

Select 1  
From table

1是什么意思,它将如何执行以及返回什么?

What does this 1 mean, how will it be executed and, what will it return?

还可以在哪种类型的方案中使用它?

Also, in what type of scenarios, can this be used?

推荐答案

SELECT 1 FROM TABLE_NAME的意思是从表中返回1".它本身并不十分明显,因此通常将它与WHERE并经常与EXISTS一起使用(如@gbn所述,这不一定是最佳实践,但是,即使有,它也足够普遍地被注意到.并没有真正的意义(也就是说,我会使用它,因为其他人会使用它,并且它会立即变得更加明显".当然,这可能是粘性鸡与蛋的问题,但我通常不会对此进行介绍)).

SELECT 1 FROM TABLE_NAME means, "Return 1 from the table". It is pretty unremarkable on its own, so normally it will be used with WHERE and often EXISTS (as @gbn notes, this is not necessarily best practice, it is, however, common enough to be noted, even if it isn't really meaningful (that said, I will use it because others use it and it is "more obvious" immediately. Of course, that might be a viscous chicken vs. egg issue, but I don't generally dwell)).

 SELECT * FROM TABLE1 T1 WHERE EXISTS (
     SELECT 1 FROM TABLE2 T2 WHERE T1.ID= T2.ID
 );

基本上,以上代码将返回表1的所有内容,而表1具有表2的对应ID.(显然,这是一个人为的示例,但我相信它传达了这一想法.就我个人而言,我可能会将其作为,因为我认为 FAR 对读者更为明确,除非有明显的迫不得已的理由不这样做.

Basically, the above will return everything from table 1 which has a corresponding ID from table 2. (This is a contrived example, obviously, but I believe it conveys the idea. Personally, I would probably do the above as SELECT * FROM TABLE1 T1 WHERE ID IN (SELECT ID FROM TABLE2); as I view that as FAR more explicit to the reader unless there were a circumstantially compelling reason not to).

实际上直到现在我都忘记了一种情况.在尝试从外部语言确定数据库中是否存在值的情况下,有时会使用SELECT 1 FROM TABLE_NAME.与选择单个列相比,这不会带来明显的好处,但是,根据实现的不同,与执行SELECT *相比,它可能会带来实质性的收益,这仅仅是因为通常情况下,数据库返回一种语言的列越多,数据库返回的语言越多.较大的数据结构,这又意味着将花费更多时间.

There actually is one case which I forgot about until just now. In the case where you are trying to determine existence of a value in the database from an outside language, sometimes SELECT 1 FROM TABLE_NAME will be used. This does not offer significant benefit over selecting an individual column, but, depending on implementation, it may offer substantial gains over doing a SELECT *, simply because it is often the case that the more columns that the DB returns to a language, the larger the data structure, which in turn mean that more time will be taken.

这篇关于从表中选择1是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 06:48