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?
 
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
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
sventhanCommented:
try like this

select * from orders where condition1 --- item = 0
union
select * from orders where condition2 --- item <>0
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.