不使用group by 完成上述sql语句
时间: 2023-08-15 07:10:39 浏览: 108
如果不使用 `GROUP BY` 来完成上述 SQL 查询语句,可以使用 `DISTINCT` 关键字来实现去重。修改后的 SQL 查询语句如下:
```sql
<select id="selectByPurchase" resultType="com.delivery.Models.MyPurchase">
SELECT DISTINCT tb1.purchase_id AS purchaseId, tb1.own_name AS ownName, tb1.purchase_type AS purchaseType,
tb1.total_price AS totalPrice, tb1.address AS address, tb1.purchase_status AS purchaseStatus,
tb1.create_time AS createTime, tb1.update_time AS updateTime, tb3.picture AS picture
FROM tb_purchase AS tb1
INNER JOIN tb_purchase_detail AS tb2 ON tb1.purchase_id = tb2.purchase_id
INNER JOIN tb_order AS tb3 ON tb2.order_id = tb3.order_id
INNER JOIN tb_user AS tu ON tu.user_name = tb1.own_name
WHERE tb1.own_name = #{name}
</select>
```
这个修改后的查询语句使用了 `DISTINCT` 关键字来确保结果集中的行是唯一的。通过使用 `INNER JOIN` 关键字和连接条件将表关联起来,避免了使用子查询的复杂性。最后,通过添加 `WHERE` 条件来限制购买者的名称为给定的参数值 `#{name}`。
阅读全文