Php 多类别id的MySQL搜索
我有一张像这样的照片Php 多类别id的MySQL搜索,php,mysql,arrays,Php,Mysql,Arrays,我有一张像这样的照片 category_table cat_id | cat_name ---------+-------------- 1 | cat 1' ---------+-------------- 2 | cat 2 ---------+-------------- 3 | cat 3 ---------+-------------- 4 | cat 4 ---------+-------------- 5
category_table
cat_id | cat_name
---------+--------------
1 | cat 1'
---------+--------------
2 | cat 2
---------+--------------
3 | cat 3
---------+--------------
4 | cat 4
---------+--------------
5 | cat 5
$cats=array("2","3","3");
另一个名为item table的表如下
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
我对item_表中的select进行了以下查询
SELECT * FROM item_table WHERE cat_id=2
我得到了这样一个类别id数组
category_table
cat_id | cat_name
---------+--------------
1 | cat 1'
---------+--------------
2 | cat 2
---------+--------------
3 | cat 3
---------+--------------
4 | cat 4
---------+--------------
5 | cat 5
$cats=array("2","3","3");
我的问题是如何使用上述数组执行“多类别”搜索?表示我希望选择类别id为2,3,4的所有项目。在MYSQL的运算符中使用
:
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)
在MYSQL的操作符中使用:
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)
“4”是从哪里来的?