-1

I have a SQL SELECT Statement which is,

SELECT * FROM users WHERE ""=""

The result is it returns all the rows.

So, what does ""="" mean?

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
shiv
  • 191
  • 2
  • 11

3 Answers3

3

This is an unnecessary WHERE clause indicating that all rows should be selected.

The = operator is used to check equality, and a WHERE clause will return all records where a specific condition is true (in this case ""=""). Since this condition will always evaluate as true (as an empty string is always equal to an empty string), all of the rows within the table will be selected.

You could leave it off entirely to receive the same results :

SELECT * 
  FROM users
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

IMHO the last part can be deleted. This where statement compares for each dataset two static values, so it is all the same for each one.

Julian Kuchlbauer
  • 895
  • 1
  • 8
  • 17
0

A construct like this is typically used to construct where clauses in dynamic SQL. For instance, if a user could select name or country, then you can write the query as:

where (name = @name or @name is null) and
      (county = @country or @country is null)

Although this captures the logic, it does not optimize well. So, often applications use dynamic SQL to construct the logic:

set @where = '1=1';
set @where = concat(@where, if(@name is not null, ' and name = @name', ''));
set @where = concat(@where, if(@country is not null, ' and country = @country', ''));

The use of "" = "" would just be an alternative form of 1=1.

I can see how this would fall into a discussion on SQL injection, because they both relate to user input.

In practice, though, this is a bit lazy, because it is possible to write the query without the additional comparison (although MySQL will optimize it away during the compilation phase).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786