简体   繁体   English

SQL从一个表中选择品牌,如果另一个表具有该品牌的产品

[英]SQL to select brand from a table, if another table has a product with that brand

Using MySQL and PHP. 使用MySQL和PHP。

I have two tables products and stock. 我有两个表产品和库存。

products has { productID | 产品具有{productID | product | 产品| brand|} 品牌|}

stock has { ID | 库存有{ID | storeID | storeID | productID | productID | qty | 数量 price } 价钱 }

I need to get all of the brands available at a specific store. 我需要在特定商店获得所有品牌。

So here is what I have that doesn't work. 所以这是我无法使用的。

SELECT DISTINCT brand 
  FROM products 
 WHERE 'products.productID' = 'stock.productID' 
   AND 'stock.storeID' = 1

If I remove the compare "WHERE" it returns all of the brands. 如果删除比较“ WHERE”,它将返回所有品牌。

Try 尝试

Select p.brand FROM products p, stock s 
WHERE p.productID=s.productID and s.storeID='1'
SELECT DISTINCT p.brand 
FROM products p inner join stock s on p.productID=s.productID
WHERE `stock.storeID` = 1

mark the diff between ` and ' 标记`和'之间的差异

try 尝试

 "SELECT p.* FROM Products p 
 INNER JOIN stock s USING(productID)
 WHERE s.storeID = '".(int)$givenID."' 
 GROUP BY p.brand";

我们也可以这样写

SELECT DISTINCT p.brand FROM products p , stock s where  p.productID=s.productID(+) and stock.storeID = 1

Try this: 尝试这个:

SELECT DISTINCT p.brand 
FROM   products p 
             INNER JOIN stock s 
                  ON p.productID=s.productID
WHERE s.`storeID` = 1

This will help 这会有所帮助

select p.brand 
  from product p, stock s 
 where p.productID = s.productID
   and s.storeID = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM