mysql中多個left join子查詢寫法以及別名用法

LHBlog發表於2017-10-30
不多說 直接上語句  
SELECT
    a.id,
    a.thumbNail,
    a. NAME,
    a.marketPrice,
    a.memberPrice,
    a.personName,
    a. STATUS,
    a.recieveOrderDate,
    a.trackNumber,
    a.contact,
    a.reportSendDate,
    b.trackNumber,
    a.reportDownloadPath
FROM
    (
        SELECT
            od.id,
            ps.thumbNail,
            ps. NAME,
            od.marketPrice,
            od.memberPrice,
            od.personName,
            od. STATUS,
            od.recieveOrderDate,
            ol.trackNumber,
            ol.contact,
            od.reportSendDate,
            od.reportSendOrderLogisticId,
            od.reportDownloadPath
        FROM
            orders.order_detail od
        LEFT JOIN orders.order_logistics ol ON od.recieveOrderLogisticId = ol.id
        LEFT JOIN orders.product_snapshot ps ON od.productSnapShotId = ps.id
        WHERE
            od.valid = TRUE
        AND ol.valid = TRUE
        AND od.orderId =?
    ) a
LEFT JOIN (
    SELECT
        ol.trackNumber,
        od.id
    FROM
        orders.order_detail od
    LEFT JOIN orders.order_logistics ol ON od.reportSendOrderLogisticId = ol.id
    WHERE
        od.valid = TRUE
    AND ol.valid = TRUE
    AND od.orderId =?
) b ON a.id = b.id
 
 
ps:解釋下 對於Mysql中的語句,牽扯到左查詢時  可以用left join關聯  但一般對於不知道什麼時候起別名 什麼時候用括號的老鐵們 真實頭疼  所以發個模板 剩下的根據自己的業務具體實施。

相關文章