从多个表中的电话号码获取姓名?

发布于 2024-11-02 02:51:43 字数 557 浏览 0 评论 0原文

我有这个表:

inbox table
SenderNumber Message

contact table
Name Number

district table
id SpvName Number

sub_district table
id district_id SpvName Number

village table
id sub_district_id SpvName Number

我想从收件箱的 SenderNumber 列中获取联系人、区、分区或村庄的姓名或 spvname。我该如何实现这一目标?结果可能是这样的,

SenderNumber | Name | Type         | Message  
-------------+------+--------------+------------
123          | john | contact      | bla bla  
234          | mary | district spv | bla bla bla  

提前致谢,并对我的英语不好表示歉意。

i have this tables:

inbox table
SenderNumber Message

contact table
Name Number

district table
id SpvName Number

sub_district table
id district_id SpvName Number

village table
id sub_district_id SpvName Number

i want to get name or spvname from contact, district, sub_district or village, based from the SenderNumber column from inbox. How do i achieve this? the result maybe like this

SenderNumber | Name | Type         | Message  
-------------+------+--------------+------------
123          | john | contact      | bla bla  
234          | mary | district spv | bla bla bla  

thanks in advance, and sorry for my bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请止步禁区 2024-11-09 02:51:43
SELECT i.SenderNumber,
       COALESCE(c.Name, d.SpvName, sd.SpvName, v.SpvName) as Name,
       CASE WHEN c.Name IS NOT NULL THEN 'contact'
            WHEN d.SpvName IS NOT NULL THEN 'district'
            WHEN sd.SpvName IS NOT NULL THEN 'sub_district'
            WHEN v.SpvName IS NOT NULL THEN 'village'  
            ELSE ''
       END AS Type,
       i.Message
    FROM inbox i
        LEFT JOIN contact c
            ON i.SenderNumber = c.Number
        LEFT JOIN district d
            ON i.SenderNumber = d.Number
        LEFT JOIN sub_district sd
            ON i.SenderNumber = sd.Number   
        LEFT JOIN village v
            ON i.SenderNumber = v.Number   
SELECT i.SenderNumber,
       COALESCE(c.Name, d.SpvName, sd.SpvName, v.SpvName) as Name,
       CASE WHEN c.Name IS NOT NULL THEN 'contact'
            WHEN d.SpvName IS NOT NULL THEN 'district'
            WHEN sd.SpvName IS NOT NULL THEN 'sub_district'
            WHEN v.SpvName IS NOT NULL THEN 'village'  
            ELSE ''
       END AS Type,
       i.Message
    FROM inbox i
        LEFT JOIN contact c
            ON i.SenderNumber = c.Number
        LEFT JOIN district d
            ON i.SenderNumber = d.Number
        LEFT JOIN sub_district sd
            ON i.SenderNumber = sd.Number   
        LEFT JOIN village v
            ON i.SenderNumber = v.Number   
往日 2024-11-09 02:51:43

我相信这样的事情会起作用......

SELECT i.SenderNumber, a.Name, a.Type, i.Message FROM
inbox i
INNER JOIN
(SELECT SpvName as Name, 'contact' as Type, Number FROM contact
 UNION
 SELECT SpvName as Name, 'sub district' as Type, Number FROM sub_district
 UNION
 SELECT SpvName as Name, 'district' as Type, Number FROM district
 UNION
 SELECT SpvName as Name, 'village' as Type, Number FROM village
 ) a
ON i.SenderNumber = a.Number

Something like this would work I believe...

SELECT i.SenderNumber, a.Name, a.Type, i.Message FROM
inbox i
INNER JOIN
(SELECT SpvName as Name, 'contact' as Type, Number FROM contact
 UNION
 SELECT SpvName as Name, 'sub district' as Type, Number FROM sub_district
 UNION
 SELECT SpvName as Name, 'district' as Type, Number FROM district
 UNION
 SELECT SpvName as Name, 'village' as Type, Number FROM village
 ) a
ON i.SenderNumber = a.Number
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文