sqlÖÐjoin onºÍjoin inµÄÇø±ð
join on ºÍ join in µÄÇø±ðÔÚÓÚ£º1. join on ʹÓÃÏàµÈ½ÏÁ¿Ìõ¼þ£¬¶ø join in ¿ÉÒÔʹÓÃÈκνÏÁ¿ÔËËã·û£»2. join in Ö§³Ö×ÓÅÌÎÊ£¬¶ø join on ²»¿É£»3. join on ͨ³£±È join in ¸ü¸ßЧ£¬ÓÉÓÚËüʹÓÃË÷Òý¡£
SQL ÖÐ JOIN ON ºÍ JOIN IN µÄÇø±ð
ÔÚ SQL ÖУ¬JOIN ²Ù×÷·ûÓÃÓÚδÀ´×Ô¶à¸ö±íµÄ¼Í¼×éºÏÔÚÒ»Æð¡£JOIN ON ºÍ JOIN IN ÊÇÁ½ÖÖ²î±ðµÄ JOIN Óï·¨£¬¾ßÓвî±ðµÄÐÐΪ£º
JOIN ON
½«Á½¸ö±íÖÐÖª×ãÖ¸¶¨Ìõ¼þµÄ¼Í¼×éºÏÔÚÒ»Æ𡣸ÃÌõ¼þÓÉ ON ×Ó¾äÖ¸¶¨¡£
ON ×Ó¾äʹÓõÈʽ (=) ÔËËã·û½ÏÁ¿Á½¸ö±íµÄÁÐÖµ¡£
ÈôÊÇ ON ×Ó¾äÖÐÖ¸¶¨µÄÌõ¼þΪ true£¬ÔòÁ½¸ö±íÖеÄÏìÓ¦¼Í¼½«×éºÏÔÚÒ»Æð¡£
Óï·¨£ºJOIN table2 ON table1.column = table2.column
JOIN IN
½«Ò»¸ö±íÖÐÖª×ãÖ¸¶¨Ìõ¼þµÄ¼Í¼×éºÏÔÚÒ»Æ𡣸ÃÌõ¼þÓÉ IN ×Ó¾äÖ¸¶¨¡£
IN ×Ó¾äÖ¸¶¨Ò»¸ö×ÓÅÌÎÊ»òÒ»¸öÖµµÄÁÐ±í¡£
ÈôÊÇ×ÓÅÌÎÊ»òÁбíÖеÄÖµÓë±íÖеÄÁÐֵƥÅ䣬ÔòÏìÓ¦¼Í¼½«×éºÏÔÚÒ»Æð¡£
Óï·¨£ºJOIN table2 ON table1.column IN (subquery or value list)
Òªº¦Çø±ð
½ÏÁ¿ÀàÐÍ£ºJOIN ON ʹÓÃÏàµÈ½ÏÁ¿£¬¶ø JOIN IN ¿ÉÒÔʹÓÃÈκνÏÁ¿ÔËËã·û¡£
×ÓÅÌÎÊÖ§³Ö£ºJOIN IN ¿ÉÒÔʹÓÃ×ÓÅÌÎÊ£¬¶ø JOIN ON ²»¿É¡£
ÐÔÄÜ£ºÓÉÓÚ JOIN ON ʹÓÃË÷Òý£¬Í¨³£±È JOIN IN ¸ü¸ßЧ¡£
ʾÀý
JOIN ON£º
SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;
µÇ¼ºó¸´ÖÆ
JOIN IN£º
SELECT * FROM table1 JOIN table2 ON table1.id IN (SELECT id FROM table3);
µÇ¼ºó¸´ÖÆ
ÔÚµÚÒ»¸öʾÀýÖУ¬table1 ºÍ table2 ÖоßÓÐÆ¥Åä ID µÄ¼Í¼½«×éºÏÔÚÒ»Æ𡣶øÔÚµÚ¶þ¸öʾÀýÖУ¬table1 ºÍ table2 Öеļͼ½«Æ¾Ö¤ table3 ÖÐµÄ ID Öµ¾ÙÐÐ×éºÏ¡£
ÒÔÉϾÍÊÇsqlÖÐjoin onºÍjoin inµÄÇø±ðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡