mysql 关键字匹配算法 SQL 实例
2009-09-24 11:41
447 查看
import java.lang.reflect.Array;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
String[] q = { "xuyz", "test", "123" };// 关键字
String[] f = { "MC", "SM" };// 查询的字段mc=产品名,sm=产品描述
String[] s = { "4", "2" }; // 权重,mc字段匹配积分4分,sm字段匹配积2分,最后按积分排序
String[] scoreAry = new String[6];
String[] clauseAry = new String[6];
int c = 0;
for (int i = 0; i < q.length; i++) {
for (int j = 0; j < f.length; j++) {
clauseAry[c] = " ( " + f[j] + " LIKE '%" + q[i] + "%') ";
scoreAry[c] = " IF(LOCATE('" + q[i] + "', " + f[j] + "), "
+ s[j] + ", 0) ";
c++;
}
}
for (String str : clauseAry) {
System.out.println("clauseAry ::: " + str);
}
for (String str : scoreAry) {
System.out.println("scoreAry ::: " + str);
}
String score = "";
String clause = "";
for (int i = 0; i < scoreAry.length; i++) {
score = score + scoreAry[i] + "+";
}
for (int i = 0; i < clauseAry.length; i++) {
clause = clause + clauseAry[i] + " OR ";
}
String sql = "SELECT mc,sm,("
+ score.substring(0, score.length() - 1)
+ ") AS score FROM tdmgbjkzk WHERE("
+ clause.substring(0, clause.length() - 3)
+ ") ORDER BY score DESC";
System.out.print(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
String[] q = { "xuyz", "test", "123" };// 关键字
String[] f = { "MC", "SM" };// 查询的字段mc=产品名,sm=产品描述
String[] s = { "4", "2" }; // 权重,mc字段匹配积分4分,sm字段匹配积2分,最后按积分排序
String[] scoreAry = new String[6];
String[] clauseAry = new String[6];
int c = 0;
for (int i = 0; i < q.length; i++) {
for (int j = 0; j < f.length; j++) {
clauseAry[c] = " ( " + f[j] + " LIKE '%" + q[i] + "%') ";
scoreAry[c] = " IF(LOCATE('" + q[i] + "', " + f[j] + "), "
+ s[j] + ", 0) ";
c++;
}
}
for (String str : clauseAry) {
System.out.println("clauseAry ::: " + str);
}
for (String str : scoreAry) {
System.out.println("scoreAry ::: " + str);
}
String score = "";
String clause = "";
for (int i = 0; i < scoreAry.length; i++) {
score = score + scoreAry[i] + "+";
}
for (int i = 0; i < clauseAry.length; i++) {
clause = clause + clauseAry[i] + " OR ";
}
String sql = "SELECT mc,sm,("
+ score.substring(0, score.length() - 1)
+ ") AS score FROM tdmgbjkzk WHERE("
+ clause.substring(0, clause.length() - 3)
+ ") ORDER BY score DESC";
System.out.print(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- AC多模匹配算法过滤敏感词实例
- 多关键字匹配算法
- Mysql -- SQL常用命令实例
- SQL语句实例说明 方便学习mysql的朋友
- mysql 运行sql语句 order,return,check关键字报错
- mysql的sql语句中如何转义系统关键字
- Hibernate 与 MySql 数据库关键字冲突You have an error in your SQL syntax; check the manual
- SQL查询月、天、周、年(MySql的实例对比)
- mysql 字段值逗号分割后sql匹配
- Mysql 比较两个数据库的表,表字段是否匹配,升级更新找sql有用
- mysql经纬度查询并且计算2KM范围内附近用户的sql查询性能优化实例教程
- MySQL死锁问题实例分析及解决方法(主要是SQL语句可能会产生的问题)
- 在论坛中出现的比较难的sql问题:11(字符分拆 多关键字匹配问题)
- SQL关键字转换大写核心算法实现
- SQL高级语法- INNER JOIN 关键字 在表中存在至少一个匹配时,INNER JOIN 关键字返回行。
- SQL高级语法-RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
- PHP处理SQL脚本文件导入到MySQL的代码实例
- hibernate操作mysql无法实例到库中<mysql SQL Error: 1064, SQLState: 42000>
- AC多模匹配算法过滤敏感词实例
- SQL高级语法-FULL JOIN 关键字 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行。