What does it mean by select 1 from table?

前端 未结 15 1796
南方客
南方客 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 07:56

    I see it is always used in SQL injection,such as:

    www.urlxxxxx.com/xxxx.asp?id=99 union select 1,2,3,4,5,6,7,8,9 from database;

    These numbers can be used to guess where the database exists and guess the column name of the database you specified.And the values of the tables.

    0 讨论(0)
  • 2020-12-07 07:57

    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
     );
    

    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).

    EDIT

    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.

    0 讨论(0)
  • 2020-12-07 07:58

    The reason is another one, at least for MySQL. This is from the MySQL manual

    InnoDB computes index cardinality values for a table the first time that table is accessed after startup, instead of storing such values in the table. This step can take significant time on systems that partition the data into many tables. Since this overhead only applies to the initial table open operation, to “warm up” a table for later use, access it immediately after startup by issuing a statement such as SELECT 1 FROM tbl_name LIMIT 1

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

    select 1 from table is used by some databases as a query to test a connection to see if it's alive, often used when retrieving or returning a connection to / from a connection pool.

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

    Although it is not widely known, a query can have a HAVING clause without a GROUP BY clause.

    In such circumstances, the HAVING clause is applied to the entire set. Clearly, the SELECT clause cannot refer to any column, otherwise you would (correct) get the error, "Column is invalid in select because it is not contained in the GROUP BY" etc.

    Therefore, a literal value must be used (because SQL doesn't allow a resultset with zero columns -- why?!) and the literal value 1 (INTEGER) is commonly used: if the HAVING clause evaluates TRUE then the resultset will be one row with one column showing the value 1, otherwise you get the empty set.

    Example: to find whether a column has more than one distinct value:

    SELECT 1
      FROM tableA
    HAVING MIN(colA) < MAX(colA);
    
    0 讨论(0)
  • 2020-12-07 08:05

    select 1 from table will return the constant 1 for every row of the table. It's useful when you want to cheaply determine if record matches your where clause and/or join.

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