SQLÖÐexistsºÍinµÄÇø±ð
¼ÙÉèÈçÏÂÓ¦Óãº
Á½Õű헗Óû§±íTDefUser£¨userid£¬address,phone£©ºÍÏû·Ñ±íTAccConsume(userid,time,amount)£¬ÐèÒª²éÏû·Ñ³¬¹ý5000µÄÓû§¼Ç¼¡£
ÓÃexists:
select * from TDefUser
where exists (select 1 from TAccConsume where TDefUser.userid=TAccConsume.userid and TAccConsume.amount>5000)
ÓÃin:
select * from TDefUser
where userid in (select userid from TAccConsume where TAccConsume.amount>5000)
ͨ³£Çé¿öϲÉÓÃexistsÒª±ÈinЧÂʸߡ£
////
exists()ºóÃæµÄ×Ó²éѯ±»³Æ×öÏà¹Ø×Ó²éѯ ËûÊDz»·µ»ØÁбíµÄÖµµÄ.Ö»ÊÇ·µ»ØÒ»¸ötrue»òfalseµÄ½á¹û(ÕâÒ²ÊÇΪʲô×Ó²éѯÀïÊÇ"select 1"µÄÔÒò£¬»»³É"select 6"ÍêÈ«Ò»Ñù£¬µ±È»Ò²¿ÉÒÔselect×ֶΣ¬µ«ÊÇÃ÷ÏÔЧÂʵÍЩ)
ÆäÔËÐз½Ê½ÊÇÏÈÔËÐÐÖ÷²éѯһ´Î£¨ÏȵóöÖ÷²éѯµÄ½á¹û£© ÔÙ¸ù¾ÝÖ÷²éѯÖеÄÿһÐÐÈ¥×Ó²éѯÀïÈ¥²éѯÓëÆä¶ÔÓ¦µÄ½á¹û.Èç¹ûÊÇtrueÔòÊä³ö,·´Ö®Ôò²»Êä³ö.
in()ºóÃæµÄ×Ó²éѯ ÊÇ·µ»Ø½á¹û¼¯µÄ,»»¾ä»°ËµÖ´ÐдÎÐòºÍexists()²»Ò»Ñù.×Ó²éѯÏȲúÉú½á¹û¼¯,È»ºóÖ÷²éѯÔÙÈ¥½á¹û¼¯ÀïÈ¥ÕÒ·ûºÏÒªÇóµÄ×Ö¶ÎÁбíÈ¥.·ûºÏÒªÇóµÄÊä³ö,·´Ö®Ôò²»Êä³ö.
£¨µÚÒ»´Î²éѯÕÒ³ö>5000µÄ½á¹û£¬µÚ¶þ´Î²éѯÅжÏuseridÊÇ·ñÔÚ½á¹ûÖУ©
////
±ÈÈçÓû§±íTDefUser£¨userid£¬address,phone£©£¬Ïû·Ñ±íTAccConsume(userid,time,amount)Êý¾ÝÈçÏ£º
Ïû·Ñ±í¾Û¼¯Ë÷ÒýÊÇuserid,time
Êý¾Ý£¨×¢ÒâÒòΪÓоۼ¯Ë÷Òý,ʵ¼Ê´æ´¢Ò²ÊÇ°´ÒÔÏ´ÎÐòµÄ£©
1 2006-1-1 200
1 2006-1-2 300
1 2006-1-2 500
1 2006-1-3 2000
1 2006-1-3 2000
1 2006-1-4 400
1 2006-1-5 500
2 2006-1-1 200
2 2006-1-2 300
2 2006-1-2 500
2 2006-1-3 2000
2 2006-1-3 6000
2 2006-1-4 400
2 2006-1-5 8000
3 2006-1-1 7000
3 2006-1-2 30000
3 2006-1-2 50000
3 2006-1-3 20000
Óï¾ä£º
select * from TDefUser
where exists (select 1 from TAccConsume where TDefUser.userid=TAccConsume.userid and TAccConsume.amount>5000)
¶ÔÓÚuserid=1£¬ÐèÒªÕÒËùÓмǼ£¬²Å
Ïà¹ØÎĵµ£º
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--ÿҳÏÔʾ¼Ç¼ÌõÊý
@currentpage int output,--µÚ¼¸Ò³
@orderid nvarchar(50),--Ö÷¼üÅÅÐò
@sort int,--ÅÅÐò·½Ê½£¬1±íʾÉýÐò£¬0±íʾ½µÐòÅÅÁÐ
......
Ò»¡¢ÉîÈëdz³öÀí½âË÷Òý½á¹¹
¡¡¡¡Êµ¼ÊÉÏ£¬Äú¿ÉÒÔ°ÑË÷ÒýÀí½âΪһÖÖÌØÊâµÄĿ¼¡£Î¢ÈíµÄSQL SERVERÌṩÁËÁ½ÖÖË÷Òý£º¾Û¼¯Ë÷Òý£¨clustered index£¬Ò²³Æ¾ÛÀàË÷Òý¡¢´Ø¼¯Ë÷Òý£©ºÍ·Ç¾Û¼¯Ë÷Òý£¨nonclustered index£¬Ò²³Æ·Ç¾ÛÀàË÷Òý¡¢·Ç´Ø¼¯Ë÷Òý£©¡£ÏÂÃ棬ÎÒÃǾÙÀýÀ´ËµÃ÷һϾۼ¯Ë÷ÒýºÍ·Ç¾Û¼¯Ë÷ÒýµÄÇø±ð£º
¡¡¡¡Æäʵ£¬ÎÒÃǵĺºÓï×Öµäµ ......
ʹÓÃË÷Òý¿É¿ìËÙ·ÃÎÊÊý¾Ý¿â±íÖеÄÌض¨ÐÅÏ¢¡£Ë÷ÒýÊǶÔÊý¾Ý¿â±íÖÐÒ»Áлò¶àÁеÄÖµ½øÐÐÅÅÐòµÄÒ»Öֽṹ£¬ÀýÈç employee ±íµÄÐÕ£¨lname£©ÁС£Èç¹ûÒª°´ÐÕ²éÕÒÌض¨Ö°Ô±£¬Óë±ØÐëËÑË÷±íÖеÄËùÓÐÐÐÏà±È£¬Ë÷Òý»á°ïÖúÄú¸ü¿ìµØ»ñµÃ¸ÃÐÅÏ¢¡£
¡¡¡¡Ë÷ÒýÊÇÒ»¸öµ¥¶ÀµÄ¡¢ÎïÀíµÄÊý¾Ý¿â½á¹¹£¬ËüÊÇij¸ö±íÖÐÒ»ÁлòÈô¸ÉÁÐÖµµÄ¼¯ºÏºÍÏàÓ¦µÄÖ¸Ïò±íÖÐÎïÀ ......
sqlÊǹØϵÊý¾Ý¿â²éѯÓïÑÔ,Ãæ¶ÔµÄÊý¾Ý¿â;
¶øhqlÊÇHibernateÕâÑùµÄÊý¾Ý¿â³Ö¾Ã»¯¿ò¼ÜÌṩµÄÄÚÖòéѯÓïÑÔ,ËäÈ»ËûÃǵÄÄ¿µÄ¶¼ÊÇΪÁË´ÓÊý¾Ý¿â²éѯÐèÒªµÄÊý¾Ý,µ«sql²Ù×÷µÄÊÇÊý¾Ý¿â±íºÍ×Ö¶Î,
¶ø×÷ΪÃæÏò¶ÔÏóµÄhql²Ù×÷µÄÔòÊdz־û¯À༰ÆäÊôÐÔ¡£
¾ÙÒ»¸ö¼òµ¥µÄÀý×Ó
sqlÓï¾ä
select * from tb_goods where id in (select goodsid f ......