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 1133 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.
Yensao
|
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:
starttime = Date.parse("Aug 10,2003, 07:07")
sdt = new Date(starttime)
|
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 = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;...
|
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 which
calculated the difference in years and days between two dates, and
takes leap years into account?
I'm calculating the difference in the usual...
| |
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 column I have to show
difference between Currenttime and date from ItemReceived. How can I
do that.
|
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: 1 day, 1hour
|
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 code is just returning me an array of 0's
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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 we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |