sqlÖÐinµÄÌæ»»º¯Êý
sql ÖÐ in ÔËËã·ûµÄÌæ»»º¯ÊýÓУºexists£º¼ì²é×ÓÅÌÎÊÊÇ·ñ·µ»ØÈκÎÐС£any£º¼ì²éÁбíÖÐÊÇ·ñ±£´æÓë×ÓÅÌÎÊÆ¥ÅäµÄÖµ¡£all£º¼ì²éÁбíÖÐËùÓÐÖµ¶¼Óë×ÓÅÌÎÊÆ¥Åä¡£union all£º½«ÅÌÎÊЧ¹û×éºÏÔÚÒ»Æð¡£Ñ¡ÔñÌæ»»º¯ÊýÈ¡¾öÓÚ±í½á¹¹ºÍ×ÓÅÌÎÊÖØ´óÐÔ¡£
SQL ÖÐ IN µÄÌæ»»º¯Êý
IN ÔËËã·ûÓÃÓÚ¼ì²é¸ø¶¨µÄÖµÊÇ·ñÔÚÖ¸¶¨ÁбíÖС£È»¶ø£¬ÔÚijЩÇéÐÎÏ£¬Ê¹Óà IN ÔËËã·û¿ÉÄÜЧÂʵÍÏ£¬ÓÈÆäÊÇÔÚÁбí°üÀ¨´ó×Úֵʱ¡£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬SQL ÌṩÁ˼¸¸öÌæ»» IN ÔËËã·ûµÄº¯Êý£¬¿ÉÒÔÌá¸ßÐÔÄÜ¡£
1. EXISTS
EXISTS ÔËËã·û¼ì²é¸ø¶¨µÄ×ÓÅÌÎÊÊÇ·ñ·µ»ØÈκÎÐС£Ëü¿ÉÒÔÓÃÀ´È¡´ú IN ÔËËã·û£¬Óï·¨ÈçÏ£º
SELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE table2.id IN (1, 2, 3))
µÇ¼ºó¸´ÖÆ
2. ANY
ANY ÔËËã·û·µ»ØÒ»¸ö²¼¶ûÖµ£¬Ö¸Ê¾¸ø¶¨µÄÁбíÖÐÊÇ·ñ±£´æÓë×ÓÅÌÎÊ·µ»ØµÄÈκÎֵƥÅäµÄÖµ¡£Ëü¿ÉÒÔÓÃÀ´È¡´ú IN ÔËËã·û£¬Óï·¨ÈçÏ£º
SELECT * FROM table1 WHERE table1.id = ANY (SELECT id FROM table2)
µÇ¼ºó¸´ÖÆ
3. ALL
ALL ÔËËã·û·µ»ØÒ»¸ö²¼¶ûÖµ£¬Ö¸Ê¾¸ø¶¨µÄÁбíÖÐËùÓÐÖµ¶¼Óë×ÓÅÌÎÊ·µ»ØµÄֵƥÅä¡£Ëü¿ÉÒÔÓÃÀ´È¡´ú IN ÔËËã·û£¬Óï·¨ÈçÏ£º
SELECT * FROM table1 WHERE table1.id IN (SELECT id FROM table2)
µÇ¼ºó¸´ÖÆ
4. UNION ALL
UNION ALL ÔËËã·û¿ÉÒÔ½«Á½¸ö»ò¶à¸öÅÌÎʵÄЧ¹û×éºÏÔÚÒ»Æð¡£Ëü¿ÉÒÔÓÃÀ´È¡´ú IN ÔËËã·û£¬Óï·¨ÈçÏ£º
SELECT * FROM table1 UNION ALL SELECT * FROM table2 WHERE table2.id IN (1, 2, 3)
µÇ¼ºó¸´ÖÆ
Ñ¡ÔñºÏÊʵÄÌæ»»º¯Êý
Ñ¡Ôñ×îºÏÊʵÄÌæ»» IN ÔËËã·ûµÄº¯ÊýÈ¡¾öÓÚËùÅÌÎÊÊý¾ÝµÄ±í½á¹¹ºÍ×ÓÅÌÎʵÄÖØ´óÐÔ¡£
EXISTS ×îÊʺϼì²é×ÓÅÌÎÊÊÇ·ñ·µ»ØÈκÎÐУ¬ÎÞÂÛ×ÓÅÌÎÊ·µ»Ø¼¸¶àÐС£
ANY ×îÊʺϼì²é¸ø¶¨ÁбíÖÐÊÇ·ñ±£´æÓë×ÓÅÌÎÊÖзµ»ØµÄÈκÎֵƥÅäµÄÖµ¡£
ALL ×îÊʺϼì²é¸ø¶¨ÁбíÖеÄËùÓÐÖµ¶¼Óë×ÓÅÌÎÊÖзµ»ØµÄֵƥÅä¡£
UNION ALL ×îÊʺϽ«Á½¸ö»ò¶à¸öÅÌÎʵÄЧ¹û×éºÏÔÚÒ»Æð£¬ÆäÖÐÒ»¸öÅÌÎÊʹÓà IN ÔËËã·û¡£
ÒÔÉϾÍÊÇsqlÖÐinµÄÌæ»»º¯ÊýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡