SQL中的customer_id(xx)

时间:2013-11-04 10:35:06

标签: sql sql-server sql-server-2012

可以真正使用你的帮助!

我有一个问题:

Select *
from Customers
Where Customer_id in (001,002,003)

......你明白了。

我的问题是,如果没有例如customer_id 003的记录,则不会显示任何记录。 如何显示“003”并说明没有找到记录?我更喜欢这个,而不是根本没有记录显示。

提前感谢你!

5 个答案:

答案 0 :(得分:1)

在SQL Server 2008+中,您可以使用

SELECT V.Customer_id,
       C.*
FROM   (VALUES(1),
              (2),
              (3)) V(Customer_id)
       LEFT JOIN Customers C
         ON C.Customer_id = V.Customer_id 

非匹配值将在NULL列中显示C.Customer_id

答案 1 :(得分:1)

通常对于这种事情,我有一个函数,它接受一个csv并为我返回一个表。 然后我做了类似下面的事情。

-- this is creating the temporary table which would normally be created by a function.
DECLARE @Temp TABLE (Customer_id int)
INSERT INTO @Temp(Customer_id)
SELECT 1
INSERT INTO @Temp(Customer_id)
SELECT 2
INSERT INTO @Temp(Customer_id)
SELECT 3

-- now do the select statement.
SELECT 
    T.Customer_id,
     C.*
FROM
    Customers C
RIGHT OUTER JOIN
     @Temp T
ON
     T.Customer_id = C.Customer_id

这将为您提供结果,如果C.Customer_idNULL,那么Id Customer中不存在table。 这与@MartinSmith的工作完全相同,但大多数SQL服务器版本都支持它。

答案 2 :(得分:0)

在oracle中你可以这样做:

with vals as
 (SELECT '001' as val
    from dual
  union
  select '002'
    from dual
  union
  select '030' from dual)
select v.val, nvl(c.Customer_id, 'no Customer found') from vals v left join Customers c on c.Customer_id = v.val

答案 3 :(得分:0)

您需要做的是查询要查找的客户ID列表(包括可能不存在的客户ID),然后将其与您的customers表一起加入。创建要查询的客户ID临时表是特定于平台的,但以下是我在Oracle中的操作方法:

with customersToLookup as (
  select '001' as customer_id from dual union all
  select '002' as customer_id from dual union all
  select '003' as customer_id from dual
)
select    customersToLookup.customer_id,
          customers.*
from      customersToLookup
left join customers
on        customers.customer_id = customersToLookup.customer_id;

根据我的经验,运行这样的查询是非常不寻常的:RDBM不应该以这种方式使用。

答案 4 :(得分:0)

可能不是最好的解决方案,因为复杂性,显然不建议用于有很多行的表,但是..我创建了2个新的临时表:#tempTab2(我保存了已经发送的id)和#tempTabFinal (如果ID存在,我保存了与该ID相对应的行,如果没有,我保存了“未找到的消息”。)这是一个打印屏幕,看看这是你要做什么,以及代码(我用于测试'questions'表,其中包含以下列:q_id和question - 实际问题) enter image description here

DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '1,153,154,1555,111,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab2 (id int not null,question varchar(60))
CREATE TABLE #tempTabFinal(id varchar(40) not null,question varchar(60))

WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0 
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO #tempTab2 (id,question) VALUES (@VALUE,'question')
END

DECLARE @INDEX INT
SET @INDEX = 1

WHILE (@INDEX <=5)
BEGIN
    IF((Select COUNT(*) FROM questions Where q_id=  (SELECT TOP 1 id from #tempTab2)) >0   )
    BEGIN
        DECLARE @TEMP as varchar (80)
        SET @TEMP = (Select question from questions where q_id = (SELECT TOP 1 id from #tempTab2))
        DECLARE @ID INT 
        SET @ID = (SELECT TOP 1 id from #tempTab2)

        Insert into #tempTabFinal Values(@ID,@Temp)

    END
    ELSE    
    BEGIN
        DECLARE @NOTFND as Varchar(20)
        SET @NOTFND = (SELECT TOP 1 id from #tempTab2)
        Insert into #tempTabFinal Values('id ' +@NOTFND + ' Not found ','Not found')
    END

    DELETE TOP (1) from #tempTab2
    SET @INDEX = @INDEX + 1
END

Select * from #tempTabFinal
DROP TABLE #tempTab2
DROP TABLE #tempTabFinal