我有一个SQL,可简化为: 8 U. p- j: Y. f, Z/ n6 B/ ySELECT * FROM table WHERE LOCATE( column,:keyword ) > 0 ORDER BY LOCATE( column,:keyword )LOCATE(column,:keyword)重复。有没有办法只计算一次?& @* A7 M6 U2 j: R% h! e. |
0 T& y1 r3 }- \; x8 l) U! m 解决方案: ; P6 Z' z4 M2 m' r1 ~7 M HAVING在MySQL别名可用于中:! C0 W( k! n4 q$ h0 m
SELECT *,LOCATE( column,:keyword ) AS somelabel FROM table HAVING somelabel > 0 ORDER BY somelabel