SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    whether to use 1=1 or not?

    I would like to know if there is any performance problem while using 1=1 condition in sql query.
    for example:
    Code MySQL:
    SELECT
    	*
    FROM
    	`table`
    WHERE
    	1=1
    	AND `field` = value

    Such condition becomes handy in conditional search. For example
    PHP Code:
    if(isset($name)){
    $sql_where .= " AND name='".$name."'";

    ....so on

    And using in query:
    Code MySQL:
    SELECT
    	*
    FROM
    	`table`
    WHERE
    	1=1
    	{$sql_where}

    Yours opinion please!!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    WHERE 1=1 is a valuable coding technique that has no performance penalty

    of course, you only need it in queries which require that the WHERE clause be built on-the-fly dynamically at execution time
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    WHERE 1=1 is a valuable coding technique that has no performance penalty

    of course, you only need it in queries which require that the WHERE clause be built on-the-fly dynamically at execution time
    Thanks rudy.
    Actually i used to perform in this way. But my friend argued that using such techniques of 1=1 makes query slow, so i thought to put in discussion.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    As a static comparison that always evaluates to true it would be stripped out before the optimizer starts processing the rest of the command.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •