在MYSQL中查找同名行

在MYSQL中查找同名行,mysql,join,inner-join,crud,outer-join,Mysql,Join,Inner Join,Crud,Outer Join,我有一个表table1a\u id作为PK,hostname,create\u dt 这里的ipaddress是varchar,create\u dt是datetime 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 2

我有一个表
table1
a\u id
作为
PK
hostname
create\u dt
这里的
ipaddress
varchar
create\u dt
datetime

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
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
---1000多行

我有另一个mysql表,其中包含以下列
id
作为
PK
name
,以及
created\u dt
: 而
created\u dt
datetime

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
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
我想使用
hostname
name
比较表1和表2,并将
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, '%');

您可以使用
子字符串\u INDEX
,我没有尝试此查询,但下面的查询看起来像您想要的

试试这个:

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