473,587 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between IN and OR

madhoriya22
252 Contributor
HI,
I am using this query to get the count from table
Expand|Select|Wrap|Line Numbers
  1.  
  2. select count(*) from defect_detail where status = 'RESOLVED' OR 'CLOSED' OR 'VERIFIED'
  3.  
It is retruning count 15. And when I am using this query
Expand|Select|Wrap|Line Numbers
  1.  
  2. select count(*) from defect_detail where status in ( 'RESOLVED' , 'CLOSED' , 'VERIFIED')
  3.  
Then it is returnin count as 19. Which one is the correct?
Aug 29 '07 #1
3 1133
pbmods
5,821 Recognized Expert Expert
Heya, madhoriya.

This is more correct than either of the two:
Expand|Select|Wrap|Line Numbers
  1. select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             'CLOSED'
  10.         OR
  11.             'VERIFIED'
  12.     )
  13.  
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             true
  10.         OR
  11.             true
  12.     )
  13.  
which, of course actually counts ALL the records in the table.

Now, you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             `status` = 'CLOSED'
  10.         OR
  11.             `status` = 'VERIFIED'
  12.     )
  13.  
but it's MUCH faster, especially as your table grows in size, to use IN instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.         `status`
  7.             IN
  8.             (
  9.                 'CLOSED',
  10.                 'RESOLVED',
  11.                 'VERIFIED'
  12.             )
  13.  
Aug 29 '07 #2
madhoriya22
252 Contributor
Heya, madhoriya.

This is more correct than either of the two:
Expand|Select|Wrap|Line Numbers
  1. select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. 'CLOSED'
  10. OR
  11. 'VERIFIED'
  12. )
  13.  
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. true
  10. OR
  11. true
  12. )
  13.  
which, of course actually counts ALL the records in the table.

Now, you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. `status` = 'CLOSED'
  10. OR
  11. `status` = 'VERIFIED'
  12. )
  13.  
but it's MUCH faster, especially as your table grows in size, to use IN instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. `status`
  7. IN
  8. (
  9. 'CLOSED',
  10. 'RESOLVED',
  11. 'VERIFIED'
  12. )
  13.  
Hi,
Thanks buddy. Nice description ! :)
Aug 29 '07 #3
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 :)
Aug 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

34
7059
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
21
2993
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
4390
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)
21
2827
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;...
4
15711
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...
3
4148
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.
12
2705
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
3478
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
9
2664
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>
11
12097
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
0
7849
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...
0
8215
Oralloy
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. ...
0
8220
tracyyun
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...
0
6626
agi2029
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...
1
5718
isladogs
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...
0
5394
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...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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
0
1189
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.