1. 关于在文本中过滤 查询 例如表 t 中 有一个 context 字段 , 文本格式为 消耗{0},得到{1}
1 SELECT 2 SUBSTR(T.str,0,LOCATE("_",T.str)) as a1, 3 SUBSTR(T.str,LOCATE("_",T.str),CHAR_LENGTH(T.str)-LOCATE("_",T.str)) as a2 4 5 FROM(SELECT 6 `REPLACE` ( 7 `REPLACE` (t.context, "消耗", ""), 8 "得到", 9 "_" 10 ) as str 11 FROM 12 t) AS T
1 SUBSTR(str FROM pos FOR len) # str 要切割的子串, pos 切割开始位置 ,len 从切割位置开始往后数几位 2 CHAR_LENGTH(str) # 获得 str 的长度 3 LOCATE(substr,str) # substr 在 str 中的位置 4 `REPLACE`(str,from_str,to_str) # 替换字符串
2. 查询需要按照一定的格式
比如需求中有对于 一个查询结果的汇总 , T 表示一个查询结果集合
1 (T) 2 UNION ALL 3 ( 4 SELECT 5 "Total:" 6 FROM T 7 )
3.关于多表的合并查询
使用这样的格式,看起来也直观,修改起来也方便
4. 要学会使用各种SQL 美化工具
1 SELECT * FROM a WHERE b.c like '1'; # 美化前 2 ### 美化后 3 SELECT 4 * 5 FROM 6 a 7 WHERE 8 b.c LIKE '1';
5 .要习惯使用 # 来注释 美化后的sql
6.下面给大家分享一个 mysql 美化后的sql 怎么合并为一行的 工具代码
1 public class Test { 2 static final String Path = ""; // sql 文件位置 3 /** 4 * 工具类将 mysql 美化的sql 转换为一行显示 5 * @param args 6 * @throws IOException 7 */ 8 public static void main(String[] args) throws IOException { 9 10 BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(new File(Path)))); 11 StringBuilder sb = new StringBuilder(); 12 String str = is.readLine(); 13 while(str!=null) 14 { 15 sb.append(str); 16 str = is.readLine(); 17 } 18 19 str = sb.toString(); 20 str=str.replace("\n", " "); 21 str=str.replaceAll("\t+", " "); 22 str=str.replace("{ ", "{"); 23 str=str.replace(" }", "}"); 24 is.close(); 25 System.out.println(str); 26 } 27 }
分享完毕!