Mysql 使用分组依据时,选择与排序条件匹配的行

Mysql 使用分组依据时,选择与排序条件匹配的行,mysql,sql,Mysql,Sql,我有一个MySQL房地产数据库。某些属性会使用不同的信息多次列出。以下是一些简化的数据: +--------------+---------+-----------------+------------------------+ | category | price | address | remarks | +--------------+---------+-----------------+---------------------

我有一个MySQL房地产数据库。某些属性会使用不同的信息多次列出。以下是一些简化的数据:

+--------------+---------+-----------------+------------------------+
| category     | price   | address         | remarks                |
+--------------+---------+-----------------+------------------------+
| Commercial   |  435000 | 1224 81ST AVE   | Great retail space!    |
| Multi Family |  435000 | 1224 81ST AVE   | Motivated seller!      |
| Residential  |  435000 | 1224 81ST AVE   | 0% down financing      |
| Residential  | 1095000 | 3290 NW BLUE LN | An exciting new…       |
| Land         |  150000 | 3878 COHO CIR   | Build your dream home… |
| Residential  |  540000 | 3918 COHO CIR   | Ultra-efficient home…  |
| Land         |  300000 | 3890 COHO CIR   | Light-filled condo…    |
| Multi Family |  300000 | 3890 COHO CIR   | Light-filled condo…    |
+--------------+---------+-----------------+------------------------+
数据库中重复的属性可以通过
price
address
列来识别;在示例中,前三行和后两行上方的数据是重复的属性

很容易使用
按价格、地址分组
为每个属性只返回一行。但是我需要得到每个组的行,
类别
住宅
,然后是
商业
,然后是
多家庭
,最后是
土地
。换句话说,如果存在
category=Residential
的行,则我需要该行,否则
category=Commercial

结果应该是:

+--------------+---------+-----------------+------------------------+
| category     | price   | address         | remarks                |
+--------------+---------+-----------------+------------------------+
| Residential  |  435000 | 1224 81ST AVE   | 0% down financing      |
| Residential  | 1095000 | 3290 NW BLUE LN | An exciting new…       |
| Land         |  150000 | 3878 COHO CIR   | Build your dream home… |
| Residential  |  540000 | 3918 COHO CIR   | Ultra-efficient home…  |
| Multi Family |  300000 | 3890 COHO CIR   | Light-filled condo…    |
+--------------+---------+-----------------+------------------------+
在mysql<5.7的旧时代,我可以使用他们的非标准group by扩展,在group by选择行之前应用排序,如下所示:

SELECT * FROM properties
GROUP BY price, address
ORDER BY FIELD(category, 'Residential', 'Commercial', 'Multi Family', 'Land')
这不再有效,因为排序是在应用GROUP BY之后应用的,因此选择的行是任意的

有许多问题和答案通过使用聚合函数来解决排序的GROUPBY子句的备选方案,但我找不到任何关于基于自定义排序函数选择行的用例的讨论


在纯MySQL中有没有办法做到这一点?注意:我使用的是MariaDB 10.1,它没有
行编号()

您还可以通过以下方式执行此操作:

select p.*
from products p
where field(category, 'Residential', 'Commercial', 'Multi Family', 'Land') =
          select field(p2.category, 'Residential', 'Commercial', 'Multi Family', 'Land')
          from products p2
          where p2.price = p.price and p2.address = p.address
          order by 1
          limit 1
         );

但是。您真的应该升级到更新版本的MariaDB。

我使用的是MariaDB 10.1,它没有该功能:
ERROR 1305(42000):功能行号不存在
MariaDB 10.1于2014年发布。是时候更新你的软件了。这非常有用,谢谢!使用窗口功能非常适合此任务。我将询问客户机托管提供商是否可以更新MariaDB.或升级到10.2
select p.*
from products p
where field(category, 'Residential', 'Commercial', 'Multi Family', 'Land') =
          select field(p2.category, 'Residential', 'Commercial', 'Multi Family', 'Land')
          from products p2
          where p2.price = p.price and p2.address = p.address
          order by 1
          limit 1
         );