表1
<>之前+----+-------------------------------+--------+--------+------+| id | thesis_title | cat_id | status | year+----+-------------------------------+--------+--------+------+| 10 |项目奖- 2014 | 1,2 | C | 2014 |项目奖- 2015 | 1,2 | C | 2015 |2015年度博士研究奖| 3 | A | 2015 |+----+-------------------------------+--------+--------+------+之前表2 <>之前+----+--------------------------+---------+| id |类别|状态|+----+--------------------------+---------+本科课程(UG) | 1 |研究生课程(PG) | 1 || 3 | PhD | 1 |+----+--------------------------+---------+之前如上所述,我有两个表。
我想从表1中选择表2的类别,如果Status是C (Closed)
for ex:Table 1 contains cat_id 1,2(string separated by commas
和表2包含id:1、2、3和类别。
所以我想从表2中为表1的cat_id选择相应的类别。
我理解你的问题....(您需要的类别名称与已关闭的项目相关)。
-
从表1中选择状态为c的所有类别
-
联合所有的categorories_id(删除重复项)并创建一个列表,以逗号分隔。
-
从表2中选择类别名,其中id在生成的列表中
"select category from table 2 where id in ('1', '2')";
编辑:1:选择关闭的类别。
$count = 0;
$cat_list = "";
$qry = "select cat_id from table 1 where status = 'C'";
$fetch = mysql_query($qry);
while($row = mysql_query($fetch)){
$count++;
if($count == 1){$cat_list .= $row['cat_id'];}
else{$cat_list .= ", ".$row['cat_id'];}
}
$cat_list = array_unique(explode(",", str_replace(" ", "", $cat_list)));
$cat_list = "'".implode("','", $cat_list)."'";
2:查询
$qry = "select category from table 2 where id in (".$cat_list.")";