select c.`stock_id`,a.`stock_name`,a.`coupon_amount`, a.`transaction_minimum`, b.`company_name`, a.`start_time`, a.`end_time`, (select count(1) from user_coupon where `activity_id` = a.`id`) as `grant`,(select count(1) from user_coupon where `activity_id` = a.`id` and (status = "20" or status = "30")) as `receive`,(select count(1) from user_coupon where `activity_id` = a.`id` and status = "30") as `writeOff` from wx_coupon a left join baishiof_company b on a.company_id = b.company_id left join user_coupon c on a.`id` = c.`activity_id` group by c.`stock_id`
时间: 2023-08-03 19:03:16 浏览: 130
这是一个查询语句,根据不同的条件从三个表(wx_coupon, baishiof_company, user_coupon)中获取数据。以下是查询结果的字段:
- c.stock_id: 股票ID
- a.stock_name: 股票名称
- a.coupon_amount: 优惠券金额
- a.transaction_minimum: 交易最低额
- b.company_name: 公司名称
- a.start_time: 活动开始时间
- a.end_time: 活动结束时间
- (select count(1) from user_coupon where activity_id = a.id) as grant: 发放的优惠券数量
- (select count(1) from user_coupon where activity_id = a.id and (status = "20" or status = "30")) as receive: 领取的优惠券数量
- (select count(1) from user_coupon where activity_id = a.id and status = "30") as writeOff: 核销的优惠券数量
查询结果按照 c.stock_id 进行分组。
相关问题
select a.*, b.name activityName, c.name productName, c.member_max memberMax, d.status as complete, group_concat(if(e.face_value is null, "", e.face_value) separator "\n") as rewardAmount, group_concat(if(e.coupon_code is null, "", e.coupon_code ) separator "\n") as couponCode , group_concat(if(e.coupon_id is null, "", e.coupon_id) separator "\n") as caId from marketing_group_tool_group_member a left join marketing_group_tool_group_info d on a.group_id = d.id left join marketing_group_tool_activity b on a.activity_id = b.activity_id left join marketing_group_tool_product_base c on a.product_id = c.product_id left join marketing_group_tool_send_coupon_record e on (a.group_id = e.group_id and a.card_no = e.card_no) left join wx_recommend_organization organization on b.organization_id = organization.id where ((organization.tree_id between 2002000000000000 and 2002999999999999) or (organization.tree_id between 1000000000000000 and 1999999999999999) or (organization.tree_id between 2000000000000000 and 2999999999999999) or (organization.tree_id between 3000000000000000 and 3999999999999999) or (organization.tree_id between 4000000000000000 and 4999999999999999) or (organization.tree_id between 6000000000000000 and 6999999999999999) or (organization.tree_id between 5000000000000000 and 5999999999999999) or (organization.tree_id between 10000000000000000 and 10999999999999999) or (organization.tree_id between 8000000000000000 and 8999999999999999) or (organization.tree_id between 9000000000000000 and 9999999999999999) or (organization.tree_id between 11000000000000000 and 11999999999999999) or (organization.tree_id between 12000000000000000 and 12999999999999999) or (organization.tree_id between 13000000000000000 and 13999999999999999) or (organization.tree_id between 14000000000000000 and 14999999999999999) or (organization.tree_id between 15000000000000000 and 15999999999999999) or (organization.tree_id between 16000000000000000 and 16999999999999999) or (organization.tree_id between 17000000000000000 and 17999999999999999) ) and 1 = 1 group by a.id, a.activity_id , a.group_id , a.product_id , a.activity_referral_code, a.product_referral_code , a.openid , a.unionid , a.nickname , a.head_img , a.mobile , a.is_sub_buy , a.is_sub_progress , a.is_sub_success , a.sort , a.card_no , a.create_time , a.is_received_notice , a.is_received_progress , a.is_received_success , b.name, c.name, c.member_max , d.status order by a.id desc limit 0,20;
这是一个比较复杂的 SQL 查询语句,有很多连表和条件。要想对其进行优化,需要结合具体的环境和数据情况进行分析。
这里提供一些常见的 SQL 优化建议:
1. 确保表上有合适的索引,包括主键、唯一键和常用查询条件的索引,可以通过 `explain` 命令来查看查询计划和索引使用情况。
2. 避免使用 `select *`,只选择需要的字段,可以减少数据量和网络传输开销。
3. 尽量避免使用 `group_concat`,这会导致 MySQL 在内部生成临时表,会消耗大量的内存和 CPU 时间,可以考虑在应用层进行拼接。
4. 将复杂查询拆分成多个简单查询,每个查询只涉及少量表和字段,可以利用缓存和减轻数据库负担。
5. 对于复杂的查询条件,可以考虑使用全文索引、分词和搜索引擎等技术,以提高查询效率和准确性。
总之,SQL 优化需要根据具体情况进行分析和实践,需要综合考虑数据量、表结构、索引设计、查询条件等多个因素。
SELECT sum(t.pay_amount)+sum(t.promotion_amount)+SUM(t.author_coupon_subsidy)+SUM(t.actual_zt_pay_promotion)+SUM(t.actual_zr_pay_promotion)+SUM(t.refund_amount)+SUM(t.jylp_wp_amount)+SUM(t.post_amount)+SUM(t.promotion_amount_yf) as 'b_amount',\n" + "SUM(t.platform_service_fee)+SUM(t.commission)+SUM(t.channel_fee)+SUM(t.colonel_service_fee)+SUM(t.channel_promotion_fee)+SUM(t.other_sharing_amount)+SUM(t.yfx_amount)+SUM(t.jylp_yp_amount) as 's_amount'\n" + "from dy_settlement_push_input t where t.deleted = '0'
这是一条 SQL 查询语句,目的是查询 dy_settlement_push_input 表中满足条件(deleted = '0')的记录中,各种金额字段的总和。具体来说,该语句计算了以下金额:
1. pay_amount:支付金额
2. promotion_amount:促销优惠金额
3. author_coupon_subsidy:优惠券补贴金额
4. actual_zt_pay_promotion:直通车支付优惠金额
5. actual_zr_pay_promotion:直冲支付优惠金额
6. refund_amount:退款金额
7. jylp_wp_amount:加油礼品卡支付金额
8. post_amount:邮费金额
9. promotion_amount_yf:促销优惠金额(应付)
10. platform_service_fee:平台服务费
11. commission:佣金
12. channel_fee:渠道费用
13. colonel_service_fee:团长服务费
14. channel_promotion_fee:渠道促销费用
15. other_sharing_amount:其他分摊金额
16. yfx_amount:邮费险金额
17. jylp_yp_amount:加油礼品卡优惠金额
最终会返回两个金额总和,分别是 b_amount 和 s_amount。
阅读全文