12人参与 • 2026-08-06 • Mysql
with 语法,可在 mysql 和 oracle 中使用
也叫做子查询部分 (subquery factoring), 是用来定义一个 sql 片断
with t as ( select * from consumer ), t1 as ( select * from consumer ) select * from t, t1
该语句的作用是在, 大量的报表查询时, 使用 with as 可以提取出大量的子查询, 更加简洁
注:with 语法,不适合 mysql 8.0 版本之前
5.6 / 5.7 版本, with 的替代品
create temporary table detail select id, end_time, status from t_wl_record_repairs_detail where end_time is null; select count(1) as sum, 'today' as name from detail... union select count(1) as sum, 'd1' as name.... union...
作用:临时表用来保存一些 ‘临时数据’
特点:
temporary 予以区别-- 可以手动 insert insert into temporary_test(tid, tname) values(1, 'a'); insert into temporary_test(tid, tname) values(2, 'b'); -- 也可手动 delete 和 drop drop temporary table if exists temporary_test;
准备语句(prepared statement)是一种数据库查询的执行机制, 最早由ibm的数据库管理系统db2引入. 随后, 准备语句得到了广泛的支持,被包括mysql、oracle、microsoft sql server等在内的众多数据库管理系统所采用.
-- prepared statement
set @tablename = 't_user';
# select * from @tablename; -- error
-- 构建动态sql语句
set @sql = concat('select * from ', @tablename);
-- 执行动态sql语句
prepare stmt from @sql;
execute stmt;
-- 释放准备的语句
deallocate prepare stmt;
set @sql = concat('drop table ', @tablename);
....
公共表表达式(ctes)是一个命名的临时结果集。cte不作为对象存储,仅在查询执行期间持续
cte是 with 语句中定义的命名查询块. 而 with 语句用于定义和使用cte的语法结构。
common table expressions (ctes) in sql server provide us with a tool that allows us to design and organize queries in ways that may allow faster development, troubleshooting, and improve performance. in the first part of this series, we’ll look at querying against these with a practice data set. from examples of wrapped query checks to organization of data to multiple structured queries, we’ll see how many options we have with this tool and where it may be useful when we query data.
with cte_name as (
query
);
[42000][1762] ora-01762: vopdrv: view query block not in from
ps:在 where 语句中 and 优先级高于 or.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论