UNION 与LIMIT 结合使用时应该注意的一个地方

本文介绍如何使用SQL语句实现特定条件下的随机排序,并通过LIMIT限制返回结果的数量。利用UNION ALL组合不同条件的查询结果,实现复杂的数据排序需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手册中的信息: 

 
圆括号中用于单个SELECT语句的ORDER BY只有当与LIMIT结合后,才起作用。否则,ORDER BY被优化去除。
(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)

UNION

(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10);

具体例子,来自优快云
问题:
  id name    grade goldUser  
  1  jack    300   10  
  2  mach    200   10  
  3  lich    100   10  
  4  rock    1     0  
  5  mick    1     10  
  6  kight   0     10  
  7  babaya  0     0  
  8  kano    0     10 
如何让
grade  = 1 
的这两个数 
即name = mick 和 name = rock 
这两条记录在结果集里的次序随机排序
答案:
SQL code


    create table lk3 (
    id int not null auto_increment primary key,
    `name` char(20),
    grade int,
    goldUser int
    ) engine=myisam;
    insert into lk3 (`name`,grade,goldUser) values
    ('jack',300, 10 ),
    ('mach', 200, 10 ),
    ('lich', 100 ,10 ),
    ('rock', 1, 0 ),
    ('mick', 1 ,10 ),
    ('kight', 0 ,10 ),
    ('babaya', 0, 0 ),
    ('kano', 0, 10);


    (SELECT * FROM lk3 where grade != 1 order by grade desc,goldUser limit 3)
    union all
    (select * from lk3 where grade = 1 order by rand() limit 2)
-- 如果这里没有limit 2的话,grade为1 的两个用户的结果永远都不可能和预期一样。
    union all
    (select * from lk3 where grade != 1 order by grade desc,goldUser limit 3,3)


结果1:


query result(8 records)
id name grade goldUser 
1 jack 300 10 
2 mach 200 10 
3 lich 100 10 
4 rock 1 0 
5 mick 1 10 
7 babaya 0 0 
6 kight 0 10 
8 kano 0 10 

结果2:

query result(8 records)
id name grade goldUser 
1 jack 300 10 
2 mach 200 10 
3 lich 100 10 
5 mick 1 10 
4 rock 1 0 
7 babaya 0 0 
6 kight 0 10 
8 kano 0 10




本文转自 david_yeung 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/81283,如需转载请自行联系原作者
static void oplus_wired_otg_cc_limit_by_soc_work(struct work_struct *work) { struct oplus_mms_wired *chip = container_of(work, struct oplus_mms_wired, otg_cc_limit_by_soc_work); static bool ui_soc_ready = 0; static int temp_ui_soc = -EINVAL; int rc, i; int otg_online = 0; union mms_msg_data data = { 0 }; if (chip->otg_cc_limit_num < 1) { chg_err("otg_cc_limit not support, num=%d\n", chip->otg_cc_limit_num); return; } oplus_mms_get_item_data(chip->comm_topic, COMM_ITEM_UI_SOC, &data, true); if (data.intval < 0) { temp_ui_soc = data.intval; chg_err("ui_soc not ready: %d\n", data.intval); return; } else { if (temp_ui_soc >= 0) ui_soc_ready = true; chip->ui_soc = data.intval; temp_ui_soc = data.intval; } otg_online = oplus_wired_get_otg_online_status(chip->wired_topic); if (!otg_online) { chg_err("otg not online, return!\n"); return; } for (i = 0; i < chip->otg_cc_limit_num; i++) { if (chip->ui_soc >= chip->otg_cc_limit_list[i].soc) { if (i != chip->otg_cc_limit_index || !ui_soc_ready) { chg_info("otg_cc_limit update, ui_soc: %d, otg_cc_limit to: %dua, pre_index: %d, next_index: %d\n", chip->ui_soc, chip->otg_cc_limit_list[i].limit_current, chip->otg_cc_limit_index, i); chip->otg_cc_limit_index = i; oplus_wired_set_otg_src_pdo(chip->otg_cc_limit_list[i].limit_current); rc = oplus_wired_set_otg_boost_curr_limit(chip->otg_cc_limit_list[i].limit_current); if (rc < 0) chg_err("can not set otg boost curr limit,rc=%d\n",rc); } else { chg_info("otg_cc_limit not update, ui_soc:[%d], otg_cc_limit:[%d] ma, current index:[%d]\n", chip->ui_soc, chip->otg_cc_limit_list[i].limit_current, chip->otg_cc_limit_index); } break; } } }
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值