简体   繁体   中英

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:

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.

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 . 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.

try this , you don't need to use where

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

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