want a query using AND

select * from orders where (company='company' and itemid=0) AND (company='company' and itemid!=0)

this query gets 0 results

I want to write a query with 'AND'
without rewriting query
LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sventhanCommented:
what is the difference between
select * from orders where company='company'
and
select * from orders where (company='company' and itemid=0) AND (company='company' and itemid!=0)?
0
rgb192Author Commented:
select * from orders where (company='company' and itemid=0) AND (company='company' and itemid!=0)

is

select * from orders where $condition1 AND $condition2


0
sventhanCommented:
try like this

select * from orders where condition1 --- item = 0
union
select * from orders where condition2 --- item <>0
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

rgb192Author Commented:
is there a way to do with AND

so I dont have to repeat query 2x
0
sventhanCommented:
select * from orders where (company='company' and itemid=0) OR (company='company' and itemid!=0)
0
rgb192Author Commented:
or doesnt work... I want both using 'and'
0
sventhanCommented:
How do you think this will give you any result?

(itemid = 0) AND (itemid <> 0)

Please post some sample DATA and expected result and we'll figure it out.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ephraim WangoyaCommented:

This is what you are describing

select *
from orders
where (company='company')
and ((itemid=0) OR (itemid!=0))

which negates the whole thing, this simply becomes
select *
from orders
where (company='company')

0
Ephraim WangoyaCommented:

Basically you are looking for a condition where ItemID is 0 and ItemID is not zero
Using AND will return no results
Using OR will return all records
0
Anthony PerkinsCommented:
>>Using OR will return all records <<
Not to get sidetracked that is not entirely correct.  It will not include rows where itemid is NULL.  I know I am being picky and it is irrelevant in the context of this question, but I thought I would bring it up instead of the author.
0
OP_ZaharinCommented:
hi rqb,
- i kind of agree with ewangoya 1st statement on by looking at your code - "select * from orders where (company='company' and itemid=0) AND (company='company' and itemid!=0)" meaning you are looking for (company = 'ABC' AND its itemid = 0) AND (company= 'ABC' AND itemid <> 0), which the same as SELECT * FROM orders WHERE company = 'ABC' without the need to have the itemid condition simply because it will return anything of itemid which is equivalent to itemid = 0 and itemid <> 0.

- my understanding might wrong therefore if you could share list of the data you have in your table and your expected output result, we might be able to assist you.
0
rgb192Author Commented:
I agree, I have to redesign the code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.