У меня есть запрос для поиска трех вещей, ключевого слова, категории и региона (это для вакансий).Все работает, за исключением региона, поскольку я получаю этот столбец из базы данных с псевдонимом.
Мой код сейчас:
$functie = $_POST['functie'];
$branche = $_POST['branche'];
$regio = $_POST['regio'];
$search = "
SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
";
// Collect all the where conditions in an array
$whr = array();
// check if $functie has some value in input filter
if (!empty($functie)) {
$whr[] = "cnt.title LIKE '%" . $functie . "%'";
}
if (!empty($regio)) {
$whr[] = "f.value LIKE '%" . $regio . "%'";
}
// check if $branche has some value in input filter
if (!empty($branche)) {
$whr[] = "cat.title LIKE '%" . $branche . "%'";
}
$where_sql = '';
// Prepare where part of the SQL
if (!empty($whr)) {
$where_sql = ' WHERE ' . implode(' OR ', $whr);
}
// Append to the original sql
$search .= $where_sql;
$searchcon = $conn->query($search);
Но приведенный выше код не работает для региона.
Однако это запрос на конечный результат, и вот результаты, которые я получаю с ним:
SELECT cnt.title as content_title, cnt.alias as content_alias, cnt.images, cnt.introtext, cnt.catid, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 2 then f.value end) as regio
FROM snm_content cnt
LEFT JOIN snm_categories cat
ON cat.id = cnt.catid
LEFT JOIN snm_fields_values f
ON cnt.id = f.item_id
WHERE cnt.title LIKE '%vrachtwagen%'

regio
- это то, что яхочу показать результаты для.
Но WHERE regio LIKE '%" . $regio . "%'";
не работает, он говорит неизвестный столбец regio
, потому что это псевдоним.Как я могу показать только все результаты, относящиеся к региону, который был опубликован?Например, изображение выше, показывать только эту строку, когда Drenthe
является опубликованным регионом.