简体   繁体   English

从一个表中选择所有列,从另一个表中选择一个列,其中列等于变量

[英]Select all columns from one table and one from another where column equals variable

Sorry for the long title. 抱歉,标题太长了。

I have a statement which needs to grab all the columns from one row from BinConfig: 我有一条语句需要从BinConfig的一行中获取所有列:

SELECT * 
FROM BinConfig WITH(NOLOCK) 
WHERE IssuerKey = @IssuerKey

But I also need to grab a single column from one row from CardRangeGroup also based on that IssuerKey column. 但是我还需要根据该IssuerKey列从CardRangeGroup一行中获取一列。

What I've tried: 我尝试过的

SELECT 
    BinConfig.*, CardRangeGroup.Name 
FROM 
    BinConfig 
JOIN 
    CardRangeGroup WITH(NOLOCK) 
WHERE 
    @IssuerKey = BinConfig.IssuerKey 
    AND @IssuerKey = CardRangeGroup.IssuerKey

Which gives me a syntax error near WHERE . 这给了我WHERE附近的语法错误。 I've tried to find resources online, but everywhere I look I can't find anything explaining how to select rows based on a passed in variable. 我尝试过在线查找资源,但是到处都找不到任何解释如何根据传入变量选择行的内容。 Any help? 有什么帮助吗?

You need to specify how the tables should be joined. 您需要指定如何连接表。 Try this: 尝试这个:

SELECT BinConfig.*, CardRangeGroup.Name 
FROM BinConfig 
JOIN CardRangeGroup ON BinConfig.IssuerKey = CardRangeGroup.IssuerKey
WHERE @IssuerKey = CardRangeGroup.IssuerKey

The with(nolock) might not be needed (or a good idea) so I removed it. 可能不需要with(nolock) (或一个好主意),所以我删除了它。

try this , you don't need to use where 试试这个,你不需要在where使用

 SELECT BinConfig.*, CardRangeGroup.Name FROM BinConfig JOIN   
 CardRangeGroup
 ON CardRangeGroup.IssuerKey = BinConfig.IssuerKey AND @IssuerKey = CardRangeGroup.IssuerKey

暂无
暂无

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

相关问题 SQL-从表中选择列,其中另一个表的列值等于列名列表 - SQL - Select Columns from table where another table column value equals column name list Select 一个表的所有列和另一个表的一些列 - Select all columns from one table and some from another table SQL选择一个表中的所有列和另一个表上另一列的最大值 - SQL Select all columns from one table and Max value of another column on another table mysql-从一个表中选择所有内容,并在其中找到$ var的另一列中选择所有内容 - mysql - Select all from one table and one column form another where $var is found 从一个表中选择所有列,其中某些列与另一个选择匹配 - Select all from one table where some columns match another select Select 基于两个不同 where 子句/连接的表中的列 - 如果一个列匹配,则使用该列,如果不匹配则使用另一个 - Select columns from a table based on two different where clauses / joins - if one column matches use that, if not then use another 在表中选择all表,其中表1中的列存在于另一个表中,第二列等于第二个表中的变量 - Select all in table where a column in table 1 exists in another table and a second column equals a variable in second table 从一个表中选择列,在另一个表中为NULL - Select column from one table, where it is NULL in another SQL 选择其中一个参数等于另一个表中的另一个参数 - SQL select where one parameter equals to another one in another table 如何将一列连接到另一个表中的所有列 - how to join one column to all columns from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM