我正在尝试编写一个SQL查询,该查询从一个表中选择多个列,并且仅对一个列使用不同的运算符。
该表很简单。这些列是:
tblFruit_ID, tblFruit_FruitType, tblFruit_FruitName
int NVarChar Text
我正在尝试选择所有tblFruit_FruitType及其对应的tblFruit_ID。
我试过了:
Select Distinct(tblFruit_FruitType), tblFruit_ID FROM tblFruit
-返回所有结果,而不仅仅是不同的结果
Select tblFruit_FruitType, tblFruit_ID FROM tblFruit Group By tblFruit_FruitType
-列tblFruit_ID的错误在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。
Select tblFruit_FruitType, tblFruit_ID FROM tblFruit Group By tblFruit_FruitType, tblFruit_ID
-返回所有结果,而不仅仅是不同的结果
我也检查了这些类似的职位,并没有任何工作:(
mySQL select one column DISTINCT, with corresponding other columns
SQL Server Distinct Union for one column
希望这是足够的信息来解答。
感谢您的时间!
编辑(样本数据和所需结果)
tblFruit_ID, tblFruit_FruitType, tblFruit_FruitName
int NVarChar Text
1 Citrus Orange
2 Citrus Lime
3 Citrus Lemon
4 Seed Cherry
5 Seed Banana
结果:
1 Citrus
4 Seed
最佳答案
select * from tblFruit where
tblFruit_ID in (Select max(tblFruit_ID) FROM tblFruit group by tblFruit_FruitType)