mysql查询语句

本文详细解释了如何处理SQL中的多对多关系,并提供了Java代码实现的示例。通过创建中间表和使用复杂的SQL语句,有效地解决多对多关系查询问题。

  如果两个表格是多对多的关系,这时候要自己建立一个中间表格,这个中间表格包含这两个表的主键作为参数

 1 use test
 2 select id from ddxx where czid =222;
 3 select a.czmc,id,b.status,dj from station a,ddxx b where a.czdm=b.czid 
 4 select csmc,csdm from station c where c.czdm IN
 5 (select b.sid from stationjoinline b where b.lid IN" )
 6 
 7 select * from station c where c.czdm IN(
 8 select b.sid from stationjoinline b where b.lid='1'
 9 select a.xlid from line a where a.xlmc='京广线'
10 )
11 
12 select b.sid from stationjoinline b where b.lid IN
13 (select a.xlid from line a where a.xlmc='京广线')
14 
15 
16 
17 多对多查询
18 select * from station c where c.czdm IN(
19 select b.sid from stationjoinline b where b.lid IN
20 (select a.xlid from line a where a.xlmc='京广线')
21 )

 

1.对应要java中的时候sql语句是要修改的,改成

2.如果字符串太长的话,可以分拆,使用的方法是:“+”放在两端代码中间

3.有append的方法的

1 StringBuffer s=new StringBuffer();
2         s.append("update ddxx a INNER JOIN");    
3         s.append("(select id from ddxx where czid  not in(select DISTINCT czdm from station)) as b");
4         s.append(" ON a.id=b.id set czid="+czid);
5         

 

复杂SQL语句 

单一表:注解,BaseDAO已经大部分实现 

两表,一对多:建表的时候,在多的那一端,放一个外键使用原生sql去做复杂查询 

复杂的增删改:  SQLQuery q2 = gs().createSQLQuery(sql2);

       int b = q2.executeUpdate(); 

两表,多对多:建一个中间表,拆分成两个一对多,再使用复杂查询去解决问题

转载于:https://www.cnblogs.com/zzzzw/p/4734874.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值