简体   繁体   中英

How to get a list that order by username if it have score in score table?

i have 2 tables called "user" and "score"
i want to list users order by username and the username should be in score table

user table:

 id Username
 1  D
 2  B
 3  F
 4  A
 5  E
 6  C

score table:

 Scorid UserID score
 1      3      156
 2      4      202
 3      4      890
 4      6      346
 5      3      657
 6      5      658
 7      5      700
 8      5      263
 9      2      1089
 10     3      745

I want a list like this:

 UserName score
 A        890
 A        202
 B        1089
 C        346
 E        700
 E        658
 E        263
 F        745
 F        657
 F        156

*List must order by username and the scores is order too in their username group
*User "D" is not listed because it has not got any score
*User may have more than one score

I am writing in PHP and i am using mysql

Which query is give this list in one query?

Select Username, score 
from   user, score 
where  score.UserID = user.id and 
       score > 0
order  by username, score
SELECT
user.username,
score.score
FROM user
LEFT JOIN score
ON score.UserID = user.id

This is the query you want to run (MySQL) with correct names (notice that I've changed your tables names to be plural):

SELECT username, score
FROM  `score` 
JOIN  `user` ON id = UserID
ORDER BY username ASC , score 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