简体   繁体   English

如何在SQL查询中获取唯一数据?

[英]How to Get unique data in SQL query?

I have added a my sql query in wordpress function. 我在wordpress函数中添加了我的sql查询。 now i want to get unique data from sql with highest number of "Score". 现在,我想从SQL中获得具有最高“分数”的唯一数据。

Ex: This is my SQL Table 例如:这是我的SQL表

I want 2 Results from this table in PHP. 我希望从PHP的此表中获得2个结果。

     Quiz_name             Quiz_id        Correct_score   User_ID

     KENNISMAKING          1              0               1
     KENNISMAKING          1              33              1
     KENNISMAKING          1              100             1
     KENNISMAKING          1              33              1
     KENNISMAKING          1              100             1
     KENNISMAKING          1              66              1
     ACTIEF VERKOPEN       7              33              1
     KENNISMAKING          1              12              1
     ASSORTIMENT-FRIS      15             50              1
     ASSORTIMENT-FRIS      15             50              1
     ASSORTIMENT-FRIS      15             25              1
     ASSORTIMENT-FRIS      15             25              1
     ASSORTIMENT-PSV       13             50              1
     ASSORTIMENT-FRIS      15             50              1
     ASSORTIMENT-PSV       13             100             1
     ACTIEF VERKOPEN       7              100             1
     ACTIEF VERKOPEN       7              100             1
     PERSONEELSZAKEN       6              33              1
     ASSORTIMENT-FRIS      15             75              1
  1. Total Number of Unique quiz taken by User 1. (Ex if "KENNISMAKING" appear 7 times so count this only 1). 用户1进行的唯一测试的总数。(例如,如果“ KENNISMAKING”出现7次,则仅计算1)。

Output: User 1 Attend Total "5" Quiz (KENNISMAKING, ACTIEF VERKOPEN, ASSORTIMENT-FRIS, ASSORTIMENT-PSV, PERSONEELSZAKEN). 输出:用户1参加总计“ 5”测验(KENNISMAKING,ACTIEF VERKOPEN,ASSORTIMENT-FRIS,ASSORTIMENT-PSV,PERSONEELSZAKEN)。

(Note: Only want Number not names.) (注意:只需要数字而不是名称。)

  1. Total Count of 100% (Correct_score) taken by user 1. from this Unique Quiz name. 用户1.从此唯一测验名称中获得的总计数100%(Correct_score)。 (If KENNISMAKING has 2 [100% Correct_score] (See line number 3,5) so count this only 1) (如果KENNISMAKING有2 [100%Correct_score](请参阅第3,5行),因此仅将其计算为1)

Output: User 1 Complete 3 Quiz 100%. 输出:用户1完成3测验100%。

raw sql. 原始SQL。 assuming the table name is quiz 假设表名称是quiz

  1. SELECT count(distinct(Quiz_name)) FROM quiz WHERE User_ID == 1;
  2. SELECT count(distinct(Quiz_name)) FROM quiz WHERE User_ID == 1 and Correct_score == 100;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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