[英]Write SQL query to get all records based on value from another table (not join case)
I have two tables, Account
and Tracking
我有两个表, Account
和Tracking
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.