【发布时间】:2015-07-06 14:13:05
【问题描述】:
我有两张这样的桌子。对于给定的属性(在表 doc_attribute 中)和文件中的给定“单词”(在文档中),我想找到不包含该特定属性但在文件中包含该“单词”的不同 doc_id 的计数。
例如,如果给定的属性是相机并且'word'是移动的,那么结果应该是2。(即,第2,第3和第5个doc_id不包含相机,其中只有第2和第3个包含'word ' 手机在文件中)
table doc_attribute
+--------+-----------+--------+
| doc_id | attribute | value |
+--------+-----------+--------+
| 1 | product | mobile |
| 1 | model | lumia |
| 1 | camera | 5mp |
| 2 | product | mobile |
| 2 | model | lumia |
| 2 | ram | 1gb |
| 3 | product | mobile |
| 3 | year | 2014 |
| 3 | made-in | china |
| 4 | brand | apple |
| 4 | model | iphone |
| 4 | camera | 5mp |
| 5 | product | camera |
| 5 | brand | canon |
| 5 | price | 20000 |
table document
+--------+-----------------------------+
| doc_id | file |
+--------+-----------------------------+
| 1 | lumia 5mp mobile 1gb 2014 |
| 2 | lumia 8mp mobile 1gb 2015 |
| 3 | galaxy mobile fullhd 2gb |
| 4 | iphone apple 5mp 2013 new |
| 5 | canon 20000 new dslr 12mp |
当前查询:
select count(doc_id)
from document
where doc_id not in (select doc_id from doc_attribute
where attribute = 'camera')
and file REGEXP '[[:<:]]mobile[[:>:]]';
【问题讨论】:
-
请先发布您尝试过的SQL。
-
从 doc_id 不在的文档中选择 count(doc_id)(从 doc_attribute where attribute = 'camera' 中选择 doc_id)和文件 REGEXP '[[:<:>:]] ';
-
您当前的查询有什么问题?它返回什么不正确?您可能希望编辑原始帖子以包含您的查询和结果。比在 cmets 中添加更好。
-
这些不是我原来的表格。我的原始表格在文件中有太多行和太多内容。所以很难检查结果。无论如何,上面的查询给出了错误的答案。
-
您能否使用您在帖子中提供的示例数据重现您的问题?你测试过吗?