【问题标题】:Finding rows with same name in MYSQL在MYSQL中查找具有相同名称的行
【发布时间】:2018-11-15 16:44:04
【问题描述】:

我有一张桌子table1a_idPK,hostname,create_dt 这里ipaddressvarcharcreate_dtdatetime

a_id    hostname              create_dt
9205    abc.com             2017-01-07 08:03:32
9206    cde                 2017-01-06 08:03:32
9207    abc2.com            2015-01-07 08:03:32

---超过1000行

我有另一个 mysql 表,其以下列 idPKnamecreated_dt: 而created_dtdatetime

id      name                 created_dt
1       abc               2017-01-07 10:03:32
2       cde.com           2017-01-07 10:03:32
3       abc2.com          2016-11-07 10:03:32
4       abc3.com          2016-11-07 10:03:32
5       abc4.com          2017-01-06 10:03:32
6       abc5.com          2015-01-06 10:03:32

我想使用hostnamename 比较table1 和table2,并从table2 中获取主机名存在于table1 中的所有行

所以从上面的例子中,我的查询应该返回

id      name                 timestamp_val
1       abc               2017-01-07 10:03:32
2       cde.com           2017-01-07 10:03:32
3       abc2.com          2016-11-07 10:03:32

我写了以下查询

SELECT *  
FROM table2.name a
INNER JOIN table1.hostname b
  where  a.name LIKE CONCAT(b.hostname, '%');

【问题讨论】:

    标签: mysql join inner-join crud outer-join


    【解决方案1】:

    你可以使用 SUBSTRING_INDEX ,我没有尝试这个查询,但下面的查询看起来像你想要的。

    试试这个:

    SELECT t2.*
    FROM table2 t1, table2 t2
    WHERE (SUBSTRING_INDEX(t1.hostname, '.', 1) = SUBSTRING_INDEX(t2.name, '.', 1))
    

    More Info

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多