【问题标题】:Mysql: Counting a matching bulk between two sets of values within multiple tables?Mysql:计算多个表中两组值之间的匹配批量?
【发布时间】:2012-03-23 14:38:30
【问题描述】:

有没有办法使用 COUNT() AS number_matches 对表之间的大量匹配行进行计数?

我正在考虑包含某种满足搜索算法的循环方法,但在我这样做之前,我想知道/希望有一种更简单的方法。

TABLE_1

  • 第 1 行:

    number_1) 1

    number_2) 2

    number_3) 3

    number_4) 4

  • 第 2 行:

    number_1) 2

    number_2) 4

    number_3) 5

    number_4) 6

比较

TABLE_2

  • 第 1 行:

    number_1) 2

    number_2) 4

    number_3) 6

    number_4) 8

结果

  • 第 1 行:

    number_matches) 2

  • 第 2 行:

    number_matches) 3

【问题讨论】:

  • 您的示例是在每个表中显示行吗?这些是行还是列?
  • 它们是列,我已将示例更清楚地说明。很抱歉造成混乱。
  • 每张表都有很多行,对吧?
  • 它满足了一个抽奖游戏场景,所以 TABLE_2 应该返回 1 行,用 TABLE_1 中的许多行进行测试

标签: mysql search loops


【解决方案1】:

鉴于你的例子,我认为你想计算一个连接的记录数量,对吧?

select count(*) from table_1 t1
join table_2 t2 on t1.col = t2.col

【讨论】:

  • 或者对于我们这些鄙视简单查询的连接语法的人:select count(*) from table_1 t1, table_2 t2, where t1.col = t2.col
  • @AndrewEdvalson:您使用的语法是从 JOIN 中的引擎转换而来的;一般来说,在更复杂的查询中使用 JOIN 而不是你的语法总是更好,因为这可以避免表的笛卡尔积导致大量查询!
  • 大声笑,只是在我的答案 cmets 中没有 SQL 反模式参数:P
  • number_* 是一列,如果 number_1 = number_2... number_2 = number_4 等呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多