简体   繁体   中英

Filter column table 1 and get results from table 2 by matching with another field

Setup:

#---------#     #---------#
# table 1 #     # table 2 #
#---------#     #---------#
#ID A    *#  |--#ID B    *#
#ID B    *#--|  #DATA X  *#
#DATA Z   #     #DATA Y   #
#---------#     #---------#

(*) = primary key

Wanted result:

Filter on "ID A" and get all data from table 2 with "ID B"

example:

Table 1

ID A | ID B | DATA Z
  1     2       z1
  1     3       z2
  2     5       z5

Table 2:

ID B | DATA X | DATA Y |
  2      xy      yx
  3      x1      y1
  3      x2      y2
  4      x4      y4

filter on ID A as '1', result:

ID A | ID B | DATA X | DATA Y
  1     2       xy       yx
  1     3       x1       y1
  1     3       x2       y2

Just join the two tables by ID B and filter on Table1:

SELECT table1.idA,
       table2.*
  FROM table1
  JOIN table2
    ON table1.idB = table2.idB
 WHERE table1.idA = 1;

Hope it helps!

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