简体   繁体   中英

mysql select column in table where two other columns are equal in the referenced tables

I've got four tables A, B, C, and D.

Table A: value1, city
Table B: value2, city
Table C: value3, city
Table D: value1, value2, value3

I need to select value2 (I'm guessing from Table D) where value1 and value3 are in the same row in Table D and both have the same "city" in their rows on tables A and C.

So, if Table A has a rows

13, Chicago
14, Milwaukee
15, St. Louis

and Table C has rows

78, Chicago
89, St. Paul
94, St. Louis

and Table D has rows

13, One, 89
13, Two, 78
14, Three, 78
15, Four, 94

Then I need to print out:

Two
Four
select
  value2
from
  tablea a
  inner join tablec c on a.value1= c.value1
  inner join tableb b on c.value3= b.value3
where
  b.city = a.city
SELECT TableD.value2 
FROM TableD 
JOIN TableC ON TableC.value3=TableD.value3 
JOIN TableA ON TableA.value1=TableD.value1 AND TableA.city = TableC.city

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