0

我不能得到不等于条件工作。会发生什么情况是因为cancelled_by字段为null而忽略了所有数据。如果cancelled_by字段是null那么这不等于不等于Tutor不等于不工作在cakephp3

这个小错误花了我5天的错误数据。我正在犯错误吗?一旦我删除了不等于条件,所有数据都按预期检索。

下面的两行是我用过的,都没有工作。

'Lessons.cancelled_by not like' => '%Tutor%' 

'Lessons.cancelled_by !=' => 'Tutor' 
$searchDate = date('Y-m-d'); 

$query3 = $this->find() 
    ->contain([ 
     'Tutors', 'Subjects', 'Students', 'Students.Guardians', 'XeroInvoices' 
    ]) 
    ->select([ 
     'Lessons.id', 'Lessons.lesson_date', 'Lessons.start_time', 'Lessons.end_time', 
     'Tutors.id', 'Tutors.first_name', 'Guardians.id', 'Tutors.last_name', 
     'Lessons.timesheet_status', 'Students.id', 'Students.first_name', 
     'Students.last_name', 'Students.has_credit', 'XeroInvoices.invoice_number', 
     'Guardians.guardian_email', 'Guardians.guardian_mobile', 
     'Guardians.guardian_last_name', 'Guardians.guardian_first_name', 
     'Lessons.due_date', 'Lessons.cancelled_by', 'Subjects.name', 'Lessons.revenue', 
     'Lessons.discount', 'Lessons.surcharge', 'Lessons.future_makeup_lesson_id', 
     'Lessons.cancelled_pastlesson_id' 
    ]) 
    ->where([ 
     'Lessons.due_date' => $searchDate, 
     'Lessons.lesson_inactive' => false, 
     'Students.has_credit like' => '%postpaid%', 
     'Lessons.cancelled_by !=' => 'Tutor' 
    ]) 
    ->order([ 
     'Guardians.id', 
     'Lessons.lesson_date' => 'ASC', 
     'Lessons.start_time' => 'ASC' 
    ]) 
    ->hydrate(false); 
+1

平等运算符通常不会与SQL中的空值交互得很好。适当时,你应该添加一个“OR”条件,用'cancelled_by IS'=> NULL'或者'IS NOT'。 –

+0

这与PHP无关。在SQL语言中,'NULL'根据定义不等于任何东西。 –

+0

如果我理解了这个问题,那么我不会寻求帮助,我不会在cakephp3中发布这个。 – jagguy

回答

2

'Lessons.cancelled_by !=' => 'Tutor'是正确的,但它不适用于空值。 'Lessons.cancelled_by IS NOT' => 'NULL'会替代NULL值。我认为你可以使用两者。所以结果只会是(cancelled_by!='Tutor'AND cancelled_by IS NOT NULL),这是你需要的结果。

-1

使用- > notEq()

例如

$query = $articles->find() 
    ->where(function ($exp) { 
     return $exp 
      ->eq('author_id', 2) 
      ->eq('published', true) 
      ->notEq('spam', true) 
      ->gt('view_count', 10); 
    }); 

在这里阅读更多Cakephp 3

+1

这是怎么解决'NULL'值的问题? 'notEq()'会产生与问题中所示完全相同的比较,即它会创建一个'!='运算符。 – ndm

0

这是由这里海报正确的答案。如果这个人可以在这里提出答案,一般来说,等价运算符不会与SQL中的空值交互。您应该添加一个“OR”条件,其中'cancelled_by IS'=> NULL或IS NOT。