How to sort by the number of votes? Table tp
Toupiao_id name ........
1 Zhang San
2 Li Si
Table tp1
Id toupiao_id ip
1 .......
1 .......
1 2 .......
1 2 .......
1 2 .......
Cyclic table tp
$ SQL = "SELECT * FROM ". $ fdyu-> table ('TP '). "". $ SQL _where. "order by toupiao_id asc"; // How can I sort order by the number of toupiao_id in table tp1?
$ Res = $ db-> query ($ SQL );
$ Arr = array ();
For example, if Li Si has 3 votes and Zhang San has 2 votes, Li Si is shown in front of the cyclic tp table, and Zhang San is shown in the back of the table.
Reply to discussion (solution)
The weather has been cool recently.
In this case, I usually cache the quantity to Table 1 and update it each time I vote.
No More. what can I do?
Count the toupiao_id Group
Thank you!
You can write as follows:
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;
Later, I found that the number of votes in the statistics and sorting process was unacceptable when the number of votes in the database reached the end. later I changed it to what I said on the first floor.