954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is wrong here

Hi
i want to select the values from my table where a column must contains some value. So i wrote this
Select * from RG_TAB where CMCode = 4 and CMCode = 5

This query not giving me any result but

Select * from RG_TAB where CMCode = 4 Or CMCode = 5

is giving me result.
I am confused. What is happening. What should be the query?

Thanks and regards,
sbv

sbv
Junior Poster
178 posts since Jan 2008
Reputation Points: 15
Solved Threads: 8
 

Select * from RG_TAB where CMCode = 4 and CMCode = 5

This query not giving me any result but


if u use this clause (and operator) it means two condition must be available. if just one condition only (ex CMCode=4) then this command didn't give result. so CMcode = 4 & CMcode = 5 must be available to get result.

Select * from RG_TAB where CMCode = 4 Or CMCode = 5 is giving me result.


This clause will give result if one of condition available or two condition available. so you don't needed two condition to give a result, one condition can give result.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

if u use this clause (and operator) it means two condition must be available. if just one condition only (ex CMCode=4) then this command didn't give result. so CMcode = 4 & CMcode = 5 must be available to get result.

This clause will give result if one of condition available or two condition available. so you don't needed two condition to give a result, one condition can give result.

Hi
Thanks for reply.
My Table contents are as follows.....

RID MasterVal CMCode
-------------------------------------
1 Cricket 4
2 Marketing 5
3 bla bla

Now here from my table i want to select records where CMCode is 4 and 5. So is my query is wrong!! :(

sbv
Junior Poster
178 posts since Jan 2008
Reputation Points: 15
Solved Threads: 8
 

yes it is, use "where cmcode = 4 or cmcode = 5" which will give you all the rows where cmcode is either 4 or 5 (it can never be both at the same time, which is why your original query gave no results).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

yes, as jwenting said. it can never be both at the same time.

Sawamura
Junior Poster in Training
69 posts since Nov 2007
Reputation Points: 55
Solved Threads: 6
 

Hi i want to select the values from my table where a column must contains some value. So i wrote this Select * from RG_TAB where CMCode = 4 and CMCode = 5

This query not giving me any result but

Select * from RG_TAB where CMCode = 4 Or CMCode = 5

is giving me result. I am confused. What is happening. What should be the query?

Thanks and regards, sbv

hi
use this query - Select * from RG_TAB where CMCode = 4 Or CMCode = 5

kevs1607
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 2
 

i suggest to use or operand in query, don't use and.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

An alternative which you may see commonly is instead of using multiple references to a field with 'or' is to write something like

Select * from RG_TAB where CMCode in ( 4, 5 )

You can imagine when you have 5 or 6 possible values that this is much shorter and easier to read.
Nige

Nige Ridd
Junior Poster in Training
52 posts since Nov 2007
Reputation Points: 13
Solved Threads: 9
 

An alternative which you may see commonly is instead of using multiple references to a field with 'or' is to write something like

Select * from RG_TAB where CMCode in ( 4, 5 )

You can imagine when you have 5 or 6 possible values that this is much shorter and easier to read. Nige

hi
thanks to all.
what i need is gained by using the query of IN and Not In.

sbv
Junior Poster
178 posts since Jan 2008
Reputation Points: 15
Solved Threads: 8
 

AND means all the conditon must satisfy.

OR means any of the condition should satisfy.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You