java - 我需要一个 SQL 查询来找到你可以用一组字母组成的所有单词,包括最多两个空白 block

标签 java android sql regex discrete-mathematics

我有一个名为字典的数据库表,当前包含所有字典条目的以下字段:

public static final String COLUMN_NAME_UID = "_id_";
public static final String COLUMN_NAME_WORD = "word";
public static final String COLUMN_NAME_WORD = "wordSorted";
public static final String COLUMN_NAME_WORD_LENGTH = "length";
public static final String COLUMN_NAME_COUNT_A = "count_A";
public static final String COLUMN_NAME_COUNT_B = "count_B";
public static final String COLUMN_NAME_COUNT_C = "count_C";
public static final String COLUMN_NAME_COUNT_D = "count_D";
public static final String COLUMN_NAME_COUNT_E = "count_E";
public static final String COLUMN_NAME_COUNT_F = "count_F";
public static final String COLUMN_NAME_COUNT_G = "count_G";
public static final String COLUMN_NAME_COUNT_H = "count_H";
public static final String COLUMN_NAME_COUNT_I = "count_I";
public static final String COLUMN_NAME_COUNT_J = "count_J";
public static final String COLUMN_NAME_COUNT_K = "count_K";
public static final String COLUMN_NAME_COUNT_L = "count_L";
public static final String COLUMN_NAME_COUNT_M = "count_M";
public static final String COLUMN_NAME_COUNT_N = "count_N";
public static final String COLUMN_NAME_COUNT_O = "count_O";
public static final String COLUMN_NAME_COUNT_P = "count_P";
public static final String COLUMN_NAME_COUNT_Q = "count_Q";
public static final String COLUMN_NAME_COUNT_R = "count_R";
public static final String COLUMN_NAME_COUNT_S = "count_S";
public static final String COLUMN_NAME_COUNT_T = "count_T";
public static final String COLUMN_NAME_COUNT_U = "count_U";
public static final String COLUMN_NAME_COUNT_V = "count_V";
public static final String COLUMN_NAME_COUNT_W = "count_W";
public static final String COLUMN_NAME_COUNT_X = "count_X";
public static final String COLUMN_NAME_COUNT_Y = "count_Y";
public static final String COLUMN_NAME_COUNT_Z = "count_Z";

我希望能够搜索实例 test* 并找到所有可以由“t”、“e”、“s”、“t”和通配符组成的单词,例如“tests”(s是通配符),“setts”(s 是通配符),“set”,“tet”,“es”,“te”,“best”(b 是通配符),等等......任何你可以用任何组合制作的东西那些字母。

我试过这样的方法,但是这个例子只找到没有通配符的四个字母的单词:

SELECT * FROM dictionary WHERE 

count_E=1 AND
count_S=1 AND
count_T=2 

SELECT * FROM dictionary WHERE  length <=4

这会产生:

"137075"    "sett"  "estt"
"145808"    "stet"  "estt"
"153675"    "test"  "estt"
"153851"    "tets"  "estt"

现在,我知道,这本质上是一个谨慎的数学问题。

这里是我如何得到所有 5 个字母的单词和一个空格以及最后一个查询中提供的所有字母:

SELECT * FROM dictionary WHERE 

count_E=1 AND
count_S=1 AND
count_T=2 

INTERSECT 
SELECT * FROM dictionary WHERE  length <=5

结果:

"97705"     "netts" "enstt"
"137075"    "sett"  "estt"
"145250"    "state" "aestt"
"145808"    "stet"  "estt"
"152303"    "taste" "aestt"
"152333"    "tates" "aestt"
"152632"    "teats" "aestt"
"153361"    "tents" "enstt"
"153675"    "test"  "estt"
"153676"    "testa" "aestt"
"153733"    "testy" "estty"
"153769"    "teths" "ehstt"
"153851"    "tets"  "estt"
"153874"    "texts" "esttx"
"156575"    "totes" "eostt"
"157952"    "trets" "erstt"
"172060"    "yetts" "estty"

但是,我必须遍历字母组合的所有迭代才能获得所有隐藏的子词...谁能帮我想出一种更优雅的方法来从查询和最多两个通配符?我也知道您可以在 SQL 中使用 REGEXP,所以这可能是一种方式。我现在还不知道,我正在将这个问题带到 hive 中......

是否有一个查询,或一系列查询,或交叉、连接等...可以帮助我解决这个问题?

更新 我想我可能偶然发现了这一点,但不确定它是否正常工作。任何帮助将不胜感激:

SELECT * FROM dictionary WHERE 
(
count_E<=1 AND
count_S<=1 AND
count_T<=1 
)
INTERSECT SELECT * FROM dictionary WHERE length =(count_E+count_S+count_T+1)     ORDER BY length

+1 是占一个空格。对于两个,我正在考虑只做一个 +2,等等...... +0 就是那些字母,以及你可以从中得到的任何东西。

最佳答案

您必须执行以下操作,将表的所有字段串联起来,如下所示:

concatenacion = "(_id||' '||Desc_art||' '||Nom_proveedor||' '||marca) like '"+resultado+"'" +
            "OR (_id||' '||Nom_proveedor||' '||marca||' '||Desc_art) like '"+resultado+"'" +
            "OR (marca||' '||Nom_proveedor||' '||Desc_art||' '||_id) like '"+resultado+"'" +
            "OR (marca||' '||Nom_proveedor||' '||_id||' '||Desc_art) like '"+resultado+"'" +
            "OR (Desc_art||' '||Nom_proveedor||' '||marca||' '||_id) like '"+resultado+"'" +
            "OR (Desc_art||' '||_id||' '||Nom_proveedor||' '||marca) like '"+resultado+"'";

然后提出你的请求,在 WHERE 子句中必须把你的连接例如:

cursor=bd.rawQuery("select _id, Desc_art, cant_art, Desc_bulto, precio"+getDefaultNroLista(codcliente)+", tiene_imagen,marca from listas_precios where "+concatenacion+" ORDER BY Desc_art ASC", null);

我的工作很好,希望有用

关于java - 我需要一个 SQL 查询来找到你可以用一组字母组成的所有单词,包括最多两个空白 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344108/

相关文章:

java - 如何使用自动发现在 Jersey 中启用自动 JSON 编码?

java - 为什么我从 MouseAdapter 中的 getSource 方法收到此错误?

java - Linux - Android Studio 1.1 Beta 3 - 未找到默认 Activity

mysql转储: Got error: 1017: Can't find file: './access.frm' (errno: 13) when using LOCK TABLES

java - 与通过 android studio 安装相比,作为 APK 安装时,Retrofit 的行为不同且无法解析

java - 所有实例都相等的类的 HashCode 实现

java - 将数据从广播接收器发送到正在运行的服务

android - Android Studio 中的 "cannot resolve symbol R"

sql - Union All 但只保留 T-SQL 中一个表中的重复项

mysql - 将另一个可变长度列添加到具有可变长度行的表中是否会进一步影响性能?