我有一个包含这样数据的表:
articles. id | author | title | content | type
1 | author1, author2 | thetitle1 | text1 | typeA
2 | author1 | thetitle2 | text2 | typeB
3 | author2 | thetitle3 | text3 | typeA
Posted 数组是一个过滤器:
$conditions = array();
$where = '';
if(isset($_POST['authors'])){ //empty, is_array and etc.
$authors = $_POST['authors']; // [ author1, author2 ]
$conditions[] = "author IN ('".implode("','",$authors)."')";
}
if(isset($_POST['types'])){
$types = $_POST['types']; // [ typeA, typeB ]
$conditions[] = "type IN ('".implode("','",$types)."')";
}
if(!empty($conditions)){
$where = ' WHERE '.implode(' AND ', $conditions);
}
$sql = "SELECT * FROM articles".$where;
似乎一切正常,但该字段author
可以包含几个作者,以逗号分隔,因此过滤器author IN ('author1')
将不起作用。如何选择所有author1
涉及的文章(在这种情况下是第一和第二条记录)?