简体   繁体   English

编写SQL查询以根据另一个表中的值获取所有记录(非联接情况)

[英]Write SQL query to get all records based on value from another table (not join case)

I have two tables, Account and Tracking 我有两个表, AccountTracking
Account table has an ID (int data type) Account表具有ID (int数据类型)
Tracking table has AccountID (FK) and Status (string data type) Tracking表具有AccountID (FK)和Status (字符串数据类型)

I want to write MySQL query. 我想写MySQL查询。

My goal is to get all accounts whose theirs ID number bigger than the biggest ID number recorded in Tracking table 我的目标是获取其ID号大于Tracking表中记录的最大ID号的所有帐户

OR 要么

all accounts that have Status "Failed" in Tracking table. 跟踪表中Status “失败”的所有帐户。

Kindly help 请帮助

I am considering the question as 2 different queries. 我认为这个问题是2个不同的查询。 You can use sub query, if you don't want to use join. 如果不想使用联接,则可以使用子查询。

Query1: 查询1:

select * from Account where id > (select max(AccountID) from Tracking);

Query2: 查询2:

select * from Account where id in (select distinct AccountID from Tracking where status = "Failed");

If I understand correctly: 如果我正确理解:

SELECT  *
FROM    Account a
        JOIN Tracking t
            ON a.ID = t.AccountID
WHERE   (a.ID > (SELECT MAX(AccountID) FROM Tracking)) OR
        (t.Status = 'Failed')

you need a regular join but use a subquery to get the "ID larger than" part of your OR statement 您需要常规联接,但是使用子查询来获得OR语句的“ ID大于”部分

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

相关问题 SQL查询可从一个表中获取所有记录,并在另一个表上进行连接,包括任何其他唯一记录 - SQL Query to get all records from one table and join on another including any additional unique records 根据另一个表的值加入sql查询(在一个查询中) - join sql query based on value of another table (in one query) SQL查询从一个表获取所有记录,除特定记录,按日期,从另一个表 - SQL Query To Get All Records From One Table, Except A Specific Record, By Date, From Another Table SQL查询以基于特定列值获取所有记录 - SQL query to get all records based on a certain column value SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another 从表1中获取记录,并在表2值不存在时从另一个表中连接它 - Get records from table 1 and join it from another table when the table 2 value does not exist Laravel 4-从另一个表的字段中获取价值并加入查询 - Laravel 4 - Get value from another table's field and join to query 如何根据另一个表从连接表复制记录 - How do I copy records from and to a join table based on another 如果联接字段值在另一个表中不存在,则从JOIN查询中获取默认值 - Get default value from JOIN query if join field value doesn't exist in another table SQL:连接另一个表中的值 - SQL: Join value from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM