我正在尝试将vars绑定到我的原始查询中,
$query = "SELECT 1 AS rank, 'CATEGORY' AS type, category_id AS id, name AS name
FROM category_translations
JOIN categories ON categories.id = category_translations.category_id
WHERE name LIKE '%?%'";
$results = DB::select($query, ['ger']);
但是我无法这样做。我尝试通过绑定命名的参数,也尝试在DB::raw
内使用DB::select
而不成功。
我在这里做什么错了?
答案 0 :(得分:1)
尝试那样使用
$query = "SELECT 1 AS rank, 'CATEGORY' AS type, category_id AS id, name AS name
FROM category_translations
JOIN categories ON categories.id = category_translations.category_id
WHERE name LIKE '% :nameSearch %'";
$results = DB::select(DB::raw($query), ['nameSearch' => 'ger']);
第二种用法
$searchText = "ger";
$results = DB::select(DB::raw($query), ['nameSearch' => "%".$searchText."%"]);
答案 1 :(得分:1)
您需要将%
添加到绑定值中,而不要添加到查询中(除非您concat()
值)...
$query = "SELECT 1 AS rank, 'CATEGORY' AS type, category_id AS id, name AS name
FROM category_translations
JOIN categories ON categories.id = category_translations.category_id
WHERE name LIKE ?";
$results = DB::select($query, ['%ger%']);