[英]SQL find rows in table based on search results in another table
I'm looking for a way to search a table values from the results of a query on another table : 我正在寻找一种方法,可以从另一个表的查询结果中搜索表值:
SELECT entry_id FROM FEEDENTRYSTATUSES WHERE starred > 0 ;
+----------+
| entry_id |
+----------+
| 1036 |
| 1059 |
+----------+
2 rows in set (0.00 sec)
SELECT url from FEEDENTRIES WHERE id = 1036 ;
+---------------------+
| url |
+---------------------+
| https://google.com/ |
+---------------------+
1 row in set (0.00 sec)
So I can get a list of IDs from the FEEDENTRYSTATUSES table. 因此,我可以从FEEDENTRYSTATUSES表中获取ID列表。 I can retrieve value from second table manually by giving the value 1036 .
我可以通过给值1036手动从第二个表中检索值。 But I would like to search in table FEEDENTRIES from values returned by first SELECT.
但是我想从第一个SELECT返回的值中搜索表FEEDENTRIES。
Is here a way to do so ? 这里有办法吗? Any help very much appreciated.
任何帮助,非常感谢。 Thanks a lot
非常感谢
You can use IN
operator and subquery to achieve that: 您可以使用
IN
运算符和子查询来实现:
SELECT url from FEEDENTRIES
WHERE id IN (SELECT entry_id FROM FEEDENTRYSTATUSES WHERE starred > 0);
Or join the tables and filter the data that you need: 或联接表并过滤所需的数据:
SELECT fe.url from FEEDENTRIES fe
INNER JOIN FEEDENTRYSTATUSES fes ON fe.id = fes.entry_id
WHERE fes.starred > 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.