What does the expression MySQL SELECT NULL FROM table_name mean? As I understand it, it is often used in combination with IF EXISTS or IF NOT EXISTS , for example:

 IF NOT EXISTS(SELECT NULL FROM table_name WHERE ...) 

But what is it used for, what is the meaning of this expression?

  • 6
    exists doesn't care what values ​​select selects. from the point of view of optimization, it is better when he chooses not a column (beyond which it may be necessary to climb onto the disk once more), but simply some arbitrary value. NULL is as good as any other. Personally, I usually write select 1 because 1 is three characters shorter and typing faster on the keyboard :) - Mike
  • @Mike, thanks, now everything has become clear :) - Ksenia

2 answers 2

In this case, the presence or absence of a record in the table_name table that satisfies the WHERE condition is simply determined. That is, if there is a record (records), then one (or several) records with one field will be selected as NULL , if there are no such records, the result will be empty.

    The meaning of select null is the output of an empty column (well, you never have to issue 13 columns, and only 12 in the table - this is to add null to select in the table so as not to invent a composite field for which the server spends time and resources). In your example, this is really a check for the presence of records by condition in the WHERE