jazzman1 Posted September 29, 2014 Share Posted September 29, 2014 Hey folks. What's the difference b/w the following two queries? I asked this question b/s in some particular situations I find myself not able to get the right result without where 1 in the query. select count(t1.id) from table1 t1 // and select count(t1.id) from table1 t1 where 1 Quote Link to comment https://forums.phpfreaks.com/topic/291345-where-to-use-where-1/ Share on other sites More sharing options...
Barand Posted September 29, 2014 Share Posted September 29, 2014 "where 1" is a boolean expression equivalent to "where 1 = true". Since 1 is always true then every record meets that criteria and is selected. Without a where clause every record is selected anyway so it is superfluous. It is used (by Dreamweaver, I think) to set up a dummy WHERE clause then append to it in the code with more conditions. eg $where = "WHERE 1 "; if (something) { $where .= "AND some_other_condition "; } if (somethingelse) { $where .= "AND yet_another_condition "; } Quote Link to comment https://forums.phpfreaks.com/topic/291345-where-to-use-where-1/#findComment-1492365 Share on other sites More sharing options...
jazzman1 Posted September 29, 2014 Author Share Posted September 29, 2014 Thanks Barry. Your support is always appreciated by me. Here's the story.A couple of days ago I had to convert sql scripts from one, old mysql database to firebird. So, because my first clash with databases was with firebird, I've never used this syntax before, because it's completely wrong in firebird, instead I'm using where 1=1, where exists ( select .. from ..) or something similar. I found a lot of queries written like in the above example described by you, maybe that developer was used Dreamweaver, I have no clue, anyway that's not interesting part. I just wanted to know if this syntax has a some special meaning in mysql, obviously not and it could be omitted. Quote Link to comment https://forums.phpfreaks.com/topic/291345-where-to-use-where-1/#findComment-1492419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.