我有以下查询:
SELECT *
FROM vendors_parts INNER JOIN
vendors ON vendors_parts.vendor = vendors.vendor_id
WHERE (vendors_parts.vendor = @vendor_id) AND (vendors_parts.active = @active) OR
(vendors_parts.vendor = @vendor_id) AND (vendors_parts.active = @inactive)
ORDER BY vendors_parts.old_id, vendors_parts.vendor_part, date_start DESC, vendors_parts.active DESC
它以以下格式返回数据:
vendor_part active old_id
b 1 1
a 0 1
a 0 1
b 0 1
x 1 3
z 1 5
c 1 20
c 0 20
我的问题是如何在对 vendor_part 升序和活动降序进行排序时将 old_id 保持在一起?我想要的输出是:
vendor_part active old_id
b 1 1
a 0 1
a 0 1
b 0 1
c 1 20
c 0 20
x 1 3
z 1 5
我还尝试了一个按 old_id 分组的子查询,但这将我限制为只有一个唯一的 old_id:
SELECT *
FROM
(
SELECT * FROM vendors_parts INNER JOIN vendors ON vendors_parts.vendor = vendors.vendor_id
WHERE (vendors_parts.vendor = @vendor_id) AND (vendors_parts.active = @active) OR (vendors_parts.vendor = @vendor_id) AND (vendors_parts.active = @inactive)
GROUP BY vendors_parts.old_id
) temp_table
ORDER BY vendor_part
如何显示分组在一起的所有 old_id,并按各自组内的 vendor_part 字母顺序排序?
谢谢你