【问题标题】:ORDER BY with multiple columns not working with Join in mysql [closed]具有多列的 ORDER BY 不适用于 mysql 中的 Join [关闭]
【发布时间】: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 exampleNeeds Debugging Details 以 db-fiddle 的形式。
  • 或者至少明确定义“不工作”的含义。你期望会发生什么? 实际上发生了什么?
  • 再次,请定义“工作”和“不工作”。你期望两列会发生什么? 实际上发生了什么?
  • @ritika 发布当前和预期输出,因为我认为它确实先按 p.sp 排序,然后按商家排序。我认为您期待其他结果。

标签: php mysql sql-order-by


【解决方案1】:

MySQL 在未按完全分组模式工作时允许无效查询。因此,在使用 mySQL 时,请始终确保拥有SET sql_mode = 'ONLY_FULL_GROUP_BY';

此设置会影响 MySQL 处理无效聚合的方式。这是一个简单的例子:

select employee_name from employees group by department;

我们在这里按部门分组,因此每个部门得到一个结果行。但是我们选择员工姓名。哪一个?一个部门有很多员工。 MySQL 应该引发一个错误,并且在完全分组模式下工作时会引发错误。但是,当不在该模式下工作时,MySQL 会默默地“修复”您的无效查询:

select ANY_VALUE(employee_name) from employees group by department;

即它只是任意选择每个部门的一名员工。

现在,如果你有这个:

select department, min(employee_name)
from employees
group by department
order by employee_name;

然后 MySQL 将其转换为

select department, min(employee_name)
from employees
group by department
order by any_value(employee_name);

假设 A 部门有员工 John 和 Tom,B 部门有 Max 和 Jane。您可能会期待这样的结果:B|Jane、A|John,因为这些是每个部门的最少名称,并且 Jane 在字母表中排在 John 之前。但是订单是针对部门的任意员工姓名,因此您也可以得到以下结果:A|John, B|Jane,如果 DBMS 恰好在 John 和 Max 上订购。结果似乎是无序的,但这是因为我们按未显示的内容排序。

现在查看您的查询:

select ... min(p.sp) sp ...
group by m.merchantId
order by p.sp DESC, m.merchantName

MySQL 把它变成

select ... min(p.sp) sp ...
group by m.merchantId
order by ANY_VALUE(p.sp) DESC, m.merchantName

上面说的适用。如果您想按显示的sp 订购,请按它订购:

select ... min(p.sp) sp ...
group by m.merchantId
order by min(p.sp) DESC, m.merchantName

order by sp DESC, m.merchantName(即sp 而不是p.sp)也应该可以工作,因为我认为别名优先于ORDER BY 中的列名,但不会依赖于此。使用列名作为别名(如min(p.sp) sp)是一个坏主意,因为这会导致混淆和错误。

(您还应该自己将select ... p.ptype ... 替换为select ... any_value(p.ptype) ...,以使您的查询中正在发生的事情一目了然。然后设置完整的分组模式以查看您的查询是否最终有效。)

【讨论】:

    【解决方案2】:

    在使用两列排序的情况下,MySQL 将第一个字段视为主要字段,将后者视为次要字段。因此,它首先对主要排序,然后对第二个排序。你可以参考这个link,这就是为什么它似乎与p.sp一起工作,然后根据m.merchantName对其进行排序,所以你必须根据优先级编写列名。

    【讨论】:

    • 不明白!请告诉我我的查询有问题吗?如果是,那么您可以编写正确的查询以便我检查吗?或者这是否可以同时使用两个不同的列进行排序?
    • 请尝试一次:按 m.merchantName, p.sp DESC 订购
    • 它实际上与两列一起排序,但它以 p.sp 作为主要排序列,将 m.merchantName 作为次要列。因此,它看起来只使用一列,而它首先使用 p.sp 排序,然后是 m.merchantName
    • 但我想要两种类型的排序(“ASC”和“DESC”),换句话说,我想要在 mysql 查询中使用两种不同的排序方式
    • 不管你写不写,ASC都是默认的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2013-10-01
    • 2022-01-26
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多