Solved

return row only if value not in other row

Posted on 2014-03-11
3
44 Views
Last Modified: 2016-06-04
Have the following subset of tblMember:

mem_num     product_type         address
1                          A                      main st
1                          C                      main st                      
2                          A                      1st street
2                          C                      1st street
2                          D                      1st street
3                          A                      2nd street
3                          B                      2nd street
4                          A                      3rd street
4                          C                      3rd street

I want to select only rows with product_type 'A', if mem_num does not have product type 'B' in another row or

mem_num       product_type         address
1                          A                         main
2                          A                         1st Street
4                          A                         3rdStreet

Thanks!
0
Comment
Question by:needplease
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39919841
SELECT * FROM tblMember 
    WHERE product_type='A' AND mem_num NOT IN (
    SELECT mem_num FROM tblMember WHERE product_type='B'
)

Open in new window


HTH,
Dan
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39919901
I prefer for this kind of queries the NOT EXISTS syntax, especially if the number of rows having the product_type='B' here to be huge
SELECT m.*
  FROM tblMember  m
    WHERE m.product_type='A' 
AND NOT EXISTS( 
    SELECT null 
   FROM tblMember o
   WHERE o.product_type='B'
       and o.mem_num  = m.mem_num 
) 

Open in new window

you shall have a (single) index on the columns mem_num + product_type to ensure the query runs as fast as possible
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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