简体   繁体   English

Select 字段,其中列具有多个元素 SQL

[英]Select field where a column has more than n elements SQL

look this:看看这个:

Given the table "Customer" that has the following columns:给定具有以下列的“客户”表:

• Name1  First name of the customer.
• Name2  Second Name of the customer.
• Surname1  Customer's First Surname.
• Surname2  Second Surname of the client.
• T_Documento  Type of customer document
• N_Document  Customer document number
• Country_Residence  Country where the Client Resides

PROBLEN TO SOLVE: Write a check that lists the Countries of Residence with less than 10 clients.难以解决:写一张支票,列出少于 10 个客户的居住国。

I have been looking for the right code but I still with out find any solution我一直在寻找正确的代码,但我仍然没有找到任何解决方案

SELECT * (
SELECT pais_residencia, COUNT(*) AS ClientesTotales
FROM Clientes
GROUP BY pais_residencia
) FROM Clientes
WHERE ClientesTotales > 10

this is wrong btw...这是错误的顺便说一句...

any idea任何想法

Your code should work, provided that you change > to < in the outer query (and add FROM in the outer query to introduce the subquery).您的代码应该可以工作,前提是您在外部查询中将>更改为< (并在外部查询中添加FROM以引入子查询)。

But it can be simplified.但是可以简化。 You don't need a subquery to filter on the count, you can use a having clause instead:您不需要子查询来过滤计数,您可以使用having子句代替:

SELECT pais_residencia, COUNT(*) AS ClientesTotales
FROM Clientes
GROUP BY pais_residencia 
HAVING COUNT(*) < 10

MySQL even supports reusing alias in the HAVING clause, which can be phrased: MySQL 甚至支持在HAVING子句中重用别名,可以表述为:

HAVING ClientesTotales < 10

If you want less than 10 clientes then you want:如果您想要少于 10 个客户,那么您想要:

SELECT * 
FROM (SELECT pais_residencia, COUNT(*) AS ClientesTotales
      FROM Clientes
      GROUP BY pais_residencia
     ) r
WHERE ClientesTotales < 10;

The direction of your comparison is wrong, based on the question.根据问题,您的比较方向是错误的。

In addition, there are some other syntactic problems with your query -- the subquery should be part of the FROM clause, for instance.此外,您的查询还有其他一些语法问题——例如,子查询应该是FROM子句的一部分。

You don't need a subquery for this.您不需要为此设置子查询。 You can just use HAVING :您可以只使用HAVING

If you want less than 10 clientes then you want:如果您想要少于 10 个客户,那么您想要:

SELECT pais_residencia, COUNT(*) AS ClientesTotales
FROM Clientes
GROUP BY pais_residencia
HAVING ClientesTotales < 10;

If you need to display all data from a table, you can use this code如果需要显示表格中的所有数据,可以使用此代码

select
     name1,
     name2,
     surname1,
     surname2,
     t_documento,
     N_Document,
     country_residence
 FROM
     (
         SELECT
             name1,
             name2,
             surname1,
             surname2,
             t_documento,
             n_document,
             country_residence,
             COUNT(country_residence) OVER(
                 PARTITION BY country_residence
             ) clientestotales
         FROM
             clientes
     )
 WHERE
     clientestotales < 10;

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

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