Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何消除sql中的重复项?_Sql - Fatal编程技术网

如何消除sql中的重复项?

如何消除sql中的重复项?,sql,Sql,有人能给我指出正确的方向来消除学生ID字段中的重复项吗 SQL> select DISTINCT(student_class.student_id) as student_Num,student_class.class_id, 2 event.event_id, event.event_name 3 from student_class, event 4 where student_class.class_id = '10' 5 and event.class_id

有人能给我指出正确的方向来消除学生ID字段中的重复项吗

SQL> select DISTINCT(student_class.student_id) as student_Num,student_class.class_id,
  2  event.event_id, event.event_name
  3  from student_class, event
  4  where student_class.class_id = '10'
  5  and event.class_id = '10';

STUDENT_NUM   CLASS_ID   EVENT_ID EVENT_NAME
----------- ---------- ---------- --------------------------------------------------
         12         10          2 Flag FOOtball Game
         12         10          5 PICKUP SOCCER GAME
          9         10          5 PICKUP SOCCER GAME
         16         10          5 PICKUP SOCCER GAME
          6         10          2 Flag FOOtball Game
         18         10          5 PICKUP SOCCER GAME
          4         10          5 PICKUP SOCCER GAME
          4         10          2 Flag FOOtball Game
         16         10          2 Flag FOOtball Game
         20         10          2 Flag FOOtball Game
          3         10          5 PICKUP SOCCER GAME
          2         10          5 PICKUP SOCCER GAME
          3         10          2 Flag FOOtball Game
          8         10          2 Flag FOOtball Game
          9         10          2 Flag FOOtball Game
          2         10          2 Flag FOOtball Game
          6         10          5 PICKUP SOCCER GAME
         20         10          5 PICKUP SOCCER GAME
         18         10          2 Flag FOOtball Game
          8         10          5 PICKUP SOCCER GAME

试试SQL>

  SELECT DISTINCT event.event_id,event.event_name,COUNT(student_class.student_id) AS 'Total Students'
   FROM student_class, event
   WHERE student_class.class_id = '10'
   AND event.class_id = '10' 
     GROUP BY event.event_id,event.event_name,student_class.student_id

这将为您提供事件id和名称,以及前往该事件的学生人数。

存在重复项,因为存在多个匹配的学生id值。预期的结果是什么?当你说“消除重复”时,你真的是指“抑制输出中的重复值”吗?因为如果我们正确使用术语,这里就不会有重复。这些行在事件列中具有不同的值。distinct应用于整行
distinct(col1)
不是列上的函数。