Home > other >  how to add string or an of characters in mysql while using group by function
how to add string or an of characters in mysql while using group by function

Time:03-22

For example, I have a table named XYZ and it has a column COUNTRY, how can I take out results in the following format using group by function,

INDIA has 3 employees

here 3 is opted from count() and INDIA is grouped by "GROUP BY", my question is that how do you print 'has' and employees in between and at the end in mySQl

I am learning DBMS in Oracle APEX. THANKS.

CodePudding user response:

In MySQL / MariaDB you want

SELECT CONCAT_WS(' ', country, 'has', COUNT(*), 'employees')

In Oracle you want

SELECT country || ' has ' || COUNT(*) || ' employees

CodePudding user response:

You can use || to concatenate strings in the SELECT

CREATE TABLE Employee (
id int, 
country varchar(10));
INSERT INTO Employee VALUES (1,'India');
INSERT INTO Employee VALUES (2,'India');
INSERT INTO Employee VALUES (3,'India');
SELECT country || ' has ' || COUNT(id) ||  ' employees' AS report
FROM Employee
GROUP BY country
| REPORT                |
| :-------------------- |
| India has 3 employees |

db<>fiddle here

  • Related