简体   繁体   中英

SQL: How compare cells from different tables?

I have two tables - band and band2. Band:

乐队表

and band2:

在此输入图像描述

The columns are equals. I'm using Access 2010. I want to SELECT rows WHERE band.Kampas<>band2.Kampas, but there isn't primary key so I can't use JOIN operation. Maybe someone has an idea?

The answer:

在此输入图像描述

Only in these rows band.Kampas<>band2.Kampas. Thanks in advance.

If I understand you correctly this is what you want:

Select * from band where kampas not in (select kampas from band2)
union
Select * from band2 where kampas not in (select kampas from band)

EDIT. Ok, might be that not in doesn't work in Access. It looks like this could work, though:

Select * from band2 where not exists (select * from band where band.kampas = band2.kampas)

This find a selection in the inner select where kampas's match and we want to pick those band2 lines that returns an empty selection in the inner select.

If you want to do this two-way (ie also find from band) just use union like I did in the first attempt.

SELECT b2.*
FROM band2 b2
WHERE b2.kampas NOT IN (SELECT b1.kampas
                        FROM band b1 
                        WHERE b1.kampas IS NOT NULL)
  AND b2.kampas IS NOT NULL    

How about this:

select * 
from Band as B1 
inner join Band2 as B2 
on B1.Stotis = B2.Stotis
where B1.Kampas <> B2.Kampas

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