Select 1 from... What does 1 in an SQL statement mean?

Source: Internet
Author: User

Select 1 from... what does 1 in an SQL statement mean? What is the result?

Select 1 from table; and select anycol (any row in the target table set) from table; there is no difference between select * from table and select * from table in terms of function. check whether there are records, it is generally used for conditional queries. 1 in select 1 from is a constant (which can be any number), and the value of all rows is it,But in terms of efficiency, 1> anycol> *, because no dictionary table is needed.


 

Test scenario: (transferred from network literature)
A table is a data table. Assume that the number of rows in the table is 10.

1: select 1 from table: Add a temporary column. The column values in each row are the numbers written after select, and the column values in this SQL statement are 1.

2: How does the value of select count (1) from Table tube count (a) Change? The resulting value is always the number of rows in the table.

3: Select sum (1) from Table calculate the sum of the temporary Columns

 

I tested it with 1 in SQL Server and found the results are as follows:

1: The test result shows a temporary column with the same number of rows as the number of rows in the Table (I do not know what to call this). The column value of each row is 1;

2: Get a number, which is the number of rows in the table;

3: Get a number, which is the number of rows in the table;

Then I tested it with "2" and the results were as follows:

1: Get a temporary column with the same number of rows as the number of rows in the table. The column value of each row is 2;

2: Get a number, which is the number of rows in the table;

3: Get a number, which is the number of rows in the Table × 2

Then I tested it with a larger number:

1: Get a temporary column with the same number of rows as the number of rows in the table. The column value of each row is the number I wrote after the SELECT statement;

2: still get a number, which is the number of rows in the table;

3: Get a number, which is the number of rows in the table × the number written after the SELECT statement.

To sum up, the first method is to add a temporary column, and the column values in each row are the numbers written after the SELECT statement. The second method is regardless of how the value of Count (a) changes, the obtained value is always the number of rows in the table. The third is to calculate the sum of the temporary columns..
 

Statement if not exists (select 1 from deleted d join inserted I on D. id = I. ID
And D. col1 = 1 and I. col1 = 2)

If not exists if it does not exist
(...) Here it is to verify the update, the ID before the update is equal to the updated ID, and the col value before the update is 1, the updated value is 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.