dragon87836215 2016-06-24 06:25
浏览 35

根据日期从表中获取事件

I have 3 tables, user, courses and usertocourse

In user table, the fields are given below

| id         | name       |   email   | 

courses table

| course_id | name  |description| enrollment_key  |

enrollment_key is 1 for self enrolled courses, enrollment_key is 0 for admin assigned courses.

usertocourse

| user_id    | course_id  | assigndate | completiondate | started |

when a admin assign a course to user, it will come to this table, or if the user self enrolled also it will come to this table. Started bit is set to 1 only when the user started the courses, else its 0. For self assigned course the completion date is automatically set to some default date say(2000-11-30)

Now what I need is two things

  1. I need to get the list of all courses to that user_id from usertocourse table(both self enrolled and assigned courses), and the list of all self enrolled courses from course table except the once which the user is already enrolled.

    SELECT a.*, b.*
    FROM courses a 
    JOIN users_to_courses b
    WHERE a.course_id=b.course_id
    and b.user_id='$user_id'

    The above query returns all the courses, but it does nt check whether it is already assigned or not. I need a query which can check both together.

  2. I have a date with me say '2016-07-01', I want to display all the courses which of that month. The below given query works fine if assign date and completion date are in same month, say if the course is starting on 2016-06-28 and completiondate is 2016-07-05, the query will not work as am comparing the $date with assign date.

    SELECT a.*,b.*
    FROM courses a
    JOIN users_to_courses b
    WHERE EXTRACT(YEAR_MONTH FROM b.`assign_date`) = EXTRACT(YEAR_MONTH FROM '$date')
    AND b.`user_id` = '$user_id'
    and a.enrollment_key='0'
    and b.completion_date!='2000-11-30'
    and a.course_id = b.course_id
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?