douleng3463 2015-04-19 18:56
浏览 44
已采纳

如何避免重复并在行已存在时选择一行?

I'm trying to generate a report of all courses in all curriculum's. My problem is that if two curriculum's have the same course, the query will bring them both. What I need if the course in both curriculum, fetch one of them only.

'curriculumcourses' Table: in this table are all courses in different curriculums.

enter image description here

The problem happen when for example:

course_id=1
curriculum_id=1
set_numer=0 //this row is not related to the problem

course_id=1
curriculum_id=2
set_numer=0 //this row is not related to the problem

The same course_id in different curriculum which is correct, but when I need to generate this report I don't need to show both courses I need just one. any help would be highly appreciated.

Here is the query I'm using

$result2 ="SELECT 
  * 
FROM
  curriculumcourses NATURAL 
  JOIN courses 
WHERE semester_ava = '$avalablesemster' 
  AND curriculum_id IN 
  (SELECT 
    curriculum_id 
  FROM
    curriculum 
  WHERE '".$coursneededdate."'
   BETWEEN startdate  AND enddate ";
if(!empty($gradunder)){
 $result2 .=" AND grad_under = '".$gradunder."'";
}
 $result2 .=")";
  • 写回答

2条回答 默认 最新

  • dqvs45976 2015-04-19 19:05
    关注

    Use GROUP BY:

    $result2 ="SELECT 
      * 
    FROM
      curriculumcourses NATURAL 
      JOIN courses 
    WHERE semester_ava = '$avalablesemster' 
      AND curriculum_id IN 
      (SELECT 
        curriculum_id 
      FROM
        curriculum 
      WHERE '".$coursneededdate."'
       BETWEEN startdate  AND enddate ";
    if(!empty($gradunder)){
     $result2 .=" AND grad_under = '".$gradunder."'";
    }
     $result2 .=") GROUP BY (course_id);";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接