将查询结果限制为总结果减去10个结果

时间:2013-04-03 20:27:33

标签: mysql sql limit

我不能解决这个问题......我红了很多但没有运气......

我有以下查询,我需要限制它以显示总结果减去特定数字的结果,比方说10。

我的意思是,如果查询将返回1000个总结果,我希望它返回990,并且需要排除最后10个结果。

这可能吗?

查看查询电话:

        $query = ' SELECT p.*,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,l.alias as locality_alias,pf.name as name_profile, '
    . ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
    . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
    . ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
    . ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
    . ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
    . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '          
            . ' FROM #__properties_products AS p '              
            . ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '               
            . ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
            . ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
            . ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '             
            . ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
            . ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
            . ' WHERE p.published = 1 '         
            .' ORDER BY p.id DESC ' 
            ;

1 个答案:

答案 0 :(得分:0)

如果您使用的是MySQL,请使用limitoffset

limit 10, 9999999

这从第10条记录开始,一直持续到数据结束(或技术上为9999999行)。

您按p.id desc排序,因此这将删除ID最高的10行。

要真正做你想要的事情(删除具有最低id的10),使用子查询然后在子查询后再次排序:

select t.*
from (<most of your query here>
      order by p.id asc
      limit 10, 999999
     ) t
order by id desc

要删除最高 ID的行,只需添加limit语句:

    $query = 'SELECT p.*,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,l.alias as locality_alias,pf.name as name_profile, '
. ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
. ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
. ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
. ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
. ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '          
        . ' FROM #__properties_products AS p '              
        . ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '               
        . ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
        . ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
        . ' LEFT JOIN #__properties_profiles AS pf ON pf.mid = p.agent_id '             
        . ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
        . ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
        . ' WHERE p.published = 1 '         
        .' ORDER BY p.id desc limit 10, 999999' 
        ;