那分组那里可以帮我换成其他写法嘛,不用group by
时间: 2023-07-31 09:10:45 浏览: 92
如果不使用 `GROUP BY`,可以使用子查询来实现分组聚合的功能。具体做法是先将需要聚合的数据通过子查询计算出来,然后将计算结果和其他需要查询的列进行连接操作。以下是一种可能的实现方式:
```sql
SELECT ban_sowing.sowingId AS sowing_id,
ban_seq.seqId AS id,
ban_seq.bannerId AS banner_id,
ban_seq.bannerName AS banner_name,
ban_seq.fromIdSourceJson AS fromIdSourceJson,
ban_seq.position AS position,
ban_seq.startPage AS startPage,
ban_seq.createTime AS ban_create_time,
ban_seq.updateTime AS ban_update_time,
ban_sowing.seqId AS sowing_id,
ban_sowing.order AS sow_order,
ban_sow.sowingName AS sowing_name,
ban_sow.picUrl AS sow_picUrl,
ban_sow.picType AS sow_picType,
ban_sow.picValue AS sow_picValue,
ban_sow.status AS sow_status,
UNIX_TIMESTAMP(ban_sow.startTime) * 1000 AS sow_startTime,
UNIX_TIMESTAMP(ban_sow.endTime) * 1000 AS sow_endTime,
ban_sid.sid_list AS ban_sids
FROM (SELECT bansow.sowingId,
GROUP_CONCAT(sty.sid) AS sid_list
FROM video_operation.bannerSowingConfig bansow
LEFT JOIN video_operation.bannerConfigStrategy sty
ON bansow.bannerId = sty.rid
WHERE bansow.status = 'OK'
GROUP BY bansow.sowingId) AS ban_sid
JOIN video_operation.bannerSowingConfig AS ban_sowing
ON ban_sowing.sowingId = ban_sid.sowingId
JOIN video_operation.sowingConfig AS ban_sow
ON ban_sowing.sowingId = ban_sow.sowingId
JOIN video_operation.bannerConfig AS ban_seq
ON ban_sowing.bannerId = ban_seq.bannerId
WHERE ban_seq.`status` = 'OK'
AND ban_sow.`status` = 'OK'
AND NOW() BETWEEN ban_sow.startTime AND ban_sow.endTime
AND ban_seq.`type` = 'center'
ORDER BY ban_sow.updateTime DESC;
```
这条 SQL 查询语句先使用子查询来计算每个轮播图对应的广告位 ID 列表,然后将计算结果和其他需要查询的列进行连接操作,最终得到结果。需要注意的是,这种实现方式可能会比使用 `GROUP BY` 更加复杂和低效,具体取决于具体的数据规模和查询需求。
阅读全文