简体   繁体   中英

Count Length of Digits Per Column Per Row

I generated the following table:

CREATE table user (  
    user_id INT NOT NULL UNIQUE AUTO_INCREMENT,  
    user_phone_number INT,  
    user_city VARCHAR(32) NOT NULL,  
    PRIMARY KEY (user_id)
);

And I'm being asked the following: number of users per length of the phone number (number of numeric digits)

I try the following:

SELECT LENGTH(user_phone_number)
FROM user
WHERE user is not null
GROUP BY user_phone_number);

but if there are several users with the same phone number length it repeats them in several rows instead of providing the Total per length. Please help

You have two problems with your SQL. The most obvious is that you have left out the COUNT() . This is important if you want to count something.

The second is that the SELECT column and the GROUP BY column don't match. These should (almost always) be the same:

SELECT LENGTH(user_phone_number), COUNT(*)
FROM user
WHERE user_phone_number is not null
GROUP BY LENGTH(user_phone_number);

You probably want an ORDER BY LENGTH(user_phone_number) at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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