mysqlÖÐfromºóÃæÄܲ»¿É¸úcase
Êǵģ¬mysqlÔÊÐíÔÚfrom×Ó¾äÖÐʹÓÃcase±í´ïʽÀ´Ñ¡ÔñÌض¨ÐУ¬Ó﷨Ϊ£ºselect … from table_name where and case when then when then else end as £¬ÆäÖÐ
MySQL FROM ºóÃæÄÜ·ñʹÓà CASE ±í´ïʽ£¿
ÃÕµ×£º
¿ÉÒÔ
ÏêϸڹÊÍ£º
MySQL ÔÊÐíÔÚ FROM ×Ó¾äÖÐʹÓà CASE ±í´ïʽÀ´´Ó±íÖÐÑ¡ÔñÌض¨ÐС£CASE ±í´ïʽʵÖÊÉÏÊÇÒ»¸öÌõ¼þÓï¾ä£¬¿ÉÒÔƾ֤¸ø¶¨µÄÌõ¼þ·µ»Ø²î±ðµÄÖµ¡£
Óï·¨£º
SELECT ... FROM table_name WHERE <filter_condition> AND CASE WHEN <condition1> THEN <value> WHEN <condition2> THEN <value> ELSE <default_value> END AS <alias></alias></default_value></value></condition2></value></condition1></filter_condition>
µÇ¼ºó¸´ÖÆ
ÆäÖУº
ÊÇÔÚÓ¦Óà CASE ±í´ïʽ֮ǰ±ØÐèÖª×ãµÄÌõ¼þ¡£
ºÍ ÊÇÒªÆÀ¹ÀµÄÌõ¼þ¡£
ÊÇƾ֤Ìõ¼þ·µ»ØµÄÖµ¡£
ÊÇÈôÊÇûÓÐÌõ¼þÆ¥Åäʱ·µ»ØµÄĬÈÏÖµ¡£
ÊÇΪ CASE ±í´ïʽЧ¹û·ÖÅɵÄÓÖÃû¡£
ʾÀý£º
¼ÙÉèÓÐÒ»¸ö “customers” ±í£¬°üÀ¨ÒÔÏÂÁУº
customer_id
name
country
ÎÒÃÇ¿ÉÒÔʹÓà CASE ±í´ïʽ´Ó±íÖÐÑ¡ÔñÀ´×ÔÌض¨¹ú¼Ò/µØÇøµÄ¿Í»§£º
SELECT * FROM customers WHERE country = 'United States' AND CASE WHEN age > 18 THEN 'Adult' WHEN age <p>´ËÅÌÎʽ«·µ»ØËùÓÐÀ´×ÔÃÀ¹úµÄ¿Í»§£¬²¢Æ¾Ö¤ÄêË꽫ËûÃÇ·ÖÀàΪ³ÉÄêÈË¡¢Î´³ÉÄêÈË»òδ֪¡£</p>
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇmysqlÖÐfromºóÃæÄܲ»¿É¸úcaseµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡