简体   繁体   English

如何从一个表中获取 select 条记录,其中第二个表的一列中所有记录的值与第一个表的多个列中的任何一个相匹配?

[英]How to select records from one table where values from all the records in a column of second table match any of the multiple columns of first table?

There is a table (table A) with columns parent1, parent2..., parent7.有一个表(表 A),其中包含列 parent1、parent2...、parent7。 All these columns contain user_id values, showing user_id values of 7 generations.所有这些列都包含 user_id 值,显示 7 代的 user_id 值。 Now there is another table (table B) which contains a column of some user_id values.现在有另一个表(表 B),其中包含一些 user_id 值的列。 I want to select all the records from table A which contain any of the user_db values from table B in any of table A's parent columns.我想要 select 表 A 中的所有记录,其中包含表 A 的任何父列中表 B 中的任何 user_db 值。 How do I do that?我怎么做?

Table joins is the way to do this.表连接是执行此操作的方法。

SELECT a.*
FROM table_a a
JOIN table_b b ON (
  a.parent1=b.user_id
  OR a.parent2=b.user_id
  OR a.parent3=b.user_id
  OR a.parent4=b.user_id
  OR a.parent5=b.user_id
  OR a.parent6=b.user_id
  OR a.parent7=b.user_id
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL-从链接表中选择first_table_id,其中第二个表中的所有记录均为指定类型 - SQL - select first_table_id from linking table where all records in second table are in specified type 如何从第二个表中未退出的一个表中选择记录 - How to select records from one table that does not exits in second table 如何根据条件选择一个表中的所有记录,而第二个表中没有记录 - How to select all records in one table where no records in second table based on criteria Mysql如何从一个表中选择列值不是X和Y的所有记录 - Mysql How to select all records from one table where column value is not both X and Y 如何显示第一个表中的记录并匹配第二个表中的记录? - How to display the records from the first table and match the records from the second table? 更好的方法来选择第一个表中的所有列,并在内连接上从第二个表中选择一列 - Better way to select all columns from first table and only one column from second table on inner join 当联接表不匹配时,如何从一个表中选择所有记录? - How to select all records from one table when there's not a match on a join table? 从与第二张表匹配的第一张表中选择受限和有序的记录 - Select limited and ordered records from first table matching with second table MySQL SELECT *来自一个表,第二个表中有检查记录 - MySQL SELECT * from one table with check records in second table PHP根据MySql第二个表中字符串中包含的值的匹配从一个表中选择记录 - PHP to select records from one table based on match of value contained in string in second table in MySql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM