Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: count syntax

  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts

    count syntax

    Code:
    select id_galery,count(id)=2 from galery_optional_inf
    where id in (13)
    This is legal, as it executes. Any explanation what "=2" does ?
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It compares the value of COUNT(id) with 2 and produces either TRUE or FALSE.

    Because MySQL doesn't support a *true* boolean type, TRUE is converted to the integer (or bit) value 1. FALSE becomes 0.

    Code:
    mysql> select * from zap;
    +------+-------+
    | id   | flags |
    +------+-------+
    |    1 |    10 |
    |    2 |     6 |
    |    3 |     9 |
    +------+-------+
    3 rows in set (0.25 sec)
    
    mysql> select count(id) = 2 from zap;
    +---------------+
    | count(id) = 2 |
    +---------------+
    |             0 |   [in other words, "FALSE"]
    +---------------+
    1 row in set (0.05 sec)
    
    mysql> select count(id) = 3 from zap;
    +---------------+
    | count(id) = 3 |   [in other words, "TRUE"]
    +---------------+
    |             1 |
    +---------------+
    1 row in set (0.00 sec)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Is there any practical/performance use of it ?
    Besides that it works I cant see any.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, lets say you want to make a forum with a VIP club... eg, everyone over a certain number of posts. You can do COUNT(*) = 1000 or COUNT(*) > 1000, etc, and it'll show if someone is or isn't a VIP.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Good example, Keleth. It's nothing that the host language (e.g., PHP/ASP/JSP) couldn't do, of course, but sometimes it's nice to do it in the query, instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Thanks to both.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

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