栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

短路逻辑评估算子

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

短路逻辑评估算子

请记住,查询不是强制执行的。您编写的查询可能在多个线程上运行,因此where子句中的短路运算符不会仅导致一个结果。

而是,使用

LIMIT
子句仅返回第一行。

SELECt * FROM quantitycacheWHERe bookstore_id = 1 OR city_id = 1 OR country_id = 1ORDER BY bookstore_id IS NULL ASC,         city_id IS NULL ASC,         country_id IS NULL ASCLIMIT 1;

要获得结果集中所有书籍的最佳匹配,请将结果保存到临时表中,找到最佳结果,然后返回有趣的字段。

CREATE TEMPORARY TABLE results (id int, book_id int, match_rank int);INSERT INTO results (id, book_id, match_rank)SELECt id, book_id,     -- this assumes that lower numbers are better    CASE WHEN Bookstore_ID is not null then 1          WHEN City_ID is not null then 2          ELSE 3 END as match_rankFROM quantitycacheWHERe bookstore_id = 1 OR city_id = 1 OR country_id = 1;Select * from (    select book_id, MIN(match_rank) as best_rank     from results     group by book_id) as rinner join results as rid     on r.book_id = rid.book_id     and rid.match_rank = r.best_rankinner join quantitycache as q on q.id = rid.id;DROP TABLE results;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431631.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号