程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL語句正確插入多值如何運行?

MySQL語句正確插入多值如何運行?

編輯:MySQL綜合教程

以下的文章主要介紹的是MySQL語句正確插入多值的實際操作步驟,以及HQL多表查詢及在條件 in 中插入N個變量值的實際操作,我前兩天在相關網站看見的資料,覺得挺好,就拿出來供大家分享。

一條sql語句插入多組數據:insert into table values (id,name),(id,name),(id,name) 但似乎語句長度有限制,不能超過1MB,並且該語句是MySQL專用的寫法,不是標准sql.

以下轉自paradise總結的HQL多表查詢的寫法及in的使用:

HQL查詢多表的時候,取出結果是兩個對象的列表,但是我只要我自己想要的屬性,之前的HQL語句是這樣寫的:

  1. from Hytxbz h,Tgbzk t where h.hytxbzid=t.hytxbzid and t.bztgid=:bztgid 

結果我debug去看query.list();是Hytxbz和Tgbzk兩個對象的列表,結果並不是我想要的,我改成

  1. from Hybztx h where h.hytxbzid in (select t.hytxbzid from Tgbzk where t.bztgid =:bztgid) 

還是不行,google一把,發現可以這樣寫

  1. select h from Hytxbz as h,Tgbzk as t where h.hytxbzid=t.hytxbzid and t.bztgid=:bztgid 

如果想取得對應屬性的話,也可以這樣寫

  1. select h.hytxbzid from Hytxbz as h,Tgbzk as t where h.hytxbzid=t.hytxbzid and t.bztgid=:bztgid 

發現in的語句可以這樣寫

  1. String ids[]=new String[]{"1","2","3"};   
  2. String hql= " from com,you.YourPOJO where id in (?)";   
  3. Query query = session.createQuery(hql);   
  4. query .setParameters(ids); 

相關方法:

  1. Query setParameters(Object[] objectArray, Type[] typeArray) throws HibernateException;   
  2. Query setParameterList(String string, Collection collection, Type type) throws HibernateException;   
  3. Query setParameterList(String string, Collection collection) throws HibernateException;   
  4. Query setParameterList(String string, Object[] objectArray, Type type) throws HibernateException;   
  5. Query setParameterList(String string, Object[] objectArray) throws HibernateException 

以上的相關內容就是對MySQL語句插入多值的介紹,望你能有所收獲。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved