1

I have two SQL queries, one that uses the other after getting its result.

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.

4

3 回答 3

4

加入它们的最简单方法是使用IN操作员:

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

但是还有很多其他的...

例如,您可以加入他们:

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

这些可能会产生不同的输出,但这取决于您的数据结构。

于 2012-08-09T16:48:16.313 回答
2
SELECT m.data 
FROM mods m
INNER JOIN users u ON m.data2 = u.data
WHERE u.data2 = value
于 2012-08-09T16:48:19.290 回答
0

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

于 2012-08-09T16:56:21.877 回答