SQL 使用 CTE 时出错

SQL error using a CTE

我遇到了这个错误:

查询错误:您的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在 ';WITH convs AS ( select c.id, c.title, c.seen, [=34= 附近使用的正确语法]_receiver, c.id_send' 在第 1 行

当我使用这个查询时 :

$query = ";WITH convs AS (
        select c.id, c.title, c.seen, c.id_receiver, c.id_sender
        from conversations c
        )
        select id, title, seen, id_receiver, id_sender
        from convs
        where id_receiver = '5'
        order by title desc limit 0,25";

$res = mysqli_query($connection ,$query);

我错过了什么吗? 非常感谢您的帮助。

PS :我最小化了查询以使其在此上下文中变得简单,如果您帮助我找到解决方案,我可能会遇到完整查询的另一个问题。所以我可能会回来找你寻求更多帮助。提前致谢。

编辑(整个查询)

$query = "WITH convs AS (
        select c.id, c.title, c.seen, c.id_receiver, c.id_sender,
        (select max(date) from messages where id_conversation = c.id and id_user <> '$iduser') as last_msg,
        (select top 1 id_user from messages where id_conversation = c.id and id_user <> '$iduser' order by date desc) as last_user,
        (select count(distinct id_user) from messages where id_conversation = c.id) as nbruser,
        (select count(*) from messages where id_conversation = c.id) as nbrmsg,
        (select username from users where id = c.id_sender) as sender, (select username from users where id = c.id_receiver) as receiver,
        (select count(*) from deleted_conversations where id_user='$iduser' and id_conversation=c.id) as deleted,
        from conversations c
        )
        select id, title, seen, id_receiver, id_sender, receiver, sender, last_msg, last_user, deleted, nbruser, nbrmsg
        from convs
        where (id_receiver = '$iduser' or (id_sender == '$iduser' and nbruser > 1)) and deleted = 0
        order by last_msg desc limit $pageLimit,$REC_PER_PAGE";

促使我使用 CTE 的原因是需要在 where 子句中使用别名。正如你所看到的,我有很多。

能否举例说明如何使用 views/temporary 表来实现我的目的?

MySQL/MariaDB 不支持 CTE。另外,在这种情况下完全没有必要:

    select id, title, seen, id_receiver, id_sender
    from conversations c
    where id_receiver = '5'
    order by ?? desc
    limit 0, 25;

注意:您还需要为 order by 指定列。

对于更复杂的示例,您可以使用子查询、视图、and/or 临时表。

CTE 与派生表非常相似:

select id, title, seen, id_receiver, id_sender, receiver, sender, last_msg, last_user, deleted, nbruser, nbrmsg
FROM
 (
        select c.id, c.title, c.seen, c.id_receiver, c.id_sender,
        (select max(date) from messages where id_conversation = c.id and id_user <> '$iduser') as last_msg,
        (select top 1 id_user from messages where id_conversation = c.id and id_user <> '$iduser' order by date desc) as last_user,
        (select count(distinct id_user) from messages where id_conversation = c.id) as nbruser,
        (select count(*) from messages where id_conversation = c.id) as nbrmsg,
        (select username from users where id = c.id_sender) as sender, (select username from users where id = c.id_receiver) as receiver,
        (select count(*) from deleted_conversations where id_user='$iduser' and id_conversation=c.id) as deleted,
        from conversations c
) as convs
where (id_receiver = '$iduser' or (id_sender == '$iduser' and nbruser > 1)) and deleted = 0
order by last_msg desc limit $pageLimit,$REC_PER_PAGE