【问题标题】:Find all rows with a column where the ending matches a column of another table in MYSQL在MYSQL中查找具有结尾与另一个表的列匹配的列的所有行
【发布时间】:2019-01-18 08:35:07
【问题描述】:

好的,我有一个表:ad-domains 有一列,我想在另一个名为 history 的表中查找所有行,该表有 3 列:键、域、时间。

我想做一些类似于

SELECT * FROM history WHERE domain LIKE "%{ALL_DOMAIN_COLUMN_FROM_ad-domains_TABLE}"

这可能吗?

一些数据例如:

广告域:

domain
------
ads.com
moreads.com
gottahaveads.com

历史:

key      domain
-----------------------------------
1        google.com
2        subads.ads.com
3        gmail.com
4        moreads.com
5        whoa.moreads.com
6        000.hi.whoa.moreads.com
7        ads.com.taco

结果应该是

key      domain
------------------------------------
2        subads.ads.com
4        moreads.com
5        whoa.moreads.com
6        000.hi.whoa.moreads.com

【问题讨论】:

  • 您将需要JOIN LIKE 条件下的两个表。如果可能有不止一场比赛,那么您需要DISTINCT 结果。或者,如果您不允许使用JOIN,您可以使用WHERE EXISTS

标签: mysql


【解决方案1】:

您要查找的查询类似于:

SELECT * FROM history
INNER JOIN ad-domain ON history.domain LIKE CONCAT('%', ad-domain.domain, '%')

Here 是有关该主题的更多信息。

【讨论】:

  • 嗯,我从来没有想过在连接条件上使用 LIKE。这样做(几乎,只需要删除 concat 语句中的第一个 '%')。虽然在我想象的 2GB 表上运行这将花费大量时间。
猜你喜欢
  • 2020-06-09
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
相关资源
最近更新 更多