表 样本数据
壁
wall_id wall_name
1 wall_1
2 wall_2
6 wall_6
wall_categories
wall_id category_id
1 2
2 1
6 1
6 2
类别
category_id category_name
1 Wallpaper
2 Photography
HTML
<a href="file.php?sort=upload_date">Date</a>
<a href="file.php?sort=wall_views">Views</a>
<a href="file.php?sort=wall_downloads">Downloads</a>
<a href="file.php?filter=1">Wallpaper</a>
<a href="file.php?filter=2">Photography</a>
PHP
$sortBy = array('upload_date','wall_views','wall_downloads');
$sort = 'upload_date';
if(isset($_GET['sort']) && inarray($_GET['sort'], $sortBy)){
$sort = $_GET['sort'];
}
$filterBy = array(1, 2);
$filter = join(',', $filterBy);
if(isset($_GET['sort']) && inarray($_GET['sort'], $sortBy)){
$sort = $_GET['sort'];
}
我想拉
来自wall_id
表的wall_name
,wall_views
,walls
和基于category_name
表的类别表中的wall_categories
。以下是我得到的查询:
$walls_query = $connectDB->prepare("SELECT * FROM walls INNER JOIN wall_categories ON
walls.wall_id = wall_categories.wall_id WHERE wall_categories.category_id
IN ($filter) AND walls.wall_id>? ORDER BY $sort DESC;");
$query->execute();
这是使用wall_id
从数据库中选择类别的查询$categories_query = $connectDB->prepare("SELECT category_name FROM categories INNER JOIN wall_categories
ON wall_categories.category_id = categories.category_id WHERE wall_id=?;");
问题是它会生成重复数据,有两个类别的图像会显示两次。需要阻止它。