What does it mean by select 1 from table?

前端 未结 15 1795
南方客
南方客 2020-12-07 07:32

I have seen many queries with something as follows.

Select 1  
From table

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

相关标签:
15条回答
  • 2020-12-07 08:05

    If you don't know there exist any data in your table or not, you can use following query:

    SELECT cons_value FROM table_name;
    

    For an Example:

    SELECT 1 FROM employee;
    
    1. It will return a column which contains the total number of rows & all rows have the same constant value 1 (for this time it returns 1 for all rows);
    2. If there is no row in your table it will return nothing.

    So, we use this SQL query to know if there is any data in the table & the number of rows indicates how many rows exist in this table.

    0 讨论(0)
  • 2020-12-07 08:06

    it does what it says - it will always return the integer 1. It's used to check whether a record matching your where clause exists.

    0 讨论(0)
  • 2020-12-07 08:06

    To be slightly more specific, you would use this to do

    SELECT 1 FROM MyUserTable WHERE user_id = 33487
    

    instead of doing

    SELECT * FROM MyUserTable WHERE user_id = 33487
    

    because you don't care about looking at the results. Asking for the number 1 is very easy for the database (since it doesn't have to do any look-ups).

    0 讨论(0)
提交回复
热议问题