您的位置:首页 > 数据库

关于 msql 使用过程中的总结

2016-03-30 20:31 281 查看
1. 关于在文本中过滤 查询 例如表 t 中 有一个 context 字段 , 文本格式为 消耗{0},得到{1}

SELECT
SUBSTR(T.str,0,LOCATE("_",T.str)) as a1,
SUBSTR(T.str,LOCATE("_",T.str),CHAR_LENGTH(T.str)-LOCATE("_",T.str)) as a2

FROM(SELECT
`REPLACE` (
`REPLACE` (t.context, "消耗", ""),
"得到",
"_"
) as str
FROM
t) AS T


SUBSTR(str FROM pos FOR len)   # str 要切割的子串, pos 切割开始位置 ,len 从切割位置开始往后数几位
CHAR_LENGTH(str)               # 获得 str 的长度
LOCATE(substr,str)                         # substr 在 str 中的位置
`REPLACE`(str,from_str,to_str) # 替换字符串


2. 查询需要按照一定的格式

比如需求中有对于 一个查询结果的汇总 , T 表示一个查询结果集合

(T)
UNION ALL
(
SELECT
"Total:"
FROM T
)


3.关于多表的合并查询



使用这样的格式,看起来也直观,修改起来也方便

4. 要学会使用各种SQL 美化工具

SELECT * FROM a WHERE b.c like '1'; # 美化前
### 美化后
SELECT
*
FROM
a
WHERE
b.c LIKE '1';


5 .要习惯使用 # 来注释 美化后的sql

6.下面给大家分享一个 mysql 美化后的sql 怎么合并为一行的 工具代码

public class Test {
static final String Path = ""; // sql 文件位置
/**
* 工具类将 mysql 美化的sql 转换为一行显示
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(new File(Path))));
StringBuilder sb = new StringBuilder();
String str = is.readLine();
while(str!=null)
{
sb.append(str);
str = is.readLine();
}

str = sb.toString();
str=str.replace("\n", " ");
str=str.replaceAll("\t+", " ");
str=str.replace("{ ", "{");
str=str.replace(" }", "}");
is.close();
System.out.println(str);
}
}


分享完毕!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: