简体   繁体   中英

Order by votes - PHP

I have a voting script which pulls out the number of votes per user.

Everything is working, except I need to now display the number of votes per user in order of number of votes. Please see my database structure:

Entries:

UserID, FirstName, LastName, EmailAddress, TelephoneNumber, Image, Status

Voting:

item, vote, nvotes

The item field contains vt_img and then the UserID , so for example: vt_img4 and both vote & nvotes display the number of votes.

Any ideas how I can relate those together and display the users in order of the most voted at the top?

Thanks

You really need to change the structure of the voting table so that you can do a normal join. I would strongly suggest adding either a pure userID column, or at the very least not making it a concat of two other columns. Based on an ID you could then easily do something like this:

select
    a.userID,
    a.firstName,
    b.votes
from
    entries a
        join voting b
            on a.userID=b.userID
order by
    b.votes desc

The other option is to consider (if it is a one to one relationship) simply merging the data into one table which would make it even easier again.

At the moment, this really is an XY problem, you are looking for a way to join two tables that aren't meant to be joined. While there are (horrible, ghastly, terrible) ways of doing it, I think the best solution is to do a little extra work and alter your database (we can certainly help with that so you don't lose any data) and then you will be able to both do what you want right now (easily) and all those other things you will want to do in the future (that you don't know about right now) will be oh so much easier.

Edit: It seems like this is a great opportunity to use a Trigger to insert the new row for you. A MySQL trigger is an action that the database will make when a certain predefined action takes place. In this case, you want to insert a new row into a table when you insert a row into your main table. The beauty is that you can use a reference to the data in the original table to do it:

CREATE TRIGGER Entries_Trigger AFTER insert ON Entries
    FOR EACH ROW BEGIN
        insert into Voting values(new.UserID,0,0);
    END;

This will work in the following manner - When a row is inserted into your Entries table, the database will insert the row (creating the auto_increment ID and the like) then instantly call this trigger, which will then use that newly created UserID to insert into the second table (along with some zeroes for votes and nvotes).

Your database is badly designed. It should be:

Voting:

item, user_id, vote, nvotes

Placing the item id and the user id into the same column as a concatenated string with a delimiter is just asking for trouble. This isn't scalable at all. Look up the basics on Normalization .

You could try this:

SELECT *
FROM Entries e
JOIN Voting v ON (CONCAT('vt_img', e.UserID) = v.item)
ORDER BY nvotes DESC

but please notice that this query might be quite slow due to the fact that the join field for Entries table is built at query time.

You should consider changing your database structure so that Voting contains a UserID field in order to do a direct join.

I'm figuring the Entries table is where votes are cast (you're database schema doesn't make much sense to me, seems like you could work it a little better). If the votes are actually on the Votes table and that's connected to a user, then you should have UserID field in that table too. Either way the example will help.

Lets say you add UserID to the Votes table and this is where a user's votes are stored than this would be your query

SELECT Users.id, Votes.*,
SUM(Votes.nvotes) AS user_votes
FROM Users, Votes
WHERE Users.id = Votes.UserID
GROUP BY Votes.UserID
ORDER BY user_votes

USE ORDER BY in your query --

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM