简体   繁体   English

如何使用 SQL 查询与一列值在哪里?

[英]How to use SQL query with a column of values for where?

How can I loop though a column of IDs I created with in a 2nd select query with a WHERE.如何在第二个 select 查询中使用 WHERE 创建一列 ID。

Example table of ids to loop through:要循环的 id 示例表:

1

Here is what I got so far:这是我到目前为止得到的:

WITH myTable  
AS (  
    select myKey as myKey  
    from dbo.table  
    Where column2 = 'x'  
    GROUP BY myKey  
)  
SELECT *  
FROM table2  
WHERE table2.key = myKey

I can't loop through table2 with my column of values.我无法使用我的值列遍历 table2。 How can I do it?我该怎么做?

You can use in :您可以in

SELECT *
FROM table2
WHERE table2.key IN (SELECT mt.myKey FROM myTable mt);

Given that the mykey is unique, you can also use JOIN and EXISTS .鉴于mykey是唯一的,您还可以使用JOINEXISTS

暂无
暂无

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

相关问题 如何使用SQL查询列作为where子句的参数? - How to can I use a SQL query column in as the arguments of a where clause? SQL Server-如何在where子句中使用多个列值 - SQL Server - How to use multiple column values in where clause SQL - 如何在 where 子句中使用逗号分隔的列值 - SQL - How to use Comma separated column values in a where clause PHP,SQL:如何在查询中使用WHERE字段获取多个值? - PHP, SQL: How to use WHERE field in Query to get multiple values? sql 查询如何在一张表中使用同一列计算值? - sql query how to calculate values use same column in one table? Access SQL查询:使两个列具有相同的值,但是如何排除列1 =列2的情况? - Access SQL Query: Making two columns of the same values, but how do you exclude cases where column 1 = column 2? SQL使用分组的值在子查询中使用+“ where in in())” - SQL Use grouped values to use in a subquery + “where column in())” 如何从一行中选择两个列值中的一个,其中一个是您的查询值,另一个是您在 SQL 中想要的值? - How to select 1 of two column values from a row where one is a value your query and the other you want in SQL? 如何使用 Power Query M 将 Excel 表列值提取到 SQL WHERE 子句中? - How do I pull Excel table column values into a SQL WHERE clause using Power Query M? 如何使用限制列作为查询的位置? - How use restrictions column as where of query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM