SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question exclude results from query

    Hi guys,

    How can I exclude results from the query, I tried using something like this but t obviously didn't work:

    PHP Code:
    SELECT FROM Categories WHERE cat_id !=='21' OR cat_id !=='22' ORDER BY category_name ASC 
    I want to select all expect for the 2 entries (21 & 22)

  2. #2
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, got it sorted using:

    SELECT * FROM Categories WHERE cat_id != '21' AND cat_id != '22' ORDER BY category_name ASC

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could reduce the number of ANDs with this

    Code MySQL:
    SELECT * 
    FROM Categories 
    WHERE cat_id NOT IN ( '21', '22')
    ORDER BY category_name ASC

    I can't recall if you need the single quotes because I usually would the numbers in an array.

    hth

    bazz

  4. #4
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    De Morgan's laws
    From Wikipedia, the free encyclopedia
    In formal logic, De Morgan's laws are rules relating the logical operators "and" and "or" in terms of each other via negation, namely:
    NOT (P OR Q) = (NOT P) AND (NOT Q)
    NOT (P AND Q) = (NOT P) OR (NOT Q)
    Hope this makes the concept clear.

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    De Morgan's laws
    From Wikipedia, the free encyclopedia
    In formal logic, De Morgan's laws are rules relating the logical operators "and" and "or" in terms of each other via negation, namely:
    NOT (P OR Q) = (NOT P) AND (NOT Q)
    NOT (P AND Q) = (NOT P) OR (NOT Q)
    Hope this makes the concept clear.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by IBazz View Post
    I can't recall if you need the single quotes...
    in mysql, the answer is always no, you don't need them

    there are two cases: cat_id is a numeric column, or cat_id is a string column

    (with values like 21 and 22, i already know which it is)

    so if cat_id is a numeric column, you neither need the values in single quotes, nor should you have them (if you have them in any other database system, you get a syntax error, but mysql silently converts the strings to numbers)

    on the other hand, if cat_id is a string column, you don't need the single quotes but you should have them (several database systems will silently convert numeric values to strings for the comparison)

    make sense?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as ever; yep.

    thanks.

    bazz

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I often quote as a defence mechanism. If your id value is coming in from a user request then it might contain malicious sql statements, for example terminating your sql statement and putting in a delete sql statement. Without quotes the delete is executed, with quotes it is just treated as an id value.


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
  •