【问题标题】:SQLite C++ Compare two tables within the same database for matching recordsSQLite C++ 比较同一数据库中的两个表以匹配记录
【发布时间】:2014-01-05 21:39:04
【问题描述】:

我希望能够使用 C++ 接口比较同一 SQLite 数据库中的两个表以匹配记录。这是我的两张桌子

表名:temptrigrams

ID          TEMPTRIGRAM       
----------  ----------  
1           The cat ran        
2           Compare two tables       
3           Alex went home
4           Mark sat down      
5           this database blows
6           data with a
7           table disco ninja 
++78

表名:spamtrigrams

ID          TRIGRAM       
----------  ----------  
1           Sam's nice ham        
2           Tuesday was cold       
3           Alex stood up
4           Mark passed out      
5           this database is
6           date with a
7           disco stew pot
++10000 

第一个表有两列和 85 条记录,第二个表有两列和 10007 条记录。

我想获取第一个表并比较 TEMPTRIGRAM 列中的记录,并将其与第二个表中的 TRIGRAM 列进行比较,然后返回表中的匹配数。因此,如果 (ID:1 'The Cat Ran' 出现在 'spamtrigrams' 中,我希望将其计数并在末尾以整数形式返回。

有人能解释一下执行此操作的查询语法吗?

谢谢。

【问题讨论】:

    标签: c++ sql database sqlite


    【解决方案1】:

    这是一个带有aggregationjoin 查询。我的猜测是你想要每个 trigram 的匹配数:

    select t1.temptrigram, count(t2.trigram)
    from table1 t1 left outer join
         table2 t2
         on t1.temptrigram = t2.trigram
    group by t1.temptrigram;
    

    如果你只想要匹配的数量:

    select count(t2.trigram)
    from table1 t1 join
         table2 t2
         on t1.temptrigram = t2.trigram;
    

    【讨论】:

    • 我在遵循您的示例时遇到了一些麻烦。我已经编辑了我的原始帖子以包含表名。您能否重申一下参考表名/列名?目前,我收到以下错误“IntelliSense:表达式必须具有算术或无范围枚举类型”。
    • 没关系 - 我设法让它工作了!再次感谢戈登。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2021-04-24
    • 2022-01-24
    相关资源
    最近更新 更多