반응형
Like
연산자로 데이터를 검색하려면 어떻게해야합니까?
시도했지만 오류가 발생합니다.
use yii\db\Query;
public function getExportData($searchVal = '')
{
$query = new Query;
if($searchVal != '') { **here i am getting error when searchVall != ''**
$query->select('*')->from('post')
->where(['like', 'title', $searchVal])
->orderBy(['added_date_time' => SORT_DESC]);
$posts = $query->createCommand()->queryAll();
} else {
$query->select('*')->from('post')->orderBy(['added_date_time' => SORT_DESC]);
$posts = $query->createCommand()->queryAll();
}
return $posts;
}
select 문에 대한 간단한 방법이 있습니까?
해결 방법
변수를 사용하여 쿼리를 전달하려면 findBySql () 메서드를 사용하십시오. 예를 들면
$query = "SELECT * FROM `post` where `title` LIKE 'foo%' ";
$result = Model::findBySql($query)->all();
이것이 문제를 해결하는 데 도움이되기를 바랍니다.
참조 페이지 https://stackoverflow.com/questions/31163251
반응형
'MySql' 카테고리의 다른 글
MySQL 모두 NULL이 아닌 경우 (0) | 2020.11.19 |
---|---|
MySQL 날짜 별 항목 선택-> = NOW (), MySQL (0) | 2020.11.19 |
MySQL Recursive categories with a single query? (0) | 2020.11.19 |
MySQL 테이블에서 최신 ID 번호를 얻는 방법은 무엇입니까? (0) | 2020.11.19 |
MySQL Laravel 5.1 Migration and Seeding Cannot truncate a table referenced in a foreign key constraint (0) | 2020.11.19 |
댓글