expresssion field = xxx after group by

时间:2018-03-15 09:25:41

标签: mysql group-by

mysql服务器版本:mysql 5.6 客户端:datagrip

我期望的sql是

select * from user GROUP BY level;

但我错误地执行了

select * from user GROUP BY level=1;

我认为mysql会抛出一个错误,但它不会。 和mysql给我一行。

为什么?

1 个答案:

答案 0 :(得分:0)

它将返回表格的最后一行和匹配的行,其中level = 1.

例如:

CategoryID    CategoryName      Description
1             Beverages         Soft drinks, coffees, teas, beers, and ales
2             Condiments        Sweet and savory sauces, relishes, spreads, and seasonings
3             Confections       Desserts, candies, and sweet breads
4             Dairy Products    Cheeses
5             Grains/Cereals    Breads, crackers, pasta, and cereal
6             Meat/Poultry      Prepared meats
7             Produce           Dried fruit and bean curd
8             Seafood           Seaweed and fish

以下查询:

SELECT CategoryID,CategoryName,Description FROM Categories GROUP BY CategoryID = 3

将导致:

CategoryID  CategoryName    Description
8            Seafood        Seaweed and fish
3            Confections    Desserts, candies, and sweet breads