1.使用自定义函数封装数据操作: 如:封装性别,查询性别时只需传入性别参数:select get_sex('1'); 创建自定义函数SQL如下: DELIMITER $$ USE `cn`$$ DROP FUNCTION IF EXISTS `get_sex`$$ CREATE DEFINER=`root`@`localhost` FUNCTION `get_sex`(sex VARCHAR(2)) RETURNS VARCHAR(2) CHARSET utf8 BEGIN DECLARE return_str VARCHAR(2) DEFAULT ''; IF (sex='1') THEN SET return_str = '男'; ELSEIF (sex='2') THEN SET return_str = '女'; ELSE SET return_str = ''; END IF; RETURN return_str; END$$ DELIMITER ; ================================================================================================= 2.调用mysql自带的函数格式化输出参数: 如:格式化输出日期时间: select DATE_FORMAT(create_date,'%y-%m-%d %H:%i') AS create_date from t_user 根据字段值做判断并格式化输出: SELECT IFNULL(money,'0.00') AS money, CASE WHEN STATUS = '1' THEN '待处理' WHEN STATUS = '2' THEN '审核拒绝' WHEN STATUS = '3' THEN '处理中' ELSE '转账成功' END AS remark, DATE_FORMAT(create_date,'%Y-%m-%d %T') AS date, transfer_type FROM t_user ============================================================================================== 3.合并查询结果并插入的SQL: INSERT INTO t_doctor_money_apply ( user_id, money, `name`, ) SELECT user_id, balance as money, `name`, FROM t_user WHERE user_id = 1 limit 1 =========================================================================================== 4.字段值算术SQL: UPDATE t_user SET read_num = read_num + 1 =========================================================================================== 5.mybatis中传参数: 表结构字段用$,如:${tableName},${field} 字符类型参数用#,如:#{userName} 整型参数用$,如: ${offset} =========================================================================================== 6.数据类型转换: select cast(user_id as char) as user_id, user_name, user_type, cast(friend_id as char) as friend_id from t_user =========================================================================================== 7.使用自定义存储过程: 如:mybatis调用存储过程并传递参数: {call t_user_procedure(?,?,?,?,?,?)} =========================================================================================== 8.left join on: 左边是主表,右边是附表,on是连接点,where接条件 SELECT ( (SELECT COUNT(1) FROM t_patient_follow_consult AS t1 LEFT JOIN t_disease AS t2 ON t2.`disease_id`=t1.`disease_id` WHERE t1.`patient_id`=#{patient_id} AND t1.`doctor_id`=#{doctor_id}) ) AS count ================================================================================== 9.数据库操作: 获取数据库的所有表名: SELECT table_name FROM information_schema.tables WHERE table_schema=#{db} 获取表的所有字段: SHOW COLUMNS FROM ${table} ================================================================================= 10.当天新记录: create_date > DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') 当月新记录: create_date > DATE_SUB(TIMESTAMP(DATE(SYSDATE())),INTERVAL DAYOFMONTH(TIMESTAMP(DATE(SYSDATE())))-1 DAY) ================================================================================= 11.按中文名的首字母排序 ORDER BY CONVERT(username USING gbk) COLLATE gbk_chinese_ci ASC ===================================================================================== 12.mysql全文检索 当检索英文时,直接检索。 当检索中文时:将中文转成unicode存储,检索时,把关键字转为unicode搜索。 如:key='俊',转成unicode为:u4FCA ,搜索的字段名为n=unicode_name,则: SELECT * FROM `user` WHERE MATCH(unicode_name)AGAINST('u4FCA' IN NATURAL LANGUAGE MODE); 中文转unicode工具类: package fullText; import java.io.UnsupportedEncodingException; public class UnicodeConverter { public static void main(String[] args) throws UnsupportedEncodingException { String s = "java工程师"; s = toEncodedUnicode(s, false); System.out.println("to unicode:\t\t" + s); System.out.println("from unicode : " + fromEncodedUnicode(s.toCharArray(), 0, s.length())); } private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; private static char toHex(int nibble) { return hexDigit[(nibble & 0xF)]; } public static String toEncodedUnicode(String theString, boolean escapeSpace) { int len = theString.length(); int bufLen = len * 2; if (bufLen < 0) { bufLen = Integer.MAX_VALUE; } StringBuffer outBuffer = new StringBuffer(bufLen); for (int x = 0; x < len; x++) { char aChar = theString.charAt(x); if ((aChar > 61) && (aChar < 127)) { if (aChar == '\\') { outBuffer.append('\\'); outBuffer.append('\\'); continue; } outBuffer.append(aChar); continue; } switch (aChar) { case ' ': if (x == 0 || escapeSpace) outBuffer.append('\\'); outBuffer.append(' '); break; case '\t': outBuffer.append('\\'); outBuffer.append('t'); break; case '\n': outBuffer.append('\\'); outBuffer.append('n'); break; case '\r': outBuffer.append('\\'); outBuffer.append('r'); break; case '\f': outBuffer.append('\\'); outBuffer.append('f'); break; case '=': // Fall through case ':': // Fall through case '#': // Fall through case '!': outBuffer.append('\\'); outBuffer.append(aChar); break; default: if ((aChar < 0x0020) || (aChar > 0x007e)) { outBuffer.append('\\'); outBuffer.append('u'); outBuffer.append(toHex((aChar >> 12) & 0xF)); outBuffer.append(toHex((aChar >> 8) & 0xF)); outBuffer.append(toHex((aChar >> 4) & 0xF)); outBuffer.append(toHex(aChar & 0xF)); outBuffer.append(' '); } else { outBuffer.append(aChar); } } } return outBuffer.toString(); } public static String fromEncodedUnicode(String str) { return fromEncodedUnicode(str.toCharArray(), 0, str.length()); } public static String fromEncodedUnicode(char[] in, int off, int len) { char aChar; char[] out = new char[len]; // 只短不长 int outLen = 0; int end = off + len; while (off < end) { aChar = in[off++]; if (aChar == '\\') { aChar = in[off++]; if (aChar == 'u') { // Read the xxxx int value = 0; for (int i = 0; i < 4; i++) { aChar = in[off++]; switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException("Malformed \\uxxxx encoding."); } } out[outLen++] = (char) value; } else { if (aChar == 't') { aChar = '\t'; } else if (aChar == 'r') { aChar = '\r'; } else if (aChar == 'n') { aChar = '\n'; } else if (aChar == 'f') { aChar = '\f'; } out[outLen++] = aChar; } } else { out[outLen++] = aChar; } } return new String(out, 0, outLen); } } ==================================================================================== 13.计算地球表面两点距离(开发附近的XX,距离,单位km) 两点坐标:A(latA,lonA) B(latB,lonB) 距离为: SELECT 12733.129728*ASIN(SQRT(POWER(SIN((latA-latB)*PI()/360),2)+COS(latA*PI()/180)*COS(latB*PI()/180)*POWER(SIN((lonA-lonB)*PI()/360),2))) AS dist 如:A(40,113.55) B(42,113.21) SELECT 12733.129728*ASIN(SQRT(POWER(SIN((40-42)*PI()/360),2)+COS(40*PI()/180)*COS(42*PI()/180)*POWER(SIN((113.55-113.21)*PI()/360),2))) AS dist ======================================================================================= 14. mybatis调用存储过程,例如: <update id="doctorapplymoney" parameterMap="applyMoneyMap" statementType="CALLABLE" > {call doctor_applymoney_procedure(?,?,?,?,?,?,?,?,?)} </update> ======================================================================================= 15.