dounuo8797 2015-03-03 09:59
浏览 677

sql order by不起作用

I have code like this:

function artykul_firmy_z_branzy($art_id, $limit) {
    return select("
        select f.id from artykuly a, firmy f where a.display = true
        and a.id_branzy = f.branza_id and a.id = :id and f.profil <> 0 order by f.profil desc, f.firma asc limit ${limit}
    ", array('id' => $art_id));
}

This code should choose some rows from sql database and sort it, and it works great, but result, which has the biggest "f.profil" is always on the end of list, when I change limit to "1", then I got only this result with biggest f.profil, so ordering works, but how to show this on the first position?

Code in template, which shows results:

{% if firmy_z_branzy_logo %}
                <div class="firmy-z-branzy">
                    <img src="/static/screw.png" class="label-screw" /> <div class="label-title">FIRMY Z TEJ BRANŻY</div>
                    <div style="clear: both;"></div>

                    {% for i in firmy_z_branzy_logo %}
                        <div class="firma-branza"><a href="/wizytowka/{{ i.id_firmy }}"><img src="/{{ i.sciezka }}m/{{ i.plik }}" / style="margin-bottom:6px;"></a>{{ i.opis }}</div>
                    {% endfor %}
                </div>
            {% endif %}
  • 写回答

1条回答 默认 最新

  • duanduji2986 2015-03-04 12:14
    关注

    Maybe try with this INNER JOIN:

    SELECT a.*, f.* FROM artykuly a 
    INNER JOIN firmy f ON a.id_branzy = f.branza_id AND a.display = true AND a.id = :id AND f.profil <> 0
    ORDER BY f.profil desc, f.firma LIMIT ${limit}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示