【发布时间】:2021-08-03 05:23:59
【问题描述】:
我正在使用 mysql,我想将 ORDER BY 与两个不同的列一起使用,但现在“ORDER BY” 仅适用于 1 列而不是 2 列,我该怎么做?
这是我的mysql查询
select count(DISTINCT p.id) cnt,m.merchantName,l.`name` locality,c.name city,bc.`category_url`,
m.merchantId,mi.src,m.url,group_concat(psm.slot) slots,min(p.sp) sp,group_concat(distinct mi.src) imgs,group_concat( distinct mi.type) imgs_type,m.cuisines,
p.ptype,m.logo,bc.categoryName,m.profile
from products p
LEFT JOIN product_slot_mapping psm
on p.id=psm.productId
left join merchants m
on p.`merchantId`=m.merchantId
left join locality l
on m.localityId=l.localityId
left join city c
on l.cityId=c.cityId
LEFT JOIN area a
on c.areaId=a.area_id
left join `business_category` bc
on m.`businessCategoryId`=bc.`businessCategoryId`
left join merchant_image mi
on m.merchantid=mi.merchantId and mi.isActive=1 and mi.type=3 and mi.isActive=1
left join product_tags pt
on pt.businessCategoryId=m.businessCategoryId and p.ptype=pt.id
left join product_properities_mapping ppm on
p.id=ppm.productId
where p.ptype in (5,6) and p.merchantId in (15538,15696) and a.url='chandigarh' and p.status=1 and m.`isActive`=1 and salesTo>=NOW() and bc.category_url='salons-and-spa'
group by m.merchantId having cnt>0 order by p.sp DESC,m.merchantName ASC limit 100
【问题讨论】:
-
在担心结果的顺序之前,您应该先考虑结果本身。您的查询无效。您
GROUP BY m.merchantId,因此您会为每个商家获得一个结果行。那么您选择的p.ptype是什么?一个商家有很多产品。您要显示哪些产品的 ptype?您要订购的p.sp也是如此。这应该指的是商家的哪些产品?确保你在 MySQL 中有SET sql_mode = 'ONLY_FULL_GROUP_BY';,这样你会在无效查询而不是任意结果上得到一个错误。 -
我觉得我们没有minimal reproducible example。 Needs Debugging Details 以 db-fiddle 的形式。
-
或者至少明确定义“不工作”的含义。你期望会发生什么? 实际上发生了什么?
-
再次,请定义“工作”和“不工作”。你期望两列会发生什么? 实际上发生了什么?
-
@ritika 发布当前和预期输出,因为我认为它确实先按 p.sp 排序,然后按商家排序。我认为您期待其他结果。
标签: php mysql sql-order-by