[SOLVED] how to add string or an of characters in mysql while using group by function

Issue

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.

Solution

In MySQL / MariaDB you want

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

In Oracle you want

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

Edit This is plain old string processing. Every language, including every SQL dialect, has its own ways of doing string processing.

You can generate any sort of string you want, either with Oracle’s || or MariaDB / MySQL’s CONCAT_WS(). For your example, it’s

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

or

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

Answered By – O. Jones

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *