我有两个表,需要使用两个相似的列来连接它们。
第一个名为article的表有一个名为'slug'的列,其中包含文章的slug,ex:'trump-failed-yet-again。'
第二个表名为log,并且有一个名为path的列,其中包含文章的url路径,例如:'/ articles / trump-failed-yet-again /'
这是我的搜索查询:
"SELECT articles.title, count(*) as num FROM articles, log WHERE articles.slug LIKE CONCAT('%',log.path) GROUP BY articles.title;"
这只返回括号,[]
我也尝试过:
"SELECT articles.title, count(*) as num FROM articles JOIN log ON articles.slug SIMILAR TO CONCAT('%',log.path) GROUP BY articles.title;"
返回DataError:无效的正则表达式:量词操作数无效
非常感谢任何帮助!
答案 0 :(得分:1)
路径末尾有一个斜杠。怎么样?
SELECT a.title, count(*) as num
FROM articles a JOIN
log l
ON a.path LIKE '%' || l.slug || '%'
GROUP BY a.title;
您还应该学会使用正确的,明确的JOIN
语法。 从不在FROM
子句中使用逗号。
答案 1 :(得分:0)
因为这有1:1的功能,你可以这样做
SELECT articles.title, count(*) as num
FROM articles
JOIN log ON articles.slug = '/articles/' || articles.slug || '/'
GROUP BY articles.title;
甚至更好
CREATE FUNCTION slug_to_article_path( slug text )
RETURNS text AS
$$
SELECT '/articles/' || slug || '/';
$$ LANGUAGE sql
IMMUTABLE;
SELECT articles.title, count(*) as num
FROM articles
JOIN log ON articles.slug = slug_to_article_path(articles.slug)
GROUP BY articles.title;
答案 2 :(得分:0)
试试这个:
` select articles.title, count(*) as views from articles
join log on articles.slug ~~ ('%' || articles.slug || '%')
group by articles.title;`