我希望创建一个简单的查询,显示类别ID与数据库中的“category”或“category_2”列匹配的所有产品,同时匹配品牌。
我需要在同一个查询中使用OR和AND。
这是我当前的查询工作正常:
$all="SELECT * FROM models WHERE category = " . $category['id'] . " AND brand = ". $brand_name['id'] ." ORDER BY year DESC, month DESC";
这是我想要实现的目标:
$all="SELECT * FROM models WHERE category = " . $category['id'] . " OR category_2 = " . $category['id'] . " AND brand = ". $brand_name['id'] ." ORDER BY year DESC, month DESC";
然而,根据我的新查询,AND品牌部分无效。
答案 0 :(得分:4)
这很容易sql注入你应该使用mysqli。试试这个:
true
答案 1 :(得分:1)
您需要对逻辑进行分组:
$all="SELECT * FROM models
WHERE (category = " . $category['id'] . " OR category_2 = " . $category['id'] . ")
AND brand = ". $brand_name['id'] ." ORDER BY year DESC, month DESC";
答案 2 :(得分:1)
尝试添加()
$all="SELECT *
FROM models
WHERE (category = " . $category['id'] . " OR category_2 = " . $category['id'] . ")
AND brand = ". $brand_name['id'] ."
ORDER BY year DESC, month DESC";
答案 3 :(得分:1)
您需要做的就是在OR条件周围放置括号。
$all="SELECT * FROM models WHERE (category = " . $category['id'] . " OR category_2 = " . $category['id'] . ") AND brand = ". $brand_name['id'] ." ORDER BY year DESC, month DESC";
答案 4 :(得分:1)
只需将.WithMany(e => e.Locations)
条件放在or
内,这样就可以将它们视为一个条件,然后将brackets
视为第二个条件
AND