SELECT q.c_id, q.c_userid, q.c_title, q.c_createtime, q.c_isprivate, q.c_acceptid, q.c_deleted, count(t.c_id) answerNum FROM t_ext_question q left join t_comment t on t.c_pid = q.c_id where q.c_deleted = 0 AND lower( q.c_title ) LIKE '%123%' AND q.c_userid = 'ADMIN' AND DATE_FORMAT( q.c_createtime, '%Y-%m-%d' ) BETWEEN '2023-05-01' AND '2023-05-14' GROUP BY q.c_id, q.c_userid, q.c_title, q.c_createtime, q.c_isprivate, q.c_acceptid, q.c_deleted HAVING count(t.c_id)=0 ORDER BY c_createtime desc
时间: 2023-06-20 20:09:41 浏览: 108
这是一个 SQL 查询,查询的是一个问题和它的答案数量。具体的查询条件包括:
- `q.c_deleted = 0`:问题没有被删除
- `lower( q.c_title ) LIKE '%123%'`:问题标题中包含字符串 "123"(不区分大小写)
- `q.c_userid = 'ADMIN'`:问题是由用户 "ADMIN" 提交的
- `DATE_FORMAT( q.c_createtime, '%Y-%m-%d' ) BETWEEN '2023-05-01' AND '2023-05-14'`:问题创建时间在 2023 年 5 月 1 日和 5 月 14 日之间
- `count(t.c_id)=0`:问题没有答案
- `ORDER BY c_createtime desc`:按照问题创建时间的倒序排序
查询的结果包括问题的 ID、用户 ID、标题、创建时间、是否为私有问题、被接受的答案 ID、是否被删除以及答案的数量。
相关问题
帮我优化一下这个sql select e.id, e.mobile, e.encoded, CASE e.is_echo WHEN 1 THEN '已回传' else '未回传' END AS isEcho , e.order_no AS orderNo, e.pay_amount AS payAmount, e.operator_id AS operatorId, e.operator_name AS operatorName, e.operator_time AS operatorTime, e.remarks AS remarks, e.`status`, CASE p.pay_status WHEN 1 THEN '支付成功' WHEN 2 THEN '支付失败' END AS payStatus , CASE e.status WHEN 1 THEN '待支付' WHEN 2 THEN '已支付' WHEN 3 THEN '已退款' WHEN 4 THEN '订单关闭' WHEN 5 THEN '退款中' WHEN 6 THEN '退款关闭' END AS statusName, e.create_time AS createTime, u.id AS userId, p.pay_channel AS payChannel, CASE p.pay_channel WHEN 1 THEN '支付宝' WHEN 2 THEN '微信' END AS payChannelName , p.out_trade_no AS outTradeNo, e.third_party_channel AS thirdPartyChannel, info.return_amount AS returnAmount, info.return_phone AS returnPhone, info.return_name AS returnName from equity.equity_order_info e left join equity.user_info u on e.mobile = u.user_photo LEFT JOIN ( SELECT * FROM equity.pay_message GROUP BY order_no ) AS p ON p.order_no = e.order_no left join equity.refund_info AS info ON info.order_no=e.order_no <where> <if test="mobile != null and mobile != ''">and e.mobile = #{mobile}</if> <if test="orderNo != null and orderNo != ''">and e.order_no = #{orderNo}</if> <if test="payAmount != null ">and e.pay_amount = #{payAmount}</if> <if test="thirdPartyChannel != null and thirdPartyChannel != ''">and e.third_party_channel = #{thirdPartyChannel} </if> <if test="outTradeNo != null and outTradeNo != ''">and p.out_trade_no = #{outTradeNo} </if> <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and DATE_FORMAT(e.create_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{startTime},'%Y-%m-%d') AND DATE_FORMAT(#{endTime},'%Y-%m-%d') </if> <if test="status != null and status > 0">and e.status = #{status}</if> <if test="userId != null ">and u.id = #{userId}</if> </where> ORDER BY e.create_time DESC
#{mobile}</if>
<if test="startTime != null">and e.create_time >= #{startTime}</if>
<if test="endTime != null">and e.create_time <= #{endTime}</if>
<if test="status != null">and e.`status` = #{status}</if>
<if test="payStatus != null">and p.pay_status = #{payStatus}</if>
<if test="payChannel != null">and p.pay_channel = #{payChannel}</if>
</where>
ORDER BY e.create_time DESC, e.id DESC
LIMIT #{start},#{length}</code></pre>
优化建议:
1. 尽可能避免使用SELECT *
2. group by 关键字比其他的聚合函数效率低,尽量避免使用
3. 减少使用CASE WHEN,可以使用join来加快速度
4. 尽量减少WHERE子句中的嵌套IF条件,可以使用join来加快速度
5. 为需要检索的列创建索引,如E.mobile、P.pay_channel、E.`status`等
解释代码@Transactional public OrderInfo add(OrderInfo orderInfo) { // 1、生成最基本的订单信息,用户信息,放到orderInfo里 Long userId = orderInfo.getUserid(); //订单id:用户id+当前年月日时分+4位流水号 String orderId = userId + DateUtil.format(new Date(),"yyyyMMddHHmm") + RandomUtil.randomNumbers(4); orderInfo.setOrderid(orderId); //用户相关 UserInfo userInfo = userInfoService.findById(userId); orderInfo.setLinkaddress(userInfo.getAddress()); orderInfo.setLinkman(userInfo.getNickname()); orderInfo.setLinkphone(userInfo.getPhone()); // 2、保存订单表 orderInfo.setCreatetime(DateUtil.formatDateTime(new Date())); orderInfoMapper.insertSelective(orderInfo); List<OrderInfo> orderInfoList = orderInfoMapper.findByOrderId(orderId); // 3、查询订单里面的书籍列表,遍历 List<GoodsInfo> goodsList = orderInfo.getGoodsList(); for(GoodsInfo orderGoodsVO : goodsList){ Long goodsId = orderGoodsVO.getId(); //书籍信息 GoodsInfo goodsDetail = goodsInfoService.findById(goodsId); if(goodsDetail == null){ continue; } Integer orderCount = orderGoodsVO.getCount() == null?0:orderGoodsVO.getCount(); //想买多少 Integer goodsCount = goodsDetail.getCount() == null?0:goodsDetail.getCount(); //有多少库存 //4、扣库存 if(orderCount>goodsCount){ throw new CustomException(ResultCode.ORDER_PAY_ERROR); } goodsDetail.setCount(goodsCount - orderCount); //5、增加销量 int sales = goodsDetail.getSales()==null?0:goodsDetail.getSales(); goodsDetail.setSales(sales+orderCount); goodsInfoService.update(goodsDetail); //6、书籍订单关联表,增加关系 OrderGoodsRel orderGoodsRel = new OrderGoodsRel(); orderGoodsRel.setOrderid(orderInfoList.get(0).getId()); orderGoodsRel.setGoodsid(goodsId); orderGoodsRel.setCount(orderCount); orderGoodsRelMapper.insertSelective(orderGoodsRel); } //7、清空购物车 cartInfoService.empty(userId); return orderInfo; }
这段代码是一个添加订单的方法,使用了事务(@Transactional)来保证操作的原子性。
第一步是生成订单号(orderId),其中包含了用户id、当前时间、以及4位流水号。然后将用户信息(地址、联系人、电话)放入订单信息中。
第二步是保存订单表,将订单信息插入到数据库中。然后查询订单信息,获取订单id。
第三步是遍历订单中的书籍列表,扣减库存、增加销量,并将书籍订单关系表中增加书籍和订单的关联关系。
最后一步是清空购物车,返回订单信息。
如果在扣减库存时订单数量大于库存数量,会抛出一个自定义异常(CustomException),并回滚事务。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)