Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2011, 12:50 PM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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
BubikolRamios is offline   Reply With Quote
Old 10-04-2011, 11:37 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 10-05-2011, 07:01 AM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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
BubikolRamios is offline   Reply With Quote
Old 10-05-2011, 04:26 PM   PM User | #4
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 10-05-2011, 07:14 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,994 Times in 3,963 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 10-05-2011, 07:21 PM   PM User | #6
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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
BubikolRamios is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:46 PM.


Advertisement
Log in to turn off these ads.