从数据库中的不同表中获取信息

时间:2010-08-18 16:16:04

标签: sql-server-2005 tsql

我有两张桌子:

       User
---------------------
id        primary key 
name      varchar 
mobile    varchar

示例数据(1, airtel, '9887456321,6985326598,88523695874')

         Client
---------------------------
id              primary key
clientname      varchar 
mobileno        varchar 

示例数据(1,John Doe, 9887456321)

我想根据移动字段值从第一个表中搜索客户端名称。

如果移动字段的值为(9887456321,6985326598,88523695874),那么我如何知道特定手机号码属于哪个客户名称?

1 个答案:

答案 0 :(得分:1)

您的client表与另一个表之间必须有匹配的列。从您的描述中不清楚该列是什么。从你所说的,我相信你的架构看起来像:

Create Table MobileNumbers
(
    Id ... not null Primary Key
    , Name varchar(??) not null
    , Mobile varchar(??) not null
)

Create Table Client
(
    Id ... not null Primary Key
    , ClientName varchar(??) not null
    , MobileNo varchar(??) not null
)

假设Client.Mobile与MobileNumbers.Name匹配,我们有:

Select ...
From Client
    Join MobileNumbers
        On MobileNumbers.MobileNo = Client.Mobile
Where Client.Mobile In('9887456321','6985326598','88523695874')

在这个结构中,我假设每个Mobile值包含一个数字而不是逗号分隔的数字列表。如果实际情况是单个单元格可以包含多个值(请编辑您的原始帖子就是这种情况),那么正如Martin Smith所提到的,您需要规范化数据。

修改

鉴于您对OP的修订,根本问题是您在单个列中有多个值。正确的解决方案是将手机号码规范化为第二个表格:

Create Table UserMobileNumber
(
    UserId ... not null References User ( Id )
    , Carrier varchar(??) not null
    , Mobile varchar(??) not null
    , Constraint UC_UserMobileNumber Unique ( Mobile )
)

然后解决方案变得微不足道了:

Select ...
From User
    Join UserMobileNumber
        On UserMobileNumber.UserId = User.Id
Where UserMobileNumber.Mobile In('9887456321','6985326598','88523695874')

但是,在此期间,您需要的是分割功能:

Create Function dbo.udf_Split
(   
    @DelimitedList nvarchar(max)
    , @Delimiter nvarchar(2) = ','
)
Returns Table
As
Return
    (
    With CorrectedList As
        (
        Select Case When Left(@DelimitedList, Len(@Delimiter)) <> @Delimiter Then @Delimiter Else '' End
            + @DelimitedList
            + Case When Right(@DelimitedList, Len(@Delimiter)) <> @Delimiter Then @Delimiter Else '' End
            As List
            , Len(@Delimiter) As DelimiterLen
        )
        , Numbers As 
        (
        Select TOP (Len(@DelimitedList) + 2) Row_Number() Over ( Order By c1.object_id ) As Value
        From sys.columns As c1
            Cross Join sys.columns As c2
        )
    Select CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen As Position
        , Substring (
                    CL.List
                    , CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen     
                    , CharIndex(@Delimiter, CL.list, N.Value + 1)                           
                        - ( CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen ) 
                    ) As Value
    From CorrectedList As CL
        Cross Join Numbers As N
    Where N.Value < Len(CL.List)
        And Substring(CL.List, N.Value, CL.DelimiterLen) = @Delimiter
    )

现在您可以执行以下操作:

Select ...
From User
    Outer Apply dbo.Split( User.Mobile ) As M
    Join Client
        On Client.MobileNo = M.Value
Where M.Value In('9887456321','6985326598','88523695874')