How to sort by the number of votes?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.