我得到了一个类似的talbe
category_table
cat_id | cat_name
---------+--------------
1 | cat 1'
---------+--------------
2 | cat 2
---------+--------------
3 | cat 3
---------+--------------
4 | cat 4
---------+--------------
5 | cat 5
另一个表称为项目表,如下
item_table
item_id | item_name | cat_id
----------+----------------+-----------
1 | name of item | 2
2 | name of item | 1
3 | name of item | 2
4 | name of item | 3
5 | name of item | 2
6 | name of item | 4
我收到了以下关于select in item_table 的查询
SELECT * FROM item_table WHERE cat_id=2
我有一个类似的类别id数组
$cats=array("2","3","3");
我的问题是如何使用上述数组执行"多类别"搜索?意味着我要选择类别id为2,3,4的所有项目。
使用MYSQL
:的In Operator
SELECT * FROM item_table WHERE cat_id IN(2,3,4)
如果你想通过表的联接获得结果,那么你可以使用:
SELECT t1.item_id, t1.item_name, t1.cat_id, t2.cat_name
FROM item_table t1
INNER JOIN category_table t2 ON t1.cat_id = t2.cat_id
WHERE t1.cat_id IN(2,3,4)