简体   繁体   中英

Error in executing query in Phalcon but query works on phpMyAdmin

I have following table structure

        headline
        is_user_article
        is_approved
        updated_date
        created_date  

I want to sort record in such a way that

  1. if is_approved = 0, sort by is_user_article DESC, updated_date DESC, created_date DESC
  2. if is_approved = 1, sort by updated_date DESC, created_date DESC

That means I want those articles at first which are user submitted(is_user_article=1) and is not approved (is_approved=0), then rest of the records ordered by updated date and created date.

I have following query

<?php
        $phql = "SELECT headline, is_user_article, created_date, updated_date, is_approved 
            FROM NewsRoom\Articles\Models\Articles ORDER BY
            CASE is_approved WHEN 0 THEN is_user_article END DESC,
            CASE WHEN is_approved = 1 THEN updated_date END DESC,
            updated_date DESC,
            created_date DESC";

        $articles = $this->modelsManager->executeQuery($phql);
?>

PROBLEM

The query doesnot work when I executed using Phalcon's modelManager . I get "Syntax error, unexpected token WHEN near is_approved=1"

However, if I execute this query in phpMyadmin replacing NewsRoom\\Articles\\Models\\Articles with actual table name tbl_articles , it works flawlessly.

Can anybody help me on this.

Change your SQL from

 CASE WHEN is_approved = 1 THEN updated_date END DESC,

to

 CASE is_approved WHEN 1 THEN updated_date END DESC,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM