跳过在where子句中检查项目

时间:2015-06-30 09:43:25

标签: sql

在下面的代码中,在WHERE子句中,将针对该条件"bcp.value = cm.id"检查所有项目。因此,如果项目在bcp表中没有值,它将不会显示,我如何让它出现? 我已经尽力使用子查询,但没有成功。 请帮忙

 SELECT DISTINCT
   u.firstname,
   u.lastname,
   u.id,
   c.shortname,
   c.fullname,
   cs.name AS 'Units Code/s Covered',
   m.name,


   MIN(FROM_UNIXTIME(cmc.timemodified)) AS 'Start Time',
   MAX(FROM_UNIXTIME(bgi.dateissued))AS 'Time Completed'

FROM
  mdl_course_modules_completion cmc

  LEFT JOIN mdl_course_modules cm
        ON (cmc.coursemoduleid =cm.id)

    LEFT JOIN mdl_course_sections cs 
        ON (cs.id = cm .section)

    LEFT JOIN mdl_modules m
        ON (m.id= cm.module )

 LEFT JOIN mdl_course c
        ON c.id = cm.course AND c.shortname = 'DOM_2015_1'
LEFT JOIN mdl_user u
        ON u.id = cmc.userid AND (u.firstname = 'bambo' OR u.firstname = 'Test bambo') 

LEFT JOIN mdl_badge_issued bgi
        ON bgi.userid = cmc.userid

LEFT JOIN mdl_badge bg
        ON bg.id = bgi.badgeid

LEFT JOIN mdl_badge_criteria bc
        ON bc.badgeid = bg.id

LEFT JOIN mdl_badge_criteria_param bcp
        ON bcp.critid = bc.id AND bcp.value = cm.id



GROUP BY u.firstname, cs.name, FROM_UNIXTIME(bgi.dateissued) 

3 个答案:

答案 0 :(得分:1)

试试这个:

Child c = new Child();
c.doStuff();

答案 1 :(得分:1)

只需将OR添加到您的Where子句中

WHERE c.shortname = 'DOM_2015_1' AND (bcp.value = cm.id OR bcp.value IS NULL)

或如果它是字符串bcp.value = ""

<强>更新

在Where子句中添加此内容之前,您必须正确加入,或许最好使用 FULL OUTER JOIN RIGHT JOIN (有可能吗?这取决于你的逻辑)而不是使用 LEFT JOIN

答案 2 :(得分:0)

jelmer@jelmer-N56JN:~/Git/Board/lib$ g++ -std=c++0x bord.c In file included from bord.c:1:0: bord.h:20:1: error: new types may not be defined in a return type class bord ^ bord.h:20:1: note: (perhaps a semicolon is missing after the definition of ‘bord’) bord.c:3:12: error: return type specification for constructor invalid bord::bord():char_bord(new std::unique_ptr<char>[10]) ^ bord.c: In constructor ‘bord::bord()’: bord.c:7:46: error: expected primary-expression before ‘]’ token char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ bord.c:7:60: error: parenthesized initializer in array new [-fpermissive] char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ bord.c:7:19: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<char>’ and ‘std::unique_ptr<char>*’) char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ bord.c:7:19: note: candidates are: In file included from /usr/include/c++/4.9/memory:81:0, from bord.h:19, from bord.c:1: /usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = char; _Dp = std::default_delete<char>] operator=(unique_ptr&& __u) noexcept ^ /usr/include/c++/4.9/bits/unique_ptr.h:249:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::unique_ptr<char>&&’ /usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> > >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = char; _Dp = std::default_delete<char>] operator=(unique_ptr<_Up, _Ep>&& __u) noexcept ^ /usr/include/c++/4.9/bits/unique_ptr.h:269:2: note: template argument deduction/substitution failed: bord.c:7:19: note: mismatched types ‘std::unique_ptr<_Tp, _Dp>’ and ‘std::unique_ptr<char>*’ char_bord[i]=new std::unique_ptr<char>[](new char[10]); ^ In file included from /usr/include/c++/4.9/memory:81:0, from bord.h:19, from bord.c:1: /usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = char; _Dp = std::default_delete<char>; std::nullptr_t = std::nullptr_t] operator=(nullptr_t) noexcept ^ /usr/include/c++/4.9/bits/unique_ptr.h:278:7: note: no known conversion for argument 1 from ‘std::unique_ptr<char>*’ to ‘std::nullptr_t’ 条件正在应用于整个结果集,因此会丢弃不符合此条件的表WHERE中的任何行。

如果您只需要检查可能来自mdl_course_modules_completion表的NULL值,那么添加额外的mdl_badge_criteria_param bcp即可。

这是因为您的(or bcp.value is null)表正在加入mdl_course_modules cm,而不是主表。如果我们有直接加入主表的情况,让mdl_badge_criteria_param bcp独立于bcp,那么当cm持有cm.id时,查询有时会产生错误的结果,并且NULL是非空值。

记住这一点,您的解决方案将是

bcp.value

修改您还可以考虑考虑WHERE c.shortname = 'DOM_2015_1' AND ( bcp.value = cm.id OR bcp.value is null ) 删除整个条件,因为您似乎希望允许每条记录与主表匹配bcp.value = cm.id出现,而是将cm.id应用于您的where条件。