saba
saba

Reputation: 382

how to put if else condition in LIMIT mysql

here is my query

$where_or = " 
                AND
                    content_status = 'Active'
                AND
                    content_post_status = 'published'
                AND
                    deleted = 0
                AND
                    content_type = '$type'
                ORDER BY content_created_at DESC ";

        $select = "content_id, content_title, content_short_description, content_url, content_created_at";

        $query = $this->db->query("
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 54
                    $where_or
                    LIMIT 3
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 55
                    $where_or
                    LIMIT 2
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 56
                    $where_or
                    LIMIT 2
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 57
                    $where_or
                    LIMIT 1
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 58
                    $where_or
                    LIMIT 1
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 60
                    $where_or
                    LIMIT 1
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 61
                    $where_or
                    LIMIT 1
                )
                UNION ALL 
                (
                    SELECT $select
                    FROM tbl_content
                    WHERE 
                    content_category_id = 118
                    $where_or
                    if content_type = 'article'
                    begin 
                        LIMIT 10
                    end
                )   
        ");

You can see in last UNION i want to put limit on if condition ..but its giving me error...how can i do that...please help..

Upvotes: 2

Views: 3732

Answers (2)

Devart
Devart

Reputation: 122042

I'd suggest you to rank records and then add WHERE condtition to filter records by content_category_id and its rank. In this case you will avoid using multiple SELECT...FROM queries.

Upvotes: 0

Ben English
Ben English

Reputation: 3938

You cannot do that with MySQL, at least not the way you are trying to. If you think about it this:

if content_type = 'article'
begin 
   LIMIT 10
end

doesn't make sense. LIMIT is applied to a set not a single record. If what you wrote were valid SQL and your data contained both content that was true for content_type = 'article' and some that was false. What would you expect to happen?

Upvotes: 1

Related Questions