Not clear about this query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Babu

    Not clear about this query

    select *From titles

    (18 row(s) affected)

    Totally 18 records are there in Titles table.

    When I query

    select title,title_id From titles where title like 'T%' and title_id
    like 'M%'

    It is returning 2 records.

    So,now if I introduce a not in 2 places also it should return 16
    records.
    Whereas it is returning 14 records. What is the bestway to understand
    this.

    select title,title_id From titles where title not like 'T%' and
    title_id not like 'M%'

    Thanks
    Ganesh
  • John Bell

    #2
    Re: Not clear about this query

    Hi

    You should get 16 records if you use an OR instead of the AND in the where
    clause.

    NOT ( A AND B )
    NOT A OR NOT B

    John

    "Babu" <babuganesh2000 @yahoo.com> wrote in message
    news:bc5c149.03 07210111.79fb11 2c@posting.goog le.com...[color=blue]
    > select *From titles
    >
    > (18 row(s) affected)
    >
    > Totally 18 records are there in Titles table.
    >
    > When I query
    >
    > select title,title_id From titles where title like 'T%' and title_id
    > like 'M%'
    >
    > It is returning 2 records.
    >
    > So,now if I introduce a not in 2 places also it should return 16
    > records.
    > Whereas it is returning 14 records. What is the bestway to understand
    > this.
    >
    > select title,title_id From titles where title not like 'T%' and
    > title_id not like 'M%'
    >
    > Thanks
    > Ganesh[/color]


    Comment

    • Ross Presser

      #3
      Re: Not clear about this query

      babuganesh2000@ yahoo.com (Babu) wrote in
      news:bc5c149.03 07210111.79fb11 2c@posting.goog le.com:
      [color=blue]
      > select *From titles
      >
      > (18 row(s) affected)
      >
      > Totally 18 records are there in Titles table.
      >
      > When I query
      >
      > select title,title_id From titles where title like 'T%' and title_id
      > like 'M%'
      >
      > It is returning 2 records.
      >
      > So,now if I introduce a not in 2 places also it should return 16
      > records.
      > Whereas it is returning 14 records. What is the bestway to understand
      > this.
      >
      > select title,title_id From titles where title not like 'T%' and
      > title_id not like 'M%'
      >
      > Thanks
      > Ganesh[/color]

      Apparently two of your records satisfy one but not both of the LIKE
      clauses; therefore they are not included in your latter statement.

      .... where title like 'T%' and title_id like 'M%' : 2 records
      .... where title not like 'T%' and title_id not like 'M%' : 14 records
      .... where not (title like 'T%' and title_id like 'M%): 16 records


      --
      Ross Presser -- rpresser AT imtek DOT com
      "... VB is essentially the modern equivalent of vulgar Latin in 13th
      Centurary Europe. Understand it, and you can travel to places you never
      heard of and still understand some people." -- Alex K. Angelopoulos

      Comment

      Working...