简体   繁体   中英

Mysql query based on row value of another table

Have two mysql tables such as

Table1

Id
Field1
Field2

Table2

Id
LinkId
Field3
Field4

The common fields to link the tables are Table1.Id and Table2.LinkId.

Also important tha Table2 can have multiple rows where LinkId are the same.

So what I have been trying to figure is a mysql query to Select all rows in Table1 that have a linked row or more in Table2 where Field3 contains a certain value. Is this easily possible?

Simply use JOIN

SELECT Table1.*
FROM Table1 A JOIN Table2 B ON A.Id = B.LinkID
WHERE B.Field3 IN ('Your inputs')

You can have multiple tables in the FROM clause:

SELECT *
FROM Table1, Table2
WHERE Table2.Field3 = 'certain value'
AND Table1.Id = Table2.LinkId

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