- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果必须处理现有的数据库结构并尝试找到一种有效的方法来根据标签选择文件。一张表包含"file",一张表包含“标签描述”,第三张表包含与文件相关的所有“标签”。
如何选择所有文件:Language = 'ENG' and Measure = 'METRIC' and Category = 'Type1'? (所以结果应该是一个文件:ID 100)。
这是相关 3 个表的简化版本:
CREATE TABLE IF NOT EXISTS `files` (
`file_id` int(11) NOT NULL DEFAULT '0',
`file_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `files` (`file_id`, `file_name`) VALUES
(100, 'testfile_1'),
(200, 'testfile_2'),
(300, 'testfile_3'),
(400, 'testfile_4');
CREATE TABLE IF NOT EXISTS `tag_parents` (
`parent_id` int(11) NOT NULL DEFAULT '0',
`parent_name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tag_parents` (`parent_id`, `parent_name`) VALUES
(1, 'Language'),
(2, 'Measure'),
(3, 'Category');
CREATE TABLE IF NOT EXISTS `tags` (
`tag_id` int(11) NOT NULL DEFAULT '0',
`file_id` int(11) DEFAULT NULL,
`tag_parent_id` int(11) DEFAULT NULL,
`tag_value` varchar(64) DEFAULT NULL,
PRIMARY KEY (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tags` (`tag_id`, `file_id`, `tag_parent_id`, `tag_value`) VALUES
(1, 100, 1, 'ENG'),
(2, 200, 1, 'ENG'),
(3, 300, 1, 'FRA'),
(4, 400, 1, 'DEU'),
(5, 100, 2, 'METRIC'),
(6, 200, 2, 'IMPERIAL'),
(7, 300, 2, 'METRIC'),
(8, 400, 2, 'IMPERIAL'),
(9, 100, 3, 'Type1'),
(10, 200, 3, 'Type3'),
(11, 300, 3, 'Type1'),
(12, 400, 3, 'Type1');
感谢任何帮助。谢谢你! (到目前为止,我的所有试验都失败了,或者它们的速度太慢了(使用子选择))。
最佳答案
这让我想到元模型,其中对象(在本例中为文件)的属性不是列,而是要在标签中查找的值。它总是比将列直接放在表中慢,但您应该能够让它合理地工作。我将 tag_parents
视为 tag_type
。以下 ( fiddle here) 应该有效:
select f.*
from files f
where exists ( -- it should have the "Category"."Type1"
select parent_id
from tag_parents categoryT, tags category
where categoryT.parent_name="Category"
and category.tag_parent_id=categoryT.parent_id
and category.tag_value="Type1"
and category.file_id=f.file_id
)
and exists ( -- as well as "Language"."ENG"
select parent_id
from tag_parents languageT, tags language
where languageT.parent_name="Language"
and language.tag_parent_id=languageT.parent_id
and language.tag_value="ENG"
and language.file_id=f.file_id
)
and exists ( -- as well as "Measure"."METRIC"
select parent_id
from tag_parents measureT, tags measure
where measureT.parent_name="Measure"
and measure.tag_parent_id=measureT.parent_id
and measure.tag_value="METRIC"
and measure.file_id=f.file_id
)
您可以通过定义一些 View 来简化您的生活,例如 Category
、Language
和 Measure
(以及任何其他 tag_parents
你会有)。这将使查询更具可读性。使用类别、度量和语言三个 View ,您可以编写:
select * from files f, category c, measure m, `language` l
where f.file_id=c.file_id and c.value="Type1"
and f.file_id=l.file_id and l.value="ENG"
and m.file_id=l.file_id and m.value="METRIC";
或者甚至更好,如果你有固定数量的这些属性,你可以定义一个类似于顶部查询的 View ,但使用外部连接而不是存在,具有可为空的列类别,措施和语言:
create view filesView (file_id, category, measure, `language`) as
select f.file_id, c.tag_value, m.tag_value, l.tag_value
from files f
left outer join (tags c, tag_parents ct) on c.file_id=f.file_id
and c.tag_parent_id=ct.parent_id
and ct.parent_name="Category"
left outer join (tags l, tag_parents lt) on l.file_id=f.file_id
and l.tag_parent_id=lt.parent_id
and lt.parent_name="Language"
left outer join (tags m, tag_parents mt) on m.file_id=f.file_id
and m.tag_parent_id=mt.parent_id
and mt.parent_name="Measure";
然后你可以这样写:
select file_id, category, measure, `language`
from filesView
where category="Type1"
and `language`="ENG"
and measure="METRIC";
(抱歉有点跑题了。)
关于php - MySQL 查询基于多个标签检索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462825/
如果我使用下面的代码,数据将为零 dispatch_async(dispatch_get_global_queue(0,0), ^{ UIImage *img = [[UIImage allo
fread来自 data.table包一般可以在读取文件时自动确定列分隔符( sep )。 例如,这里fread自动检测 |作为列分隔符: library(data.table) fread(past
因此,如果我有一个如下所示的数据框: A B C rowname1 4.5 4 3.2 rowname2 3 23
我有一个汽车模型的搜索数据库:“日产Gtr”,“Huynday Elantra”,“Honda Accord”等。 现在我还有一个用户列表和他们喜欢的汽车类型 user1喜欢:carId:1234,c
我正在使用 Javamail 来获取一些电子邮件数据。我将用户输入作为电子邮件 ID、imap 地址和密码并连接到 imap。然后我监视收件箱的电子邮件并查明此人是否在“收件人”或“抄送”中。 Ema
我有一些数据,我想根据差距统计来评估最佳簇数。 我阅读了 gap statistic 上的页面在 r 中给出了以下示例: gs.pam.RU Number of clusters (method '
我有一个用户名和密码组合,我将使用它通过 java 代码访问安全服务器。 我的想法是: 在外部存储加密凭据 执行时提示用户输入解密密码 在使用前将解密的凭据直接存储在字符数组中 使用凭据连接到数据库
这是 Firebase 数据:[Firebase 数据][1] 我必须从员工那里检索所有字段并将其存储在一个数组中。 现在数据更改 toast 消息即将到来,但已经很晚了。 Firebase.setA
我是 iOS 的新手,正在开发一个基本的应用程序,它目前正在使用 SSKeychain 和 AFNetworking 与 API 进行交互。当您使用我检索的应用程序登录并在我的 CredentialS
编辑:这个问题已经在 apphacker 和 ConcernedOfTunbridgeWells 的帮助下得到解决。我已更新代码以反射(reflect)我将使用的解决方案。 我目前正在编写一个群体智能
我是 C 的新手,我想编写一个程序来检查用户输入的单词是否合法。我已经在 stackoverflow 上搜索了建议,但很多都是针对特定情况的。请在我被激怒之前,我知道这个语法不正确,但正在寻找一些关于
我相信你们中的一些人编写过 C# 类,这些类必须从数据库设置密码/从数据库获取密码。 我假设敏感细节不会以明文形式显示。处理此类数据的推荐程序是什么?检索到的文本是否加密?您是否将 pws 存储在加密
我在 linux 上使用 2.7 之前的 python 版本,想知道如何检索 RUID? 2.7 及更高版本从 os 包中获得了 getresuid,但我似乎找不到 2.6 的等效项 最佳答案 您可以
我已经在 Android 中实现了一个存储对象的标准 LRUCache。每个键都是与存储的对象关联的唯一 ObjectId。我的问题是从缓存中检索对象的唯一方法是通过 ObjectId(无迭代器)。实
这已经被问过很多次了。解决方案(对我有用)是从 packages.config 文件(这就足够了)和 packages 文件夹中删除 *** 包。 这对我来说是一个糟糕的解决方案,因为每次我想安装一些
我有以下文字: #{king} for a ##{day}, ##{fool} for a #{lifetime} 以及以下(损坏的)正则表达式: [^#]#{[a-z]+} 我想匹配所有#{word
我正在寻找一种快速(如高性能,而不是快速修复)解决方案来持久化和检索数千万个小型(大约 1k)二进制对象。每个对象都应该有一个用于检索的唯一 ID(最好是 GUID 或 SHA)。额外的要求是它应该可
有没有办法获取 RegInit 的重置值?通过探测产生的类型的成员?我可以看到 RegInit 将返回类型(例如 UInt )。例如,我将有一个寄存器,我想通过 regmap 对其进行控制。 val
Iv 目前接手了一个项目,其中开发人员在某些表的 json 数组列中存储了 has many 关系。 产品表 ---------------------------- id | product | c
Git 会在任何地方记录推送到远程的历史吗? 我注意到我们能够在 Microsoft VSTS 中查看 Git 存储库的推送历史记录以及每次推送的相关提交。它甚至显示旧的、过时的提交,由于后来的强制推
我是一名优秀的程序员,十分优秀!