简体   繁体   中英

How to join two tables to find two conditions?

So I am trying to list all of the names and ages of people who have not visited the same bar that Ashley has visited.

I currently have two tables: Visits and Drinkers

Drinkers:

`DLicNO     DName     DCity         Age 
AK117229    Ashley    Honolulu      53  
UU761336    Joe       Chicago       51  
ZM193312    Mike      Wilmington    72  
MD891129    Clark     St.Louis      62  
YU134618    Johnson   St.Louis      61  `

Visits:

`DLicNo     BNO        NumberOfTimes    
AK117229    L22174     7    
MD891129    L59871     1    
AK117229    L59871     2    
UU761326    L31927     8    
YU134618    L31927     8    
ZM193312    L889109    6`

I attempted to join them doing this:

SELECT DName, Age 
FROM  visits V, drinkers D 
WHERE V.DLicNo != (
    SELECT DLicNo  
    FROM visits 
    WHERE DLicNo = 'AK117229' 
) 
AND V.DLicNo = D.DLicNO;

I just feel as if I am doing something wrong in the.= condition. Any help would be greatly appreciated.

Use This Two Method:

SELECT 
 DName, 
 Age 
FROM  
   visits V JOIN drinkers D  ON (V.DLicNo =D.DLicNO AND  V.DLicNo != 'AK117229')

OR

SELECT 
  DName, 
  Age 
FROM  
  visits V JOIN  drinkers D ON V.DLicNo =D.DLicNO 
WHERE   
  V.DLicNo != 'AK117229'

You need to figure out the combinations of other drinkers with Ashley in each bar and count them, only showing the results where the count is 0 (ie that person has not drunk in the same bar as Ashley ):

SELECT d1.DName, d1.Age
FROM visits v1
JOIN drinkers d1 ON d1.DLicNO = v1.DLicNO AND d1.DName != 'Ashley'
JOIN visits v2 ON v1.BNO = v2.BNO
LEFT JOIN drinkers d2 ON d2.DLicNO = v2.DLicNO AND d2.DName = 'Ashley'
GROUP BY d1.DName, d1.Age
HAVING COUNT(d2.DLicNO) = 0

Output:

DName       Age
Joe         51
Johnson     61
Mike        72

Demo on dbfiddle

You are right about ".=", Generally speaking. you have to use "NOT IN (select..,)". but in your case you do not need that extra "SELECT": This is the right form:

SELECT DName, Age 
FROM  visits V JOIN drinkers D ON V.DLicNo = D.DLicNO
WHERE V.DLicNo != 'AK117229';

I guess you need to do something like below:

SELECT DName, AGE FROM Drinkers D INNER JOIN visits V ON D.DLicNo = V.DLicNo WHERE V.BNO NOT IN 
   (SELECT V1.BNO  
    FROM visits V1
    WHERE V1.DLicNo = 'AK117229')

It is simple and short and easy to understand

Here is the working fiddle

You can try with CTE option. It is tested in MYSQL 8.0 Version, where CTE was introducted. Read more on Common Table Expressions in MySQL

WITH ashleybars as
(
  SELECT BNO
  FROM visits
  WHERE DLicNo = 'AK117229'
), otherpersonsOfsamebar as 
(
  select distinct DLicNo
  from visits as v inner join ashleybars as b
  where v.bno = b.bno
  )
  select * from drinkers
  where DLicNo NOT IN (select DLicNo FROM otherpersonsOfsamebar);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM