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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度