【问题标题】:Counting the Number of Rows That Have a Spesific Value in a Column in a Table in SQLSQL统计表中某列具有特定值的行数
【发布时间】:2021-10-08 04:09:38
【问题描述】:
id | street | city | country | postal code |
--------------------------------------------
0  |a street|x city| Turkey  | 12345       | 
1  |b street|y city| Turkey  | 12335       | 
2  |c street|z city| USA     | 12315       | 
3  |d street|j city| Turkey  | 32345       | 
4  |e street|k city| Germany | 12135       | 
5  |f street|l city| France  | 13215       | 
6  |g street|m city| Turkey  | 42135       | 
7  |h street|n city| Italy   | 12135       | 
8  |i street|z city| Spain   | 32115       | 

你好。假设我们有一个像上面那样名为“person_address”的表。在 db 中有很多这样的不同表。我想在 person_address 表的“country”列中找到值为“Turkey”的行数,即 4。如何将其转换为 postgresql?

SELECT * FROM person_address u WHERE u.country = 'Turkey'; 

使用此代码,我可以列出我想要的内容,但我需要该列表的编号。之后我必须在 Java spring boot 项目中使用它。应该使用@Query 注释还是有更好的方法?

【问题讨论】:

标签: sql postgresql


【解决方案1】:

如果你输入你想要的输出,你会很快得到答案。以下查询返回已列出 4 次的国家/地区。

select *
from
   (select *,
        count(*) over(partition by country order by country) as numberOfCountry
    from person_address) t
where numberOfCountry = 4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2012-06-20
    • 2014-09-21
    相关资源
    最近更新 更多