加入字符串的一部分

时间:2012-10-25 07:46:03

标签: mysql join

我有以下表格:

**visitors**
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| visitors_id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| visitors_path       | varchar(255) | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+

**fedora_info**
+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| pid            | varchar(255) | NO   | PRI |         |       |
| owner_uid      | int(11)      | YES  |     | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

首先,我通过以下方式查找与特定页面相关的visitors_path

SELECT visitors_id, visitors_path
FROM visitors
WHERE visitors_path REGEXP '[[:<:]]fedora/repository/.*:[0-9]+$';

以上查询返回预期结果。

上面查询中的

现在.*:[0-9]+在第二个表中引用了pid。现在我想知道在第二个表中按owner_uid分组的上述查询中的结果计数。

我如何加入这些表?

修改

示例数据:

visitors
+-------------+---------------------------------+
| visitors_id | visitors_path                   |
+-------------+---------------------------------+
|        4574 | fedora/repository/islandora:123 |
|        4575 | fedora/repository/islandora:123 |
|        4580 | fedora/repository/islandora:321 |
|        4681 | fedora/repository/islandora:321 |
|        4682 | fedora/repository/islandora:321 |
|        4704 | fedora/repository/islandora:321 |
|        4706 | fedora/repository/islandora:456 |
|        4741 | fedora/repository/islandora:456 |
|        4743 | fedora/repository/islandora:789 |
|        4769 | fedora/repository/islandora:789 |
+-------------+---------------------------------+

fedora_info
+-----------------+-----------+
| pid             | owner_uid |
+-----------------+-----------+
| islandora:123   |         1 |
| islandora:321   |         2 |
| islandora:456   |         3 |
| islandora:789   |         4 |
+-----------------+-----------+

Expected result:
+-----------------+-----------+
| count           | owner_uid |
+-----------------+-----------+
| 2               |         1 |
| 4               |         2 |
| 3               |         3 |
| 2               |         4 |
| 0               |         5 |
+-----------------+-----------+

2 个答案:

答案 0 :(得分:1)

我建议您规范化您的数据库。在前端语言的visitors提取pid中插入行并将其放在单独的列中(例如fi_pid)。然后你可以轻松加入。

以下查询可能适合您。但它会很少cpu密集。

SELECT 
       COUNT(a.visitors_id) as `count`,
       f.owner_uid
FROM   (SELECT visitors_id, 
               visitors_path, 
               SUBSTRING(visitors_path, ( LENGTH(visitors_path) - 
                                          LOCATE('/', REVERSE(visitors_path)) ) 
                                        + 2) AS 
                      pid 
        FROM   visitors 
        WHERE  visitors_path REGEXP '[[:<:]]fedora/repository/.*:[0-9]+$') AS `a`

JOIN fedora_info AS f 
         ON ( a.pid = f.pid ) 

GROUP  BY f.owner_uid 

答案 1 :(得分:0)

以下查询返回预期结果,但其速度非常慢Query took 9.6700 sec

SELECT COUNT(t2.pid), t1.owner_uid
FROM fedora_info t1
JOIN (SELECT TRIM(LEADING 'fedora/repository/' FROM visitors_path) as pid
FROM visitors
WHERE visitors_path REGEXP '[[:<:]]fedora/repository/.*:[0-9]+$') t2 ON t1.pid = t2.pid
GROUP BY t1.owner_uid