zoukankan      html  css  js  c++  java
  • mysql查询语句

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

     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(); 

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

    ---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!
  • 相关阅读:
    JavaScript的3种继承方式
    JavaScript回调函数及数组方法测试
    JavaScript实现二叉树算法
    SpringMVC之使用Servlet原生API作为参数
    HashMap详解
    面试笔记--Fast-Fail(快速失败)机制
    面试笔记--HashMap扩容机制
    org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
    多模块调用Service失败
    常用命令汇总
  • 原文地址:https://www.cnblogs.com/zzzzw/p/4734874.html
Copyright © 2011-2022 走看看