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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器