简体   繁体   English

如何在Hypersql数据库(HSQLDB)中选择多列但仅按一列分组?

[英]How to select multiple columns but group by only one column in Hypersql database(HSQLDB)?

I am using HSQLDB-2.3.3 version, here I'm showing a simple MySQL query. 我正在使用HSQLDB-2.3.3版本,这里显示了一个简单的MySQL查询。

Example: 例:

SELECT name,age,emailid,country FROM players GROUP BY country;

But if I do same query in HyperSQL it shows an error, "Expression not in aggregate or group by columns PUBLIC.PLAYERS.NAME" 但是,如果我在HyperSQL执行相同的查询,则会显示错误, "Expression not in aggregate or group by columns PUBLIC.PLAYERS.NAME"

And if I apply group to every column that I selected with select statement, it display results with aggregation of all column. 而且,如果我将group应用于使用select语句选择的每个列,它将显示所有列聚合的结果。

My question is, how to display multiple columns with one column aggregate ( GROUP BY ) in HyperSQL ?? 我的问题是,如何在HyperSQL以一列聚合( GROUP BY )显示多列?

Table Players : 桌上型Players

pid name emailid country region age pid name emailid country region age

1 Samual Samual@gmail.com INDIA DELHI 25 1 Samual Samual@gmail.com INDIA DELHI 25

2 Vino Vino@gmail.com INDIA DELHI 20 2 Vino Vino@gmail.com INDIA DELHI 20

3 John John@gmail.com INDIA DELHI 20 3 John John@gmail.com INDIA DELHI 20

4 Andy Andy@gmail.com INDIA DELHI 22 4 Andy Andy@gmail.com INDIA DELHI 22

5 Brian Brian@hotmail.com America DELHI 21 5 Brian Brian@hotmail.com America DELHI 21

6 Dew Dew@hotmail.com America DELHI 24 6 Dew Dew@hotmail.com America DELHI 24

7 Kris Kris@hotmail.com America DELHI 25 7 Kris Kris@hotmail.com America DELHI 25

8 William William@hotmail.com INDIA DELHI 26 8 William William@hotmail.com INDIA DELHI 26

9 George George@hotmail.com INDIA DELHI 23 9 George George@hotmail.com INDIA DELHI 23

10 Peter Peter@gmail.com INDIA DELHI 19 10 Peter Peter@gmail.com INDIA DELHI 19

11 Tom Tom@gmail.com America DELHI 20 11 Tom Tom@gmail.com America DELHI 20

12 Andre Andre@hotmail.com INDIA DELHI 20 12 Andre Andre@hotmail.com INDIA DELHI 20

Expected Result : 预期Result

name age emailid country name age emailid country

Brian 21 Brian@hotmail.com America Brian 21 Brian@hotmail.com America

Samual 25 Samual@gmail.com INDIA Samual 25 Samual@gmail.com INDIA

The implementation of GORUP BY in MySQL is different from other databases that follow the SQL Standard. MySQL中GORUP BY的实现与遵循SQL标准的其他数据库不同。 In this case, the query shouldn't work. 在这种情况下,查询不起作用。

The result that you expect contains the person with the lowest pid for each country. 您期望的结果包含每个国家/地区的pid最低的人。 You can write a query that explicitly asks for this: 您可以编写一个明确要求此查询的查询:

SELECT name, age, emailid, country FROM players 
    WHERE pid IN (SELECT MIN(pid) FROM players GROUP BY country)

The query first finds the lowest pid for each country. 该查询首先找到每个国家的最低pid It then selects the two rows that contain these pid values. 然后,它选择包含这些pid值的两行。

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

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