qq_21075683 于 2016.03.15 16:59 提问

sql语句优化,很着急，在线等，sql如下

SELECT
t1.user_id AS user_id,
t1.user_name AS user_name,
t1.serv_num AS serv_num,
t1.create_date AS create_date,
t1.connect_type AS connect_type,
(
SELECT
timestampdiff(DAY, t1.create_date, now())
) AS reg_day,
ifnull(
count(DISTINCT c.order_no),
0
ifnull(sum(c.price * c.num), 0) AS buy_mon,
ifnull(
count(DISTINCT s.order_no),
0
) AS def_orders,
ifnull(sum(s.refund_price), 0) AS def_mon,
ifnull(count(shopc.cart_id), 0) AS cart_flag,
ar.area_name AS area_name
FROM
g_customer t1
LEFT JOIN g_serial_records t ON t1.user_id = t.user_id
AND pay_time IS NOT NULL
AND pay_time != '1900-01-01 00:00:00'
LEFT JOIN g_consume_records c ON c.user_id = t1.user_id
AND c.order_no = t.order_no
LEFT JOIN g_refund_records s ON t1.user_id = s.user_id
AND (s.state != '6')
AND t.order_no = s.order_no
AND s.product_id = c.product_id
AND s.sku_no = c.sku_no
LEFT JOIN (
SELECT
t2.user_id AS user_id,
count(t2.user_id) AS totalcount
FROM
(
SELECT
user_id,
date_format(create_date, '%y%m%d')
FROM
g_serial_records
WHERE
pay_time > '1900-01-01 00:00:00'
GROUP BY
date_format(create_date, '%y%m%d'),
user_id
) t2
GROUP BY
t2.user_id
) t2 ON t.user_id = t2.user_id
LEFT JOIN g_shopping_cart shopc ON shopc.user_id = t1.user_id
LEFT JOIN (
SELECT
FROM
(
SELECT
min(newa.create_date),
newa.user_id,
newa.area_name
FROM
GROUP BY
newa.user_id
GROUP BY
) ar ON ar.user_id = t1.user_id
GROUP BY
t1.user_id

1个回答

qq_21075683   2016.03.15 17:04