Ad

How To Add String Or An Of Characters In Mysql While Using Group By Function

- 1 answer

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.

Ad

Answer

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

Ad
source: stackoverflow.com
Ad