# Column not found: Unknown column in where clause

**URL:** <https://discourse.cakephp.org/t/column-not-found-unknown-column-in-where-clause/2709>\
**Category:** Need Help\
**Created:** [June 14, 2017, 11:20am UTC](https://discourse.cakephp.org/t/column-not-found-unknown-column-in-where-clause/2709 "2017-06-14T11:20:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![themarsbeing](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@themarsbeing](https://discourse.cakephp.org/u/themarsbeing)\
**Post date:** [June 14, 2017, 11:20am UTC](https://discourse.cakephp.org/t/column-not-found-unknown-column-in-where-clause/2709/1 "2017-06-14T11:20:46Z")

</div>

Following is the association

```
$this->table('school');

$this->belongsTo('Districts', [
    'foreignKey' => 'district_id'
]);
$this->hasMany('Students', [
    'foreignKey' => 'school_id'
]);

```

Each school has many students  
Each school belongs to a district

Let’s say there are 4 districts. ([Districts.id](http://Districts.id) = 1,2,3,4)  
In School’s Model, I’m trying to fetch the list of all students.  
But if the district is 1 or 2, I want to show only those students who scored more than 50 marks.  
And for the other districts, I am trying to show students who scored less than 35 marks.  
This is the finder function I wrote for the above mentioned.

```
public function findStudentsList(Query $query, array $options)
{ 
    $query->select(['id','name','modified','created'])
    ->contain([
        'Districts'=> function ($query) {
            return $query->select(['id','name']);
        },
        'Students' => function ($query) {
            return $query->where([
                'CASE 
                WHEN Districts.id=1 THEN Students.marks >50 
                WHEN Districts.id=2 THEN Students.marks >50 
                ELSE Students.marks < 35 
                END']);
        }		
    ]);
}

```

But when I execute this, I get the following error.

`Column not found: Unknown column 'Districts.id' in 'where clause'`

Is this the right way to do it? I’m new to CakePHP3.

---

<div class="post-metadata">

**Author:** ![themarsbeing](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@themarsbeing](https://discourse.cakephp.org/u/themarsbeing)\
**Post date:** [June 14, 2017, 11:53am UTC](https://discourse.cakephp.org/t/column-not-found-unknown-column-in-where-clause/2709/2 "2017-06-14T11:53:17Z")

</div>

Used `district_id` instead of `Districts.id` and it worked.
