如何在LIMIT mysql中放入if else条件

时间:2012-03-06 06:06:14

标签: mysql if-statement limit

这是我的查询

$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
                )   
        ");

你可以在最后的UNION中看到我想要限制条件..但是它给我错误...我怎么能这样做...请帮助..

2 个答案:

答案 0 :(得分:1)

你无法用MySQL做到这一点,至少不是你想要的方式。如果你考虑一下 这样:

if content_type = 'article'
begin 
   LIMIT 10
end

没有意义。 LIMIT应用于集合而不是单个记录。如果你写的是有效的SQL,你的数据包含的内容对于content_type ='article'都是正确的,而有些则是false。你期望发生什么?

答案 1 :(得分:0)

我建议您对记录进行排名,然后添加WHERE条件以按content_category_id及其排名过滤记录。在这种情况下,您将避免使用多个SELECT ... FROM查询。