java 实现 sql join_7种SQL Join语句

本文介绍了SQL语句结构,并通过实例详细讲解了7种JOIN方式:左连接、右连接、内连接、左连接的差集、右连接的差集、全外连接以及获取交集补集的方法,以Java实现SQL JOIN操作。
摘要由CSDN通过智能技术生成

1、SQL语句结构

select  distinct  < select_list >

from  < left_table > < join_type >

join  < right_table >

on  < join_condition >

where  < where_condition >

group by  < group_by_list >

having  < having_condition >

order by  < order_by_condition >

limit  < limit_number >

2、7种Join方式及实例

实验脚本:

drop table IF EXISTS shuzi ;

create table shuzi (id tinyint,note varchar(20));

insert into shuzi values (1,'一'),(2,'二'),(3,'三'),(4,'四'),(5,'五'),(6,'六'),(7,'七'),(8,'八'),(9,'九'),(10,'十');

select * from shuzi;

drop table IF EXISTS qianshu ;

create table qianshu (id int,des varchar(20));

insert into qianshu values (1,'壹'),(2,'贰'),(4,'肆'),(5,'伍'),(6,'陆'),(10,'拾'),(100,'佰'),(1000,'仟'),(10000,'万');

select * from qianshu;

c410ae0d38eb37145f163986172d01ff.png

左连接,左表的全部,右表不满足的列补空

select a.id,a.note,b.id,b.des from shuzi a left join qianshu  b on a.id=b.id order by a.id;

41dc9c7c9093e1c1e12ff60df9f2ba15.png

右连接,右表的全部,左表不满足的列补空

select a.id,a.note,b.id,b.des from shuzi a right join qianshu  b on a.id=b.id order by b.id;

c82425de7f7c9d7bf2c34fd4bb27c967.png

内连接,只输出左右表均存在的记录(默认from a,b方式)

SELECT a.id,a.note,b.id,b.des FROM shuzi a INNER JOIN qianshu  b ON a.id=b.id ORDER BY b.id;

fd9bf4314e317d5a2a5849925dfc56d2.png

左连接,只保留左表特有数据(差集)

select a.id,a.note,b.id,b.des from shuzi a left join qianshu  b on a.id=b.id where b.id is null order by a.id

f87a194df204ccec60214c4d3a4c635b.png

右连接,只保留右表特有数据(差集)

SELECT a.id,a.note,b.id,b.des FROM shuzi a RIGHT JOIN qianshu  b ON a.id=b.id WHERE a.id IS NULL ORDER BY b.id;

782793228df29da2251a735678efeeb8.png

全外连接,获取左右表的所有记录,各自没有时补空

mysql不支持full outer join,要实现全外连接可以通过合并左,右外连接结果集实现

select a.id,a.note,b.id,b.des from shuzi a left join qianshu  b on a.id=b.id

union

select a.id,a.note,b.id,b.des from shuzi a right join qianshu  b on a.id=b.id

d8a9f254c122af6f7f3e07e54d46d76a.png

获取两表连接交集的补集(最后一个)

SELECT * FROM (

SELECT a.id aid,a.note,b.id bid,b.des FROM shuzi a LEFT JOIN qianshu  b ON a.id=b.id

UNION

SELECT a.id aid,a.note,b.id bid,b.des FROM shuzi a RIGHT JOIN qianshu  b ON a.id=b.id) v_a

WHERE aid IS NULL OR bid IS NULL;

570f7d5c149e08982a0a12fcfff560ab.png

Java中可以使用第三方库如JSqlParser和PrettySQL来格式化SQL语句。其中,JSqlParser可以将SQL语句解析成AST(抽象语法树)形式,然后再将AST转化为格式化后的SQL语句;而PrettySQL则可以直接对SQL语句进行格式化。 以下是使用JSqlParser进行SQL语句格式化的示例代码: ```java import java.io.StringReader; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.statement.Statement; import net.sf.jsqlparser.util.deparser.ExpressionDeParser; import net.sf.jsqlparser.util.deparser.SelectDeParser; import net.sf.jsqlparser.util.deparser.StatementDeParser; public class SqlFormatter { public static String format(String sql) throws Exception { Statement stmt = CCJSqlParserUtil.parse(new StringReader(sql)); SelectDeParser selectDeparser = new SelectDeParser(); StatementDeParser stmtDeparser = new StatementDeParser(selectDeparser, new StringBuilder()); ExpressionDeParser expressionDeparser = new ExpressionDeParser(stmtDeparser, selectDeparser, new StringBuilder()); stmtDeparser.setExpressionVisitor(expressionDeparser); stmt.accept(stmtDeparser); return stmtDeparser.getBuffer().toString(); } } ``` 使用示例: ```java String sql = "SELECT id, name FROM users WHERE age > 18"; String formattedSql = SqlFormatter.format(sql); System.out.println(formattedSql); ``` 输出结果: ``` SELECT id, name FROM users WHERE age > 18 ``` 注:JSqlParser还可以进行更复杂的SQL语句解析和转化,如增、删、改操作,JOIN查询等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值