mysql5.7的row_number函数
时间: 2023-09-10 15:10:11 浏览: 146
row_number
在MySQL 5.7版本中,没有内置的ROW_NUMBER函数的写法。如果你需要实现类似ROW_NUMBER功能,可以使用变量和子查询来实现。以下是一种实现方式:
```sql
CREATE TABLE TZ_DIAG_STAND
SELECT *
FROM (
SELECT
@RN := CASE WHEN @DIAGNOSIS_NAME = DIAGNOSIS_NAME THEN @RN + 1 ELSE 1 END AS RN,
@DIAGNOSIS_NAME := DIAGNOSIS_NAME AS DIAG_NAME,
MASTER_ID,
NUM
FROM (
SELECT
T1.DIAGNOSIS_NAME,
MASTER_ID,
COUNT(1) NUM
FROM (
SELECT MASTER_ID, DIAGNOSIS_NAME
FROM ICD_10
WHERE MASTER_ID IS NOT NULL AND DIAGNOSIS_NAME = '妊娠合并肠炎'
) T1
GROUP BY T1.DIAGNOSIS_NAME, MASTER_ID
ORDER BY DIAGNOSIS_NAME, NUM DESC
) A,
(SELECT @RN := 0, @DIAGNOSIS_NAME := 0) B
) A
WHERE RN = 1;
```
这个查询使用了变量@RN和@DIAGNOSIS_NAME来模拟ROW_NUMBER函数的功能,并在最外层的WHERE子句中选择RN等于1的记录。这样就可以获得诊断编码出现最多的记录。请注意,这只是一种实现方式,具体的查询语句可能会根据实际情况有所调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [MySQL5.7版本没有row_number函数的写法](https://blog.csdn.net/qq_43278973/article/details/120205035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文