-1

伙计们,我想不通..我红了很多,但没有运气...

我有以下查询,我需要限制它以显示总结果减去特定数量的 os 结果,比如说 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 ' 
            ;
4

1 回答 1

0

如果您使用的是 MySQL,请使用limitand offset

limit 10, 9999999

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

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

要真正做你想做的事情(删除 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' 
        ;
于 2013-04-03T21:21:46.873 回答