根据其他表中出现次数的差异进行查询

时间:2014-06-26 12:48:51

标签: android sqlite android-sqlite

我有3张桌子。

table_main:

    +-----+----------+-------------+
    | _id | ENTRY_ID |   DATA      |
    +-----+----------+-------------+
    |   1 | a1       | some data 1 |
    |   2 | a2       | some data 2 |
    |   3 | a3       | some data 3 |
    +-----+----------+-------------+

table_additions:

+----------+-----------+
| ENTRY_ID | TIMESTAMP |
+----------+-----------+
| a1       |    123456 |
| a2       |    123458 |
| a1       |    123654 |
| a1       |    123658 |
| a2       |    123843 |
| a3       |    123911 |
+----------+-----------+

table_deletions:

+----------+-----------+
| ENTRY_ID | TIMESTAMP |
+----------+-----------+
| a3       |    123556 |
| a2       |    123558 |
| a3       |    123754 |
| a1       |    123858 |
| a3       |    123863 |
| a3       |    123976 |
+----------+-----------+

我在android工作。我想从table_main获取所有条目的数据:

  

table_additions>中出现的次数发生的人数   table_deletions

因此,在上面的例子中,输出应该是:

+-----+----------+-------------+
| _id | ENTRY_ID |   DATA      |
+-----+----------+-------------+
|   1 | a1       | some data 1 |
|   2 | a2       | some data 2 |
+-----+----------+-------------+

1 个答案:

答案 0 :(得分:0)

这可以通过相关子查询来完成:

SELECT *
FROM table_main
WHERE (SELECT COUNT(*)
       FROM table_additions
       WHERE entry_id = table_main.entry_id
      ) >
      (SELECT COUNT(*)
       FROM table_deletions
       WHERE entry_id = table_main.entry_id
      )