MySQL JOIN使用子字符串作为计数器

时间:2017-05-04 11:20:25

标签: php mysql

我有两张表如下:

xyz

id    url
--    -------------
1     someweb.com/1
2     example.com
3     abc.com

abc

id    url
--    -----------
1     example.com

我期待输出为:

id    url            count
--    -----------    -----
1     example.com    2
2     abc            1

我执行此查询:

SELECT DISTINCT
    xyz.url,
    COUNT(xyz.url) as count
from      abc
LEFT JOIN xyz
on        abc.id = substring_index(xyz.url, '/someweb.com/', -1)

因为某些链接位于abc表中,该表链接为xyz字符串。

知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT
    count(*) AS count,
    u.url
FROM
    (
        SELECT
            *
        FROM
            xyz
        UNION ALL
            SELECT
                *
            FROM
                abc
    ) AS u
GROUP BY
    u.url