一尘不染

将MySQL代码转换为Access:GROUP_CONCAT和三重JOIN

mysql

我很难将一段MySQL代码转换为Access。我正在尝试将Sakila(MySQL)数据库中找到的查询之一用于我正在处理的Access项目。

首先,GROUP_CONCAT函数根本不起作用。经过一些Google搜索后,我发现Access不支持此功能,但是找不到可行的替代方法。但是,可以用一些“
+”运算符代替CONCAT。

然后是三重LEFT
JOIN,它不断返回缺少的操作员错误。我找到了一篇博客文章,解释了一系列括号如何提供帮助,但是这导致了更多的麻烦,并促使我删除了括号,此后引发了更多丢失的操作员错误。

另外,似乎也未接受SEPARATOR,但这可能是由于GROUP_CONCAT无法正常工作。

有谁愿意指引我正确的方向?我为此苦苦挣扎太久了。

SELECT
a.actor_id,
a.first_name,
a.last_name,
GROUP_CONCAT(DISTINCT CONCAT(c.name, ': ',
    (SELECT GROUP_CONCAT(f.title ORDER BY f.title SEPARATOR ', ')
                FROM film f
                INNER JOIN film_category fc
                  ON f.film_id = fc.film_id
                INNER JOIN film_actor fa
                  ON f.film_id = fa.film_id
                WHERE fc.category_id = c.category_id
                AND fa.actor_id = a.actor_id
             )
         )
         ORDER BY c.name SEPARATOR '; ')
AS film_info
FROM
actor AS a
LEFT JOIN film_actor AS fa ON a.actor_id = fa.actor_id
LEFT JOIN film_category AS fc ON fa.film_id = fc.film_id
LEFT JOIN category AS c ON fc.category_id = c.category_id
GROUP BY a.actor_id, a.first_name, a.last_name

阅读 384

收藏
2020-05-17

共1个答案

一尘不染

MySQL GROUP_CONCAT()函数最常被引用的Access替代方法是Allen Browne
ConcatRelated()函数,可在此处获得

至于JOIN周围的括号,是的,Access SQL对此有些挑剔。代替

FROM
actor AS a
LEFT JOIN film_actor AS fa ON a.actor_id = fa.actor_id
LEFT JOIN film_category AS fc ON fa.film_id = fc.film_id
LEFT JOIN category AS c ON fc.category_id = c.category_id

尝试

FROM 
    (
        (
            actor AS a 
            LEFT JOIN 
            film_actor AS fa 
                ON a.actor_id = fa.actor_id
        ) 
        LEFT JOIN 
        film_category AS fc 
            ON fa.film_id = fc.film_id
    ) 
    LEFT JOIN 
    category AS c 
        ON fc.category_id = c.category_id
2020-05-17