我有一个表格,其中包含英语单词列表,我试图选择可以使用给定字符串制作的所有单词"hand"
(如在游戏拼字游戏中)选择以任意顺序包含子集字符的行
+--------+
| word |
+--------+
| test |
| father |
| woman |
| zebra |
+--------+
我到目前为止的查询只会检查手中是否存在任何字符。
SELECT * FROM words WHERE word SIMILAR to '%e%|%z%|%h%';
/* returns test, father and zebra as they all contain either e,z or h */
但是这并没有考虑到一个字是否包含字符比手多次呢,我使用python中的代码来检查单词是否有效
def isValidWord(word, hand):
"""Return true or false can the word be made using the characters in the hand"""
for i in word: # for each character in word
if hand.count(i)<word.count(i): # is the character in the hand enough times
return False
return True # if every character in the word is present in the hand
我的问题我该如何构建一个查询来检查单词中的每个字符并确保该字符的出现次数不超过字符串出现次数? 或者这不是数据库的工作吗?
在此先感谢。
这不是一个(关系)数据库的工作,要坦率地说。 –
是的,我认为可能是这种情况,我是PostgreSQL的新手,我不知道是否有一些内置的方法可以简化它的工作,唉。 –