如何结合两个sql select语句

时间:2017-02-23 15:14:35

标签: mysql sql sql-server oracle

我有两个SQL select语句,说

SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood FROM FoodTable WHERE FoodID IS NOT NULL AND FoodDate >= '2015-10-01' GROUP BY FoodID ORDER BY NumberOfFood DESC

我还有另一个SQL语句

SELECT FoodSellerID, Market1, SellerLastName, SellerFirstName, PrimaryAddress1, PrimaryAddress2, PrimaryCity, PrimaryState, PrimaryZip FROM SellerTable, MarketTable 
WHERE Market1= MarketTable.MarketID

很少有人指出,FoodID查询到SellerTable,Market1查询到MarketTable。我的问题是如何将两者结合起来并返回表格,如

FoodID | NumberofFood | SellerLastName | SellerFirstName | PrimaryAddress1 | PrimaryAddress2 | PrimaryCity | PrimaryState | PrimaryZip

1 个答案:

答案 0 :(得分:0)

尝试这个:

SELECT TOP (150) a.FoodID, COUNT(*) AS a.NumberOfFood,b.SellerLastName, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2, b.PrimaryCity, b.PrimaryState, b.PrimaryZipFROM FoodTable a inner join SellerTable b on a.FoodID = b.FoodSellerID WHERE a.Market1= b.MarketTable.MarketID IS NOT NULL AND FoodDate >= '2015-10-01' GROUP BY a.FoodID, b.SellerLastName, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2, b.PrimaryCity, 
b.PrimaryState, b.PrimaryZip ORDER BY a.NumberOfFood DESC

注意:可能是另一种解决方案。