在多表联合查询的时候,遇到复杂的查询逻辑,我们不一定要在查询的sql中来解决,很多时候,灵活运用mybatis的标签,也可以解决我们的参数查询逻辑等;下面是一个查询案例,还有很大的优化空间,这里不做处理,仅供参考;
plate_word pw,
AND a.plate_no LIKE CONCAT('%',#{plateNo,jdbcType=VARCHAR},'%')
AND a.plate_no=pw.plate_no
AND match(pw.plate_word) against(#{plateNo,jdbcType=VARCHAR} IN BOOLEAN MODE)
AND e.plate_no LIKE CONCAT('%',#{plateNo,jdbcType=VARCHAR},'%')
AND e.plate_no=pw.plate_no
AND match(pw.plate_word) against(#{plateNo,jdbcType=VARCHAR} IN BOOLEAN MODE)
SELECT allTh.* FROM(
SELECT @rownum:=@rownum+1 AS rowNum,allTables.*
FROM (SELECT @rownum:=0) r,(
SELECT a.coupon_id, a.coupon_no coupon_code, a.coupon_type coupon_rule_type, a.dealer_id, a.create_time,
a.bgn_date, a.end_date, 11 coupon_mode,
a.`status` coupon_status,
IFNULL(a.deduct_money,0) coupon_use_money,a.plate_no coupon_plate_no,a.shop_id, a.free_time, a.free_rate,
f.`name` dealer_name, c.account coupon_user, c.mobile_phone coupon_phone,a.money
coupon_cut_money,b.source_name
FROM
driver_coupon a
LEFT JOIN open_api_coupon_source b ON b.id= a.openapi_rela_id AND b.dealer_id= #{dealerId,jdbcType=BIGINT}
LEFT JOIN sys_user c ON a.user_id=c.user_id
LEFT JOIN sys_user f ON a.shop_id=f.user_id
WHERE a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.`status` != 99
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.coupon_type = #{couponRuleType,jdbcType=TINYINT}
AND c.mobile_phone =#{mobilePhone,jdbcType=VARCHAR}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
AND b.id =#{sourceId,jdbcType=BIGINT}
UNION ALL
SELECT a.record_id coupon_id, a.coupon_no coupon_code, a.free_type coupon_rule_type, a.dealer_id,
a.create_time,
a.bgn_date, a.end_date, 11 coupon_mode, a.`status` coupon_status,
IFNULL(a.deduct_money,0) coupon_use_money, a.plate_no coupon_plate_no, a.user_id shop_id, null free_time,
CASE WHEN a.free_type=1 THEN a.free_price
ELSE 0
END AS free_rate,
f.`name` dealer_name, a.`name` coupon_user,a.phone coupon_phone,
CASE WHEN a.free_type=0 THEN a.free_price
ELSE 0
END AS coupon_cut_money,'' AS source_name
FROM
visit_car_info a ,dealer_user_rela b
LEFT JOIN sys_user f ON b.user_id=f.user_id
WHERE a.type= 1
AND a.is_delete=0
AND b.dealer_id=a.dealer_id
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND b.user_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND a.phone =#{mobilePhone,jdbcType=VARCHAR}
AND a.user_id = #{operateId,jdbcType=BIGINT}
AND a.type= 99
UNION ALL
SELECT a.coupon_id, a.coupon_code, a.free_type coupon_rule_type,a.dealer_id, a.create_time,
a.bgn_date, a.end_date, 22 coupon_mode, a.`status` coupon_status,
a.deduct_money coupon_use_money, a.plate_no coupon_plate_no, a.shop_id, a.free_time,
a.free_rate, d.`name` dealer_name, '' coupon_user,'' coupon_phone,a.free_price coupon_cut_money,'' AS
source_name
FROM
offline_coupon a
LEFT JOIN sys_user d ON a.shop_id=d.user_id
WHERE a.`status` = 1
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
UNION ALL
SELECT a.coupon_id, a.coupon_code, a.free_type coupon_rule_type, a.dealer_id, a.create_time,
a.bgn_date, a.end_date, 22 coupon_mode, a.`status` coupon_status,
b.deduct_money coupon_use_money, e.plate_no coupon_plate_no, a.shop_id, a.free_time,
a.free_rate, f.`name` dealer_name, d.account coupon_user, d.mobile_phone coupon_phone,a.free_price
coupon_cut_money,'' AS source_name
FROM
charge_coupon_info b
INNER JOIN offline_coupon a on a.coupon_code = b.coupon_code
LEFT JOIN charge_info e ON b.charge_id =e.charge_id
LEFT JOIN sys_user d ON e.user_id=d.user_id
LEFT JOIN sys_user f ON a.shop_id = f.user_id
WHERE a.`status` = 1
AND a.dealer_id = b.dealer_id
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND b.coupon_type=0
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND d.mobile_phone = #{mobilePhone,jdbcType=VARCHAR}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
UNION ALL
SELECT a.coupon_id, a.coupon_code, a.free_type coupon_rule_type, a.dealer_id, a.create_time,
a.bgn_date, a.end_date, 22 coupon_mode, a.`status` coupon_status,
0 coupon_use_money, a.plate_no coupon_plate_no, a.shop_id, a.free_time,
a.free_rate, f.`name` dealer_name, '' coupon_user, '' coupon_phone,a.free_price coupon_cut_money,'' AS
source_name
FROM
offline_coupon a
LEFT JOIN sys_user f ON a.shop_id = f.user_id
WHERE a.`status` != 1
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
UNION ALL
SELECT a.charge_id coupon_id, a.coupon_code, 33 coupon_rule_type, a.dealer_id, a.pay_time create_time,
NULL bgn_date, NULL end_date, 22 coupon_mode, 1 coupon_status,
IFNULL(sum(a.deduct_money), 0) coupon_use_money, a.plate_no coupon_plate_no, null shop_id, null freeTime,
null free_rate, f.`name` dealer_name, '' coupon_user,'' coupon_phone,0 coupon_cut_money,'' AS source_name
FROM
charge_info a,dealer_user_rela i
LEFT JOIN sys_user f ON f.user_id=i.user_id
WHERE a.dealer_id=i.dealer_id
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.coupon_code IS NOT NULL
AND a.coupon_code != ''
AND a.coupon_code != 'charge_coupon_info'
AND NOT EXISTS (SELECT 1 FROM driver_coupon b WHERE a.dealer_id = b.dealer_id AND a.coupon_code =
b.coupon_no)
AND NOT EXISTS (SELECT 1 FROM offline_coupon c WHERE a.dealer_id = c.dealer_id AND a.coupon_code =
c.coupon_code)
AND NOT EXISTS (SELECT 1 FROM visit_car_info d WHERE a.dealer_id = d.dealer_id AND a.coupon_code =
d.coupon_no)
AND a.pay_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
GROUP BY a.coupon_code
)allTables
)allTh
AND rowNum in
#{id,jdbcType=INTEGER}
ORDER BY create_time DESC
SELECT SUM(aa.allCount) AS COUNTALL
FROM (
SELECT COUNT(1) AS allCount
FROM
driver_coupon a
LEFT JOIN open_api_coupon_source b ON b.id= a.openapi_rela_id AND b.dealer_id= #{dealerId,jdbcType=BIGINT}
LEFT JOIN sys_user c ON a.user_id=c.user_id
LEFT JOIN sys_user f ON a.shop_id=f.user_id
WHERE a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.`status` != 99
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.coupon_type = #{couponRuleType,jdbcType=TINYINT}
AND c.mobile_phone =#{mobilePhone,jdbcType=VARCHAR}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
AND b.id =#{sourceId,jdbcType=BIGINT}
UNION ALL
SELECT COUNT(1) AS allCount
FROM
visit_car_info a, dealer_user_rela b
LEFT JOIN sys_user f ON b.user_id=f.user_id
WHERE a.type= 1
AND a.is_delete=0
AND b.dealer_id=a.dealer_id
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND b.user_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND a.phone =#{mobilePhone,jdbcType=VARCHAR}
AND a.user_id = #{operateId,jdbcType=BIGINT}
AND a.type= 99
UNION ALL
SELECT COUNT(1) AS allCount
FROM
offline_coupon a
LEFT JOIN sys_user d ON a.shop_id=d.user_id
WHERE a.`status` = 1
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
UNION ALL
SELECT COUNT(1) AS allCount
FROM
charge_coupon_info b
INNER JOIN offline_coupon a on a.coupon_code = b.coupon_code
LEFT JOIN charge_info e ON b.charge_id =e.charge_id
LEFT JOIN sys_user d ON e.user_id=d.user_id
LEFT JOIN sys_user f ON a.shop_id = f.user_id
WHERE a.`status` = 1
AND a.dealer_id = b.dealer_id
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND b.coupon_type=0
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND d.mobile_phone =#{mobilePhone,jdbcType=VARCHAR}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
UNION ALL
SELECT COUNT(1) AS allCount
FROM
offline_coupon a
LEFT JOIN sys_user f ON a.shop_id = f.user_id
WHERE a.`status` != 1
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.shop_id=#{shopId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.`status` = #{couponStatus,jdbcType=TINYINT}
AND a.free_type = #{couponRuleType,jdbcType=TINYINT}
AND a.operate_id = #{operateId,jdbcType=BIGINT}
UNION ALL
SELECT COUNT(1) AS allCount
FROM (
SELECT COUNT(a.charge_id)
FROM
charge_info a,dealer_user_rela i
LEFT JOIN sys_user f ON f.user_id=i.user_id
WHERE a.dealer_id=i.dealer_id
AND a.dealer_id= #{dealerId,jdbcType=BIGINT}
AND a.coupon_code IS NOT NULL
AND a.coupon_code != ''
AND a.coupon_code != 'charge_coupon_info'
AND NOT EXISTS (SELECT 1 FROM driver_coupon b WHERE a.dealer_id = b.dealer_id AND a.coupon_code
= b.coupon_no)
AND NOT EXISTS (SELECT 1 FROM offline_coupon c WHERE a.dealer_id = c.dealer_id AND a.coupon_code
= c.coupon_code)
AND NOT EXISTS (SELECT 1 FROM visit_car_info d WHERE a.dealer_id = d.dealer_id AND a.coupon_code =
d.coupon_no)
AND a.pay_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
GROUP BY a.coupon_code
) ci
)aa
SELECT count(1) coupon_count, IFNULL(sum(a.free_price),0) deduct_money, b.user_id shop_id, a.`status`
coupon_status, 11 coupon_mode
FROM visit_car_info a, dealer_user_rela b
WHERE a.type= 1
AND a.is_delete=0
AND a.free_type=0
AND a.dealer_id = b.dealer_id
AND a.dealer_id=#{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.free_type=#{couponRuleType,jdbcType=TINYINT}
GROUP BY b.user_id, a.`status`
UNION ALL
SELECT count(1) coupon_count, 0 deduct_money, b.user_id shop_id, a.`status` coupon_status, 11 coupon_mode
FROM visit_car_info a, dealer_user_rela b
WHERE a.type= 1
AND a.is_delete=0
AND a.free_type!=0
AND a.dealer_id = b.dealer_id
AND a.dealer_id=#{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.free_type=#{couponRuleType,jdbcType=TINYINT}
GROUP BY b.user_id, a.`status`
UNION ALL
SELECT IFNULL(SUM(a.rest_count),0) coupon_count, 0 deduct_money, a.shop_id, 2 coupon_status, 11 coupon_mode
FROM qrcode_coupon a
WHERE a.`status`=0
AND a.dealer_id=#{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.coupon_type=#{couponRuleType,jdbcType=TINYINT}
GROUP BY a.shop_id
UNION ALL
SELECT count(1) coupon_count, IFNULL(sum(a.deduct_money),0) deduct_money, a.shop_id,
a.`status` coupon_status, 11 coupon_mode
FROM driver_coupon a
WHERE a.dealer_id=#{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.coupon_type=#{couponRuleType,jdbcType=TINYINT}
GROUP BY a.shop_id, a.`status`
UNION ALL
SELECT count(1) coupon_count, IFNULL(sum(a.deduct_money),0) deduct_money, a.shop_id, a.status coupon_status,
22 coupon_mode
FROM offline_coupon a
WHERE a.dealer_id = #{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.free_type=#{couponRuleType,jdbcType=TINYINT}
GROUP BY a.shop_id, a.status
UNION ALL
SELECT count(1) coupon_count, IFNULL(sum(coupon_use_money),0) deduct_money, '-1' shop_id, 1
coupon_status, 22 coupon_mode
FROM (
SELECT IFNULL(sum(a.deduct_money),0) coupon_use_money
FROM charge_info a
WHERE a.dealer_id = #{dealerId,jdbcType=BIGINT}
AND a.coupon_code IS NOT NULL
AND a.coupon_code != 'charge_coupon_info'
AND a.coupon_code != ''
AND a.pay_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND NOT EXISTS (SELECT 1 FROM driver_coupon b WHERE a.dealer_id = b.dealer_id AND a.coupon_code =
b.coupon_no)
AND NOT EXISTS (SELECT 1 FROM offline_coupon c WHERE a.dealer_id = c.dealer_id AND a.coupon_code =
c.coupon_code)
AND NOT EXISTS (SELECT 1 FROM visit_car_info d WHERE a.dealer_id = d.dealer_id AND a.coupon_code =
d.coupon_no)
GROUP BY a.coupon_code
) ck
select sum(money) money from (
select IFNULL(sum(collect_money),0) money
from qrcode_coupon a
where a.dealer_id=#{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.coupon_type=#{couponRuleType,jdbcType=TINYINT}
union all
select IFNULL(sum(price),0) money
from offline_coupon_accredit a
where a.dealer_id=#{dealerId,jdbcType=BIGINT}
AND a.create_time between #{bgnTime,jdbcType=TIMESTAMP} and #{endTime,jdbcType=TIMESTAMP}
AND a.free_type=#{couponRuleType,jdbcType=TINYINT}
)b
select a.user_id,a.name
from sys_user a,
dealer_user_rela b
where a.is_delete = 0
and a.user_id= b.user_id
and user_type = #{userType,jdbcType=TINYINT}
select a.user_id, a.name
from sys_user a,
dealer_user_rela b
where a.is_delete = 0
and a.user_id= b.user_id
and a.user_id = #{userId,jdbcType=BIGINT}
union
select user_id, name
from sys_user
where is_delete = 0
and parent_user_id = #{parentUserId,jdbcType=BIGINT}
and user_type = #{userType,jdbcType=TINYINT}
and is_merchant = #{isMerchant,jdbcType=TINYINT}
select user_id,name
from sys_user
where is_delete = 0
and user_id = #{userId,jdbcType=BIGINT}