MySQL語(yǔ)句正確插入多值如何運(yùn)行?
以下的文章主要介紹的是MySQL語(yǔ)句正確插入多值的實(shí)際操作步驟,以及HQL多表查詢及在條件 in 中插入N個(gè)變量值的實(shí)際操作,我前兩天在相關(guān)網(wǎng)站看見(jiàn)的資料,覺(jué)得挺好,就拿出來(lái)供大家分享。
一條sql語(yǔ)句插入多組數(shù)據(jù):insert into table values (id,name),(id,name),(id,name) 但似乎語(yǔ)句長(zhǎng)度有限制,不能超過(guò)1MB,并且該語(yǔ)句是MySQL專用的寫(xiě)法,不是標(biāo)準(zhǔn)sql.
以下轉(zhuǎn)自paradise總結(jié)的HQL多表查詢的寫(xiě)法及in的使用:
HQL查詢多表的時(shí)候,取出結(jié)果是兩個(gè)對(duì)象的列表,但是我只要我自己想要的屬性,之前的HQL語(yǔ)句是這樣寫(xiě)的:
- from Hytxbz h,Tgbzk t where h.hytxbzid=t.hytxbzid and t.bztgid=:bztgid
結(jié)果我debug去看query.list();是Hytxbz和Tgbzk兩個(gè)對(duì)象的列表,結(jié)果并不是我想要的,我改成
- from Hybztx h where h.hytxbzid in (select t.hytxbzid from Tgbzk where t.bztgid =:bztgid)
還是不行,google一把,發(fā)現(xiàn)可以這樣寫(xiě)
- select h from Hytxbz as h,Tgbzk as t where h.hytxbzid=t.hytxbzid and t.bztgid=:bztgid
如果想取得對(duì)應(yīng)屬性的話,也可以這樣寫(xiě)
- select h.hytxbzid from Hytxbz as h,Tgbzk as t where h.hytxbzid=t.hytxbzid and t.bztgid=:bztgid
發(fā)現(xiàn)in的語(yǔ)句可以這樣寫(xiě)
- String ids[]=new String[]{"1","2","3"};
- String hql= " from com,you.YourPOJO where id in (?)";
- Query query = session.createQuery(hql);
- query .setParameters(ids);
相關(guān)方法:
- Query setParameters(Object[] objectArray, Type[] typeArray) throws HibernateException;
- Query setParameterList(String string, Collection collection, Type type) throws HibernateException;
- Query setParameterList(String string, Collection collection) throws HibernateException;
- Query setParameterList(String string, Object[] objectArray, Type type) throws HibernateException;
- Query setParameterList(String string, Object[] objectArray) throws HibernateException
以上的相關(guān)內(nèi)容就是對(duì)MySQL語(yǔ)句插入多值的介紹,望你能有所收獲。
【編輯推薦】