sqlÖжà¸ö×Ö¶ÎinµÄÓ÷¨
sql ÖÐÕë¶Ô¶à¸ö×Ö¶ÎʹÓà in ²Ù×÷·ûÓÐÁ½ÖÖÒªÁ죺ͨ¹ý×ÓÅÌÎÊ»òÖ±½ÓÁгö¶à¸öÖµ¡£×ÓÅÌÎÊÓÃÓÚ´ÓÆäËûÅÌÎÊÖмìË÷Öµ£¬¶ø¶à¸öÖµ¿ÉÒÔÓöººÅÍÑÀëÖ±½ÓÁгö¡£in ²Ù×÷·û¼ì²é¸ø¶¨ÖµÊÇ·ñÔÚÖ¸¶¨ÖµµÄÁбíÖС£
SQL Öжà¸ö×Ö¶ÎÖÐµÄ IN Ó÷¨
IN ²Ù×÷·ûÓÃÓÚ¼ì²é¸ø¶¨ÖµÊÇ·ñÔÚÖ¸¶¨ÖµµÄÁбíÖС£µ±ÐèÒª¼ì²é¶à¸ö×Ö¶Îʱ£¬¿ÉÒÔÔÚ IN ²Ù×÷·ûÖÐʹÓÃ×ÓÅÌÎÊ»ò¶à¸öÖµ¡£
×ÓÅÌÎÊ
×ÓÅÌÎÊÊÇÒ»ÖÖǶÌ×ÅÌÎÊ£¬ÓÃÓÚ´ÓÊý¾Ý¿âÖмìË÷Êý¾Ý¡£Ëü¿ÉÒÔÔÚ IN ²Ù×÷·ûÖÐʹÓ㬽«¶à¸ö×ֶεÄÖµÓëÆä½ÏÁ¿¡£Óï·¨ÈçÏ£º
SELECT * FROM table_name WHERE (column1, column2, ...) IN ( SELECT column1, column2, ... FROM subquery );
µÇ¼ºó¸´ÖÆ
ÀýÈ磺
SELECT * FROM orders WHERE (order_id, customer_id) IN ( SELECT order_id, customer_id FROM order_details );
µÇ¼ºó¸´ÖÆ
¶à¸öÖµ
Ò²¿ÉÒÔÔÚ IN ²Ù×÷·ûÖÐÖ¸¶¨¶à¸öÖµ£¬ÓöººÅÍÑÀë¡£Óï·¨ÈçÏ£º
SELECT * FROM table_name WHERE (column1, column2, ...) IN (value1, value2, ...);
µÇ¼ºó¸´ÖÆ
ÀýÈ磺
SELECT * FROM customers WHERE (first_name, last_name) IN ('John', 'Doe');
µÇ¼ºó¸´ÖÆ
ʾÀý
˼Á¿Ò»¸ö°üÀ¨ÒÔÏÂÊý¾ÝµÄ±í orders£º
order_id | customer_id | product_id |
---|---|---|
101 | 1 | 201 |
102 | 1 | 301 |
103 | 2 | 201 |
104 | 2 | 302 |
ÒªÕÒ³ö¶©¹ºÁ˲úÆ· 201 »ò 302 µÄ¿Í»§£¬¿ÉÒÔʹÓà IN ²Ù×÷·û£º
SELECT * FROM orders WHERE (product_id) IN (201, 302);
µÇ¼ºó¸´ÖÆ
Êä³ö½«ÊÇ£º
order_id | customer_id | product_id |
---|---|---|
101 | 1 | 201 |
102 | 1 | 301 |
103 | 2 | 201 |
104 | 2 | 302 |
ÒÔÉϾÍÊÇsqlÖжà¸ö×Ö¶ÎinµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡