简体   繁体   中英

How to complete this query function?

I have three tables.

Table A ###(Code, Value are combined primary key)

Code    Value

1     |   b

1     |   c

3     |   c

Table B

Value

b

Table C

Value

c

I would like to write a query of 'Code' from Table A.

The condition is that 'Code' should contain Value 'c'.

If 'Code' contains Value'b', this Code shouldn't be queried. (that's, Code 1 has value b and value c, so Code 1 needs to be excluded)

But I'm not available to do that query.

The expected outcome might be '3'

I want to use intersect but MySql doesn't contain this function yet. So I tried some codes. I'm sure that my codes have problems but I have no idea how to fix it.

SELECT DISTINCT A.*
FROM A B C
WHERE A.Value IN
    (SELECT Value FROM B)
AND A.Value NOT IN
    (SELECT Value FROM C);

Could you give me some tips on my questions?

the problem is that you are joining a, b and c. you almost got it.

select distinct a.value
from a
where a.value in (select value from b)
and a.value not in (select value from c)

or you can use join

select * 
from a 
inner join b where b.value = a.value
    and not in (select value from c)

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