简体   繁体   中英

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.

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.

The simplest way to join them is with the IN operator:

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版本中可用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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