Jump to content

where to use where 1


jazzman1

Recommended Posts

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

Link to comment
Share on other sites

"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 ";
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.