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

时间:2017-03-28 12:54:01

标签: php mysql sql

客户端

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

客户服务:

    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

服务

ServiceID  ServiceType
1     1
2     1
3     1
4     2

我有下表 我需要得到这样的结果

---------------------------------------------------------------------------
|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 |
---------------------------------------------------------------------------

我这样想:

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

我不断获得多次相同的域名,用户名,电子邮件,并且与其他行匹配:

---------------------------------------------------------------------------
    |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|
    -------------------------------------------------------------------------

它也适用于其他行。我需要在“”之间获得描述然后得到与域相同的结果的问题。这是他们相关的唯一方式,我无法更改表索引。我不知道如何解释任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的JOIN条件似乎过于复杂。试试这个:

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

只要Domain中包含Description,就会返回匹配。

Demo here