简体   繁体   English

SQL:获取查询结果并另外使用-结合

[英]SQL: Taking the result of of query and using it another - combine

I have two SQL queries, one that uses the other after getting its result. 我有两个SQL查询,一个在获取结果后使用另一个。

SELECT users.data from users WHERE users.data2 = value

SELECT mods.data FROM mods WHERE mods.data2 = (result of previous query)

How would I combine these statements? 我将如何结合这些陈述? There are similar questions as such, but I'm new to SQL and don't understand them. 也有类似的问题,但是我是SQL的新手,不了解它们。

The simplest way to join them is with the IN operator: 加入它们的最简单方法是使用IN运算符:

SELECT mods.data FROM mods WHERE mods.data2 IN 
    (SELECT users.data from users WHERE users.data2 = value)

but there are plenty of others... 但是还有很多其他的...

For example you could join them: 例如,您可以加入他们:

SELECT mods.data 
FROM mods 
JOIN users ON mods.data2 = users.data
WHERE users.data2 = value

These may produce different outputs though depending on how your data is structured. 尽管这取决于您的数据结构,但它们可能会产生不同的输出。

SELECT m.data 
FROM mods m
INNER JOIN users u ON m.data2 = u.data
WHERE u.data2 = value

根据确切的RDBMS,您可以使用公共表表达式 (CTE),在最新的MS SQL版本中可用。

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

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