简体   繁体   English

从一个表中选择所有字段,其中另一个ID为ID的表中的字段等于一个字符串

[英]Select all fields from a table where a field in another table with an ID is equal to a string

I have 2 tables: 我有2张桌子:

Ads : Fields ID , A , B and C : 广告 :字段IDABC

+----+---+-------+------+
| ID | A | B     | C    |
+----+---+-------+------+
|  1 | x | y     | z    |
|  2 | c | v     | b    |
|  3 | n | n     | m    |
+----+---+-------+------+

Requests : Fields ID , AdID , and Status : 请求 :字段IDAdIDStatus

+----+------+----------+
| ID | AdID |  Status  |
+----+------+----------+
|  3 |    1 | pending  |
|  4 |    2 | approved |
|  5 |    3 | pending  |
+----+------+----------+

ID (from Ads ) = AdID (from Requests ). ID (来自Ads )= AdID (来自Requests )。

Now, I want to get all records from Ads where AdID 's (from Requests ) Status equals pending . 现在,我想从Ads中获取所有记录,其中AdIDStatus (来自Requests )等于pending AdId here would be the value ID from Ads . 这里的AdId将是Ads的值ID

So, with the above tables, the result I'd get would be ID 1 and 3 from Ads: 因此,使用上述表格,我得到的结果将是Ads的ID 1和ID 3:

+----+---+---+---+
| ID | A | B | C |
+----+---+---+---+
|  1 | x | y | z |
|  3 | n | n | m |
+----+---+---+---+

This is the closest I've got so far, but this obviously doesn't work because it can only select one row - whereas I need to select many: 这是我到目前为止所获得的最接近的数据,但这显然行不通,因为它只能选择一行-而我需要选择许多行:

SELECT * FROM Ads WHERE ID = (SELECT AdID FROM Requests WHERE Status = 'pending')

This might not make sense - please ask if I haven't explained it well - I'll help as much as possible :) 这可能没有意义-请问我是否解释得不好-我会尽可能提供帮助:)

Use IN in place of = : IN代替=

SELECT * 
FROM Ads 
WHERE ID IN (SELECT AdID FROM Requests WHERE Status = 'pending')

尝试这个:

SELECT * FROM Ads A LEFT JOIN Requests R ON A.ID=R.AdID WHERE R.AdID='pending';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM