简体   繁体   English

MySQL查询基于另一个表的行值

[英]Mysql query based on row value of another table

Have two mysql tables such as 有两个mysql表,例如

Table1 表格1

Id
Field1
Field2

Table2 表2

Id
LinkId
Field3
Field4

The common fields to link the tables are Table1.Id and Table2.LinkId. 链接表的公共字段是Table1.IdTable2.LinkId.

Also important tha Table2 can have multiple rows where LinkId are the same. 同样重要的是,Table2可以具有多个行,其中LinkId相同。

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. 因此,我一直试图找出一个MySQL查询来选择Table1中的所有行,其中Table2中具有链接的行或更多行,其中Field3包含某个值。 Is this easily possible? 这容易吗?

Simply use JOIN 只需使用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: FROM子句中可以有多个表:

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

暂无
暂无

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

相关问题 如何使用PHP查询MySQL的某些行,然后针对每一行,基于一个UID,基于值查询另一个表? - How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value? MySQL - 根据同一表中的另一个行值计算行值 - MySQL - Calculate a row value based on another row value in the same table MYSQL根据值(如果存在)对SELECT行进行查询,否则从另一个表返回数据 - MYSQL Query to SELECT row based on a value (if exists) otherwise return data from another table 基于mysql中的另一个表单元格值排除的行 - Excluded row based on a another table cell value in mysql mysql根据另一个表中的值删除行 - mysql delete row based on value from another table 根据日期和mysql表中的另一列值获取单行 - Get single row based on the date and another column value in mysql table MySQL查询-根据不同表中的列名获取行值 - Mysql Query - Get row value based on column name in a different table 根据另一个表的第一个值使用PDO查询MYSQL数据库 - Query MYSQL database using PDO based on the first value of another table MYSQL查询将值插入一行并将数据从另一个表复制到另一行 - MYSQL Query to INSERT INTO a value into one row and copy data from another table into another row MySQL根据另一个表中同一行的另一个字段值的重复外观更新布尔值 - MySQL updating a Boolean field value based on the duplicated appearance of another filed value of the same row from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM