mysql查询时需要连接多个表时,比如查询订单的商品表,需要查询商品的其他信息,其他信息不在订单的商品表,需要连接其他库的表,但是连接的条件基本都是商品ID就可以了,先给一个错误语句(查询之间的嵌套,效率很低):

SELECT
    A.order_id,
    A.wid,
    A.work_name,
    A.supply_price,
    A.sell_price,
    A.total_num,
    A.sell_profit,
    A.sell_percent,
    A.goods_id,
    A.goods_name,
    A.classify,
    B.gb_name
FROM
    (
        SELECT
            A.sub_order_id AS order_id,
            A.photo_id AS wid,
            A.photo_name AS work_name,
            A.supply_price,
            A.sell_price,
            sum(A.num) AS total_num,
            (
                A.sell_price - A.supply_price
            ) AS sell_profit,
            (
                A.sell_price - A.supply_price
            ) / A.sell_price AS sell_percent,
            A.goods_id,
            A.goods_name,
            B.goods_name AS classify
        FROM
            order_goods AS A
        LEFT JOIN (
            SELECT
                A.goods_id,
                A.parentid,
                B.goods_name
            FROM
                test_qyg_goods.goods AS A
            LEFT JOIN test_qyg_goods.goods AS B ON A.parentid = B.goods_id
        ) AS B ON A.goods_id = B.goods_id
        WHERE
            A.createtime >= '2016-09-09 00:00:00'
        AND A.createtime = '2016-09-09 00:00:00'
AND A.createtime
最后修改:2024 年 07 月 18 日
如果觉得我的文章对你有用,请随意赞赏