【问题标题】:SQL error using a CTE使用 CTE 的 SQL 错误
【发布时间】:2016-05-01 16:44:48
【问题描述】:

我收到此错误:

查询错误:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在 ';WITH convs AS ( select c.id, c.title, c.seen, c.id_receiver, c.id_send' at line 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 子句中使用别名。正如你所看到的,我有很多。

你能举个例子说明如何使用视图/临时表来实现我的目的吗?

【问题讨论】:

  • 我猜我们缺少 order by 子句。你直接写了 desc。
  • @I_am_Batman 在我的实际代码中,它存在,但仍然无法正常工作。我肯定只是在这里打错了(我编辑了问题)。
  • 这仍然是您在 order by 之后没有给出列名来排序的错字吗?
  • MySQL 不支持 CTE。

标签: php mysql sql common-table-expression


【解决方案1】:

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 指定列。

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

【讨论】:

  • 不知道 MySQL/MariaDB 不支持 CTE。你是对的,在这种 minimized 的情况下,它是没有用的。但是,我的实际查询更复杂。在我的问题中查看编辑部分。
【解决方案2】:

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

【讨论】:

  • 我很高兴在 mysql 中有另一种方法可以做到这一点。但是,我现在有这个错误:Query Error : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from conversations c ) as convs where deleted = 0 and (id_' at line 11
  • 现在它就像一个魅力,感谢您非常有用的回答。问题是别名删除后的逗号:as deleted,
猜你喜欢
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多