Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql query select on two criteria, same field?

Posted on 2009-07-02
16
Medium Priority
?
1,180 Views
Last Modified: 2013-12-07
I want an sql SELECT that will select on the same field
and will use two, separate criteria for that field.

The result should be the subset that satisfies BOTH criteria.

The example below does not work.

How do I do this?


select * from cam where cust='1' and cust='2'

Open in new window

0
Comment
Question by:pillmill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +4
16 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24765190
select * from cam where cust='1' or  cust='2'
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24765196
you never can 1 = 2
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24765204
You can also use IN()

where cust in('1', '2')
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:pillmill
ID: 24765716
Thanks. Both where cust in('1', '2')  and  select * from cam where cust='1' or  cust='2'  are producing the WRONG result.

I want the intersection of the sets of results produced by these two queries:

1. select * from cam where cust='1'

2. select * from cam where cust='2'
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24765759
Hi pillmill,

The intersection will be a NULL set.  "cust" can be 1, or it can be 2, but it can't be both.


Kent
0
 

Author Comment

by:pillmill
ID: 24765896
Thanks. Here are the records:

table cam:

cust     cama
1          a
1           b
2          a
2          c


I want the query to return the value "a" .  The value "a" is the one
which is the same between cust 1 and 2.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24766043

Hmm....  There are a lot of variables here.  Can 'a occur more than once per customer number?  Will it occur only twice?  Are more values than 1 and 2 possible for cust, etc.

Here's one solution:

SELECT cama, count() as c FROM mytable
GROUP BY cama
HAVING c > 1;


Kent
0
 
LVL 11

Expert Comment

by:Andytw
ID: 24766127
Try:
SELECT cama FROM cam WHERE cust = 1
INTERSECT
SELECT cama FROM cam WHERE cust = 2;
 
or
 
SELECT cama c1
FROM cam WHERE cust = 1
WHERE EXISTS ( SELECT NULL
               FROM cam c2
               WHERE c1.cama = c2.cama
                 AND c1.cust <> c2.cust )
  

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 24766220
I suspect your problem may be little more complex than that indicated by your example. Are you trying to find values that exist for more than one customer, that exist for every customer, or do you want entire records (or perhaps more than one field) where those instances occur? Perhaps you could post some more relevant sample data and what your expected output should be.
0
 

Author Comment

by:pillmill
ID: 24766678
SELECT cama c1
FROM cam WHERE cust = 1
WHERE EXISTS ( SELECT NULL
               FROM cam c2
               WHERE c1.cama = c2.cama
                 AND c1.cust <> c2.cust )

produces a MySQL error:

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE exists ( SELECT NULL
0
 

Author Comment

by:pillmill
ID: 24766712
Record "a" is the common element.  

0
 
LVL 32

Expert Comment

by:awking00
ID: 24766861
Change "where exists" to "and exists".
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 24766880
Hi Pill,

In this simple example, the cleanest SQL is an inner join.

SELECT cama
FROM mytable t0, mytable t1
WHERE t0.cama = t1.cama
  AND t0.cust = 1
  AND t0.cust = 2;

But that's probably not a lot of use with a larger data set.  Still, let's see where this goes.  :)


Kent
0
 
LVL 2

Expert Comment

by:kbac
ID: 24771030
select * from cam where
cama in (select cama from cam where cust='1' )
and
cama in (select cama from cam where cust='2' )
0
 

Author Closing Comment

by:pillmill
ID: 31599269
Thanks!
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 24861181
Hi Pill,

For the record, there was a typo in my SQL, but it looks like you figured that out.   :)

Kent
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question