HI,
I am using this query to get the count from table -
-
select count(*) from defect_detail where status = 'RESOLVED' OR 'CLOSED' OR 'VERIFIED'
-
It is retruning count 15. And when I am using this query -
-
select count(*) from defect_detail where status in ( 'RESOLVED' , 'CLOSED' , 'VERIFIED')
-
Then it is returnin count as 19. Which one is the correct?
3 1129 pbmods 5,821
Recognized Expert Expert
Heya, madhoriya.
This is more correct than either of the two: -
select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
-
Note that the items in the IN subclause are sorted. This way, MySQL can use a binary search on the options, which is much faster than going through the list sequentially.
True, MySQL will automatically sort the items for you, but why make it do all that extra work when the values are constants?
Admittedly, when you only have three options, the time savings isn't great, but it's a good habit to get into.
The problem with this statement, which I will go ahead and 'explode' so you can get a better picture of what's going on: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
(
-
`status` = 'RESOLVED'
-
OR
-
'CLOSED'
-
OR
-
'VERIFIED'
-
)
-
Note that MySQL will only check to see if `status` = 'RESOLVED'. When you use "OR 'CLOSED'", MySQL evaluates 'CLOSED' as true, so in essence your query is doing this: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
(
-
`status` = 'RESOLVED'
-
OR
-
true
-
OR
-
true
-
)
-
which, of course actually counts ALL the records in the table.
Now, you could do this: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
(
-
`status` = 'RESOLVED'
-
OR
-
`status` = 'CLOSED'
-
OR
-
`status` = 'VERIFIED'
-
)
-
but it's MUCH faster, especially as your table grows in size, to use IN instead: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
`status`
-
IN
-
(
-
'CLOSED',
-
'RESOLVED',
-
'VERIFIED'
-
)
-
Heya, madhoriya.
This is more correct than either of the two: -
select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
-
Note that the items in the IN subclause are sorted. This way, MySQL can use a binary search on the options, which is much faster than going through the list sequentially.
True, MySQL will automatically sort the items for you, but why make it do all that extra work when the values are constants?
Admittedly, when you only have three options, the time savings isn't great, but it's a good habit to get into.
The problem with this statement, which I will go ahead and 'explode' so you can get a better picture of what's going on: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
(
-
`status` = 'RESOLVED'
-
OR
-
'CLOSED'
-
OR
-
'VERIFIED'
-
)
-
Note that MySQL will only check to see if `status` = 'RESOLVED'. When you use "OR 'CLOSED'", MySQL evaluates 'CLOSED' as true, so in essence your query is doing this: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
(
-
`status` = 'RESOLVED'
-
OR
-
true
-
OR
-
true
-
)
-
which, of course actually counts ALL the records in the table.
Now, you could do this: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
(
-
`status` = 'RESOLVED'
-
OR
-
`status` = 'CLOSED'
-
OR
-
`status` = 'VERIFIED'
-
)
-
but it's MUCH faster, especially as your table grows in size, to use IN instead: -
SELECT
-
COUNT(*)
-
FROM
-
`defect_detail`
-
WHERE
-
`status`
-
IN
-
(
-
'CLOSED',
-
'RESOLVED',
-
'VERIFIED'
-
)
-
Hi,
Thanks buddy. Nice description ! :)
pbmods 5,821
Recognized Expert Expert
Heya, Madhoriya.
We aim to please.
Good luck with your project, and if you ever need anything, post back anytime :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: yensao |
last post by:
Hi,
I have a hard time to understand difference and similarities between
Relational database model and the Object-Oriented model. Can somebody
help me with this?
Thank you in advance.
...
|
by: b83503104 |
last post by:
Hi,
Can someone tell me the difference between single quote and double
quote?
Thanks
|
by: Frank |
last post by:
For my website i would like to display the age of my son in years,
months, days and hours.
For now i manage to get a result for totals. Like the total number of
days.
This is the beginning:
...
|
by: Rich |
last post by:
I was considering C# for developing a scientific application, but I have
noticed a ~30% difference between VC++ .NET and C# on the same machine,
under identical conditions:
double a = 0,b = 0, c...
|
by: jamesyreid |
last post by:
Hi,
I'm really sorry to post this as I know it must have been asked
countless times before, but I can't find an answer anywhere.
Does anyone have a snippet of JavaScript code I could borrow...
| |
by: bbawa1 |
last post by:
Hi,
I have a table which has a field ItemsReceived of type datetime. I
have a grid view which has two columns.
In first column i have to show the data from field ItemsReceived and
in second...
|
by: Petronius |
last post by:
Hallo, does anyone have an idea how to implement difference lists in
Javascript?
Thanks allot in advance
|
by: Julius |
last post by:
Hej dudes,
I need to calc the difference between two timestamps / dates ...
For example what i need to calculate:
Date 1: 2007.11.06 - 20:13:04
Date 2: 2007.11.07 - 21:13:04
Difference:...
|
by: viki1967 |
last post by:
Hi all!
This new forum its great! :)
Congratulations !!!
My answer: why this my code not working?
Nothing error but not work the difference.... :
<html>
|
by: cmb3587 |
last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays
Ex:
arrayA: 3 5 8 9
arrayB: 3 4 6 9
difference of A-B: 5 8
however, my...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |