我试图将表与LEFT JOIN和SUBSTR LOCATE组合在一起。其中一个表(ClientService)有一个名为description的列,我需要的记录位于“”之间 例如:这是一个测试“示例”。我需要示例与我的其他表(HostingAccount)进行比较
这是我的查询。
SELECT
CS.ClientServiceID,
S.ServiceID,
CS.InvoicePeriod,
CS.Period,
S.Price,
S.ServiceCategoryID,
IF(LENGTH(CS.Description) > 0, CS.Description, S.Description) AS Description,
CS.Discount,
DATE_FORMAT(CS.StartDate, "%d-%m-%Y") AS StartDate,
'.($Timestamp === TRUE ? 'UNIX_TIMESTAMP(CS.PayedUntill)' : 'DATE_FORMAT(IFNULL(CS.PayedUntill, DATE_ADD(CS.StartDate, INTERVAL 1 YEAR)), "%d-%m-%Y")').' AS PayedUntill,
DATEDIFF(NOW(), CS.PayedUntill) AS PayedUntillDifference,
CS.ReferenceID,
CS.External,
CS.Redirect,
CS.RedirectType,
CS.Active,
H.HostingID,
H.ServerIP,
H.Username
FROM ClientService AS CS
JOIN Client AS C
ON C.ClientID = CS.ClientID
JOIN Service AS S
ON S.ServiceID = CS.ServiceID
LEFT JOIN Hosting AS H
ON H.HostingID = CS.ReferenceID
AND H.ClientID = C.ClientID
---> LEFT JOIN HostingAccount AS HA
ON HA. <----
WHERE CS.ClientServiceID = :CSID
AND C.ClientID = :CID
AND CS.Active IN (1,5,6,9)
我用---&gt;标记了LEFT JOIN HostingAccount &lt; ---并且其中没有“”
的示例描述我已经测试了这个查询,它给了我没有表ClientService
中的“”的记录SELECT SUBSTR(Description, LOCATE('"',Description)+1,
(CHAR_LENGTH(Description) - LOCATE('"',REVERSE(Description)) -
LOCATE('"',Description))) from ClientService
问题:
如何将SUBSTR查询与LEFT JOIN HostingAccount AS HA结合使用 ON HA.description(没有分隔符)= CS.description(在分隔符之间)
答案 0 :(得分:0)
我没有测试过以下内容,我不是100%确定我是按照这个问题进行的,但我认为你的意思是这样的?
<?php
$sql='SELECT
CS.`ClientServiceID`,
S.`ServiceID`,
CS.`InvoicePeriod`,
CS.`Period`,
S.`Price`,
S.`ServiceCategoryID`,
IF( LENGTH( CS.`Description` ) > 0, CS.`Description`, S.`Description`) AS "Description",
CS.`Discount`,
DATE_FORMAT(CS.`StartDate`, "%d-%m-%Y") AS "StartDate",
' .( $Timestamp === TRUE ? 'UNIX_TIMESTAMP( CS.`PayedUntill` )' : 'DATE_FORMAT(IFNULL(CS.`PayedUntill`, DATE_ADD(CS.`StartDate`, INTERVAL 1 YEAR)), "%d-%m-%Y")' ) . ' AS "PayedUntill",
DATEDIFF(NOW(), CS.`PayedUntill`) AS "PayedUntillDifference",
CS.`ReferenceID`,
CS.`External`,
CS.`Redirect`,
CS.`RedirectType`,
CS.`Active`,
H.`HostingID`,
H.`ServerIP`,
H.`Username`
FROM `ClientService` CS
JOIN `Client` C ON C.ClientID = CS.ClientID
JOIN `Service` S ON S.ServiceID = CS.ServiceID
LEFT JOIN `Hosting` H ON H.HostingID = CS.ReferenceID AND H.ClientID = C.ClientID
LEFT JOIN `HostingAccount` HA ON HA.`description`=(
SELECT SUBSTR(`Description`, LOCATE( \'"\', `Description` )+1,
( CHAR_LENGTH( `Description`) - LOCATE(\'"\',REVERSE(`Description`) ) -
LOCATE(\'"\',Description)))
from `ClientService`
)
WHERE CS.`ClientServiceID` = :CSID
AND C.`ClientID` = :CID
AND CS.`Active` IN (1,5,6,9)';
?>