Stephen Alexander Stephen Alexander - 4 months ago 20
MySQL Question

SELECT * FROM tablename WHERE 1

I've been curious what are the differences between;

    1- SELECT * FROM `tablename`


    2- SELECT * FROM `tablename` WHERE 1


    3- SELECT * FROM `tablename` WHERE 1=1


Thanks!

Answer

2 and 3 are the same, functionally 1 is also the same.

People add 'WHERE 1' or 'WHERE 1 = 1' so WHERE conditions can be easily added or removed to/from a query by adding in/commenting out some "AND ..." components.

i.e.

SELECT * FROM `tablename` WHERE 1=1
--AND Column1 = 'Value1'
AND Column2 = 'Value2'