0

How do I retrieve this: 123 from this: 123?arg=value#ancor, in MYSQL; in other words, removing queries and removing anchors from the URL path.

Example scenario:

table1:
+----+------+
| id | path |
+----+------+
|  1 |  100 |
|  2 |  200 |
|  3 |  300 |
+----+------+

table2:
+----+----------------+
| id |           path |
+----+----------------+
|  1 |     100#anchor |
|  2 |  200?arg=value |
|  3 |      other/300 |
+----+----------------+

SELECT * FROM table1
INNER JOIN table2
ON table1.path = revised_path(table2.path)

result:
+----+------+----+---------------+
| id | path | id |          path |
+----+------+----+---------------+
|  1 |  100 |  1 |    100#anchor |
|  2 |  200 |  2 | 200?arg=value |
+----+------+----+---------------+

UPDATE: Path will always be numeric, but can be any length.

UPDATE 2: revised_path() is a temporary replacement for the solution I'm looking for.

4

2 回答 2

0

据我了解,您需要检查路径是否匹配,但由于一个包含在其他数据中,您应该检查表一中的路径(id)是否包含在表二中的路径中,我会这样做

SELECT table_1.id as t1_id,table_1.path as t1_path,
table_2.id as t2_id, table_2.path as t2_path
FROM table_1
INNER JOIN table_2
WHERE INSTR(table_2.path,table_1.path) > 0

编辑:

这应该为您提供您要求的输出:

SELECT table_1.id as t1_id,table_1.path as t1_path,
table_2.id as t2_id, table_2.path as t2_path
FROM table_1
INNER JOIN table_2
where table_1.path = cast(table_2.path as unsigned)
于 2012-07-02T22:02:35.350 回答
0

所以看起来路径是 URL 的一部分。所以 PHP 函数 parse_url 可以解决问题。

$sql = "SELECT * FROM table1 INNER JOIN table2 ON table1.path = revised_path(table2.path)";
$result = mysql_query($sql);
while ($data = mysql_fetch_object)) {
    $path = parse_url($data->path);
}

// more code to diplay $path etc...

parse_url上的 PHP 文档

于 2012-07-02T21:54:22.443 回答