使用CTE的SQL错误

时间:2016-05-01 16:44:48

标签: php mysql sql mysqli common-table-expression

我收到了这个错误:

查询错误:您的SQL语法出错;检查与您的MariaDB服务器版本对应的手册,以便在'; WITH convs AS附近使用正确的语法(在第1行选择c.id,c.title,c.seen,c.id_receiver,c.id_send'

当我使用此查询时:

$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子句中使用别名。你可以看到我有很多。

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

2 个答案:

答案 0 :(得分:5)

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的列。

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

答案 1 :(得分: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