怎麼按投票數來排序?

來源:互聯網
上載者:User
表tp
toupiao_id name ........
1 張三
2 李四

表tp1
id toupiao_id ip
1 1 .......
1 1 .......
1 2 .......
1 2 .......
1 2 .......

迴圈表tp
$sql = "SELECT * FROM ".$fdyu->table('tp')." ".$sql_where." order by toupiao_id asc";//// 請問order by這個怎麼寫可以按表tp1裡的toupiao_id數的多少來排列
$res = $db->query($sql);
$arr = array();

比如,李四有3票,張三有2票,那麼在迴圈tp表的時候,李四顯示在前面,張三顯示在後面


回複討論(解決方案)

最近天氣一冷一熱的
你這種情況我一般會把數量緩衝到表1中,每次投票更新一下

現在已經沒放了,有什麼辦法解決

對 toupiao_id 分組求 count

幫忙寫一下,謝謝^_^

可以這樣寫:

select tp1.toupiao_id, count(*) as total_vote from tp1 left join tp on tp1.toupiao_id = tp.toupiao_id group by tp1.toupiao_id order by total_vote desc;

select a.*,b.num from tp as a left join (select toupiao_id,count(*) as num from tp1 group by toupiao_id) as b on a.toupiao_id=b.toupiao_id order by b.num desc;

後來發現這樣統計和排序票數,資料庫到後面票數多的時候受不了,後來還是改成1樓說的那樣了

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.