dongmin3754 2017-03-28 12:54
浏览 68
已采纳

从clumon“”中选择结果并加入另一个表

Client

ClientID    UserID  CustomerID
1   1   1
2   2   2
3   3   3
4   4   4
5   5   5

client service:

    ClientServiceID   ClientID  ServiceID   Description

    28  10  6           Premium master "multaps1.com"

    48  134 6           Premium master "multaps3.com"

    58  135 6           Premium master "multaps4.com"

    68  137 6           Premium master "multaps5.com"

        366 273 1           Webhosting master "multaps10.com"

HostingAccount:

    HostingAccountID     Domain DomainIP      Username  Email

   2        multaps1.com         7    Bert    Bert@in.com
    3       multaps2.com         9    Bernard     nard.@Sedn.ca
    4       multaps3.com         3    Bree       Aenean@Cum.org
    5       explts.com       1    exploi   ante@cursusluctus.edu
    6       multaps5.com    10    Rama   quis@tempusrisusuk

Service

ServiceID  ServiceType
1     1
2     1
3     1
4     2

I have the folowing tables I need to get results like this

---------------------------------------------------------------------------
|CustomerID| Description               | Domain      | UserName | Email   |
---------------------------------------------------------------------------
|  1   | Premium master "multaps1.com" | multaps1.com| Bert| Bert@in.com  |
---------------------------------------------------------------------------
|  2   | Premium master "multaps2.com" | multaps2.com| Bernard| nard@Sedn |
---------------------------------------------------------------------------
|  2   | Premium master "multaps3.com" | multaps3.com| Bree   | Aan@C.org |
---------------------------------------------------------------------------

I am trying like this:

SELECT DISTINCT C.CustomerID,
                               CS.Description,
                               HA.Email,
                               HA.Domain,
                               HA.UserNam
                           FROM ClientService AS CS
                           INNER JOIN Client AS C ON C.ClientID=CS.ClientID
                           LEFT JOIN Service As S
                           ON CS.ServiceID = S.ServiceID
                           LEFT JOIN HostingAccount AS HA
                           On HA.Domain = ANY (SELECT DISTINCT SUBSTR(Description, LOCATE( '"', Description )+1,
                               ( CHAR_LENGTH( Description) - LOCATE('"',REVERSE(`Description`) ) -
                               LOCATE('"',Description)))
                                   from ClientService
                                   )
                                   WHERE S.ServiceType IN (1, 2, 3)
                           ORDER BY HA.HostingAccountID DESC

I keep getting multiple time same Domain, username, email and it matches with other rows like this:

---------------------------------------------------------------------------
    |CustomerID| Description               | Domain      | UserName | Email   |
    ---------------------------------------------------------------------------
    |  1   | Premium master "multaps1.com" | multaps1.com| Bert| Bert@in.com  |
    ---------------------------------------------------------------------------
    |  2   | Premium master "multaps2.com" | multaps1.com| Bert| Bert@in.com|
    ---------------------------------------------------------------------------
    |  2   | Premium master "multaps3.com" | multaps1.com| Bert   | Bert@in.com|
    -------------------------------------------------------------------------

It does this also for other rows to. the problem I need to get description between "" and then get same result as domain. that is the only way they are related and I cant change tables indexes. I don't know how to explain any help will be appreciated.

  • 写回答

1条回答 默认 最新

  • dongqie2355 2017-03-28 13:26
    关注

    Your JOIN condition seems overcomplicated. Try this instead:

    LEFT JOIN HostingAccount AS HA ON  CS.Description LIKE CONCAT('%', HA.Domain, '%') 
    

    This will return a match as soon as Domain is contained within the Description.

    Demo here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面