TA的每日心情 | 无聊 昨天 09:05 |
---|
签到天数: 1050 天 连续签到: 1 天 [LV.10]测试总司令
|
以下是在 SQL 中使用 `WHERE` 进行条件判定男女金额前五位的查询方法,假设表名为 `your_table`,有字段 `gender`(性别)和 `amount`(金额):
一、MySQL 数据库
- <font size="3" face="微软雅黑">```sql
- -- 先分别查询出男性金额前五位和女性金额前五位
- (SELECT * FROM your_table WHERE gender = '男' ORDER BY amount DESC LIMIT 5)
- UNION ALL
- (SELECT * FROM your_table WHERE gender = '女' ORDER BY amount DESC LIMIT 5);
- ```</font>
复制代码
二、SQL Server 数据库
- <font size="3" face="微软雅黑">```sql
- -- 先分别查询出男性金额前五位和女性金额前五位
- SELECT * FROM
- (SELECT *, ROW_NUMBER() OVER (PARTITION BY gender ORDER BY amount DESC) AS rn FROM your_table) AS t
- WHERE (gender = '男' AND rn <= 5) OR (gender = '女' AND rn <= 5);
- ```</font>
复制代码
三、Oracle 数据库
- <font size="3" face="微软雅黑">
- ```sql
- -- 先分别查询出男性金额前五位和女性金额前五位
- SELECT * FROM
- (SELECT t.*, ROW_NUMBER() OVER (PARTITION BY gender ORDER BY amount DESC) AS rn FROM your_table t)
- WHERE (gender = '男' AND rn <= 5) OR (gender = '女' AND rn <= 5);
- ```</font>
复制代码
|
|