我有一个包含大量轮胎品牌和信息的数据库。我有一个名为 Accepted_Brands 的表,它有 shop_id 和 brand_id,所以它告诉我哪个商店有什么品牌。
tire_brands 表只有一个 ID 和一个名称,列出了所有轮胎品牌。
在该特定商店的页面上,我列出了他们销售的所有轮胎品牌......并且可以选择添加新品牌。
添加新品牌弹出窗口只有一个下拉菜单,这是我的代码:
if ($result = $mysqli->query('SELECT id, name FROM tire_brands ORDER BY name')) {
while( $row = $result->fetch_assoc()){
$tire_name = $row['name'];
$tire_id = $row['id'];
echo "<option value=\"".$tire_id."\">".$tire_name."</option>";
}
因此,它会在下拉列表中显示所有轮胎品牌,他们可以选择将其添加到他们接受的列表中。问题是它显示了所有这些,即使它已经被选中。所以我可以添加两个相同的品牌。
有没有办法将它从 SELECT 查询中排除,或者有其他方法让下拉列表只能显示他们未使用的品牌列表?
谢谢!
最佳答案
你必须使用这样的子查询:
if ($result = $mysqli->query('SELECT id, name FROM tire_brands where id NOT IN (select shop_id from Accepted_Brands) ORDER BY name')) {
while( $row = $result->fetch_assoc()){
$tire_name = $row['name'];
$tire_id = $row['id'];
echo "<option value=\"".$tire_id."\">".$tire_name."</option>";
}
这只是一个例子,我不知道你如何在你的 Accepted_Brands 上插入数据,以及你如何理解一个 id,brand 是否已经在你的 Accepted_Brands 表中
关于php - MySQL SELECT 查询 - 排除某些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7742566/