MYSQL - How to count the first name of each line? I have a customer table that has a column with the full name of the customer. I need to know the number of characters in each customer's first name
------------------------
| table client |
------------------------
| id | name |
------------------------
| 1 | Dylan Smith |
| 2 | Bruce Johnson |
| 3 | James Williams |
| 4 | Thomas Johnson |
| 5 | Jimmy Jones |
| 6 | Matthew Miller |
------------------------
I need the query to return the following:
id | qtd_caracte
1 | 5
2 | 5
3 | 5
4 | 6
5 | 5
6 | 7
Does anyone know how this is possible? already searched on google and did not find.
One method uses substring_index():
select t.*, char_length(substring_index(name, ' ', 1))
from t;
Note this conveniently works even if the name does not have a space, which is why I suggest it over position()/instr()/locate().
Use string functions:
select id, char_length(substring_index(name, ' ', 1)) nb_car
from mytable
All you need is the function LOCATE() to get the position of the first space:
select id,
locate(' ', name) - 1 qtd_caracte
from client
If there is a case that the name does not contain a space:
select id,
locate(' ', concat(name, ' ')) - 1 qtd_caracte
from client
See the demo.
Results:
> id | qtd_caracte
> -: | ----------:
> 1 | 5
> 2 | 5
> 3 | 5
> 4 | 6
> 5 | 5
> 6 | 7
Try this:
SELECT id, char_length(SUBSTRING_INDEX(`name`, ' ', 1)) AS `qtd_caracte` from client ;
Enjoy...
Related
Below are the results of the following query
Select
MOM_rebels_cms.log.AGE,
MOM_rebels_cms.log.TEAM,
MOM_rebels_cms.log.LAST_NAME,
MOM_rebels_cms.log.NUMBER,
MOM_rebels_cms.log.Game_1,
MOM_rebels_cms.log.Game_2,
MOM_rebels_cms.log.Game_3,
MOM_rebels_cms.log.Game_4,
MOM_rebels_cms.log.Game_5
From
MOM_rebels_cms.log
id | AGE | TEAM | LAST_NAME | NUMBER | GAME_1 | GAME_2 | GAME_3 | GAME_4
1 | 9 |Rebels| SMITH | 22 | 3 | 1 | 2 | 2
I would like to display the Total for the Game_x columns in the same row like:
id | AGE | TEAM | LAST_NAME | NUMBER | GAME_1 | GAME_2 | GAME_3 | GAME_4 | TOTAL
1 | 9 |Rebels| SMITH | 22 | 3 | 1 | 2 | 2 | 8
I can get the total using:
Select
MOM_rebels_cms.log.AGE,
MOM_rebels_cms.log.TEAM,
MOM_rebels_cms.log.LAST_NAME,
MOM_rebels_cms.log.NUMBER,
MOM_rebels_cms.log.Game_1 +
MOM_rebels_cms.log.Game_2 +
MOM_rebels_cms.log.Game_3 +
MOM_rebels_cms.log.Game_4
AS TOURNEYSum
From
MOM_rebels_cms.log
but it only displays:
id | AGE | TEAM | LAST_NAME | NUMBER | TOTAL
1 | 9 |Rebels| SMITH | 22 | 8
I would like to display the individual game results as well as the total.
You used the games in a sum, but did not select them otherwise.
Select
MOM_rebels_cms.log.AGE,
MOM_rebels_cms.log.TEAM,
MOM_rebels_cms.log.LAST_NAME,
MOM_rebels_cms.log.NUMBER,
MOM_rebels_cms.log.Game_1,
MOM_rebels_cms.log.Game_2,
MOM_rebels_cms.log.Game_3,
MOM_rebels_cms.log.Game_4,
MOM_rebels_cms.log.Game_1 +
MOM_rebels_cms.log.Game_2 +
MOM_rebels_cms.log.Game_3 +
MOM_rebels_cms.log.Game_4
AS TOURNEYSum
From
MOM_rebels_cms.log
Adding the individual games to your query will work just fine.
Assuming you have the same number of games in each row, you only need to select the columns individually as well as your total:
Select
MOM_rebels_cms.log.AGE,
MOM_rebels_cms.log.TEAM,
MOM_rebels_cms.log.LAST_NAME,
MOM_rebels_cms.log.NUMBER,
MOM_rebels_cms.log.Game_1,
MOM_rebels_cms.log.Game_2,
MOM_rebels_cms.log.Game_3,
MOM_rebels_cms.log.Game_4,
MOM_rebels_cms.log.Game_1 +
MOM_rebels_cms.log.Game_2 +
MOM_rebels_cms.log.Game_3 +
MOM_rebels_cms.log.Game_4
AS TOURNEYSum
From
MOM_rebels_cms.log
You can select both the individual values and the sum at once. Simplifying it a little;
SELECT id, AGE, TEAM, LAST_NAME, NUMBER,
Game_1, Game_2, Game_3, Game_4,
Game_1 + Game_2 + Game_3 + Game_4 AS TOTAL
FROM MOM_rebels_cms.log
In my MySQL database I have two tables.
Table contact_groups:
+-------------+-----------+
| groupname | fieldname |
+-------------+-----------+
| Wholesalers | grp_whs |
| Retailers | grp_rtl |
| Consumers | grp_cns |
+-------------+-----------+
Table contacts:
+---------+---------+---------+---------+
| name | grp_whs | grp_rtl | grp_cns |
+---------+---------+---------+---------+
| Tom | 0 | 1 | 1 |
| Dick | 1 | 0 | 0 |
| Harry | 0 | 1 | 0 |
| John | 0 | 1 | 1 |
| Jane | 1 | 1 | 0 |
| Anna | 1 | 0 | 0 |
| Bob | 0 | 0 | 1 |
| Charlie | 0 | 1 | 1 |
+---------+---------+---------+---------+
I need to write a single qyery that returns a list of field names and group names from the contact_groups table, with the number of names from the contacts table associated with that group concatenated to the group name. In case of the above data, that would return the following:
+-----------+-----------------+
| fieldname | groupname |
+-----------+-----------------+
| grp_whs | Wholesalers (3) |
| grp_rtl | Retailers (5) |
| grp_cns | Consumers (4) |
+-----------+-----------------+
(The reason why I need a single query producing the above output in this format is that this statement will be executed by a form generator that lack flexibility and can only execute one single query that must return fieldname and group names in this manner.)
How do I do this?
If you want the ans to be in this specific format use CONCAT(groupname, '(', value,')' )
you will get some thing like this Wholesalers (3).
This is what I could think off
select fieldname, concat(groupname,' (',
case
when fieldname = 'grp_whs' then
(
select sum(grp_whs) from contacts
)
when fieldname = 'grp_rtl' then
(
select sum(grp_rtl) from contacts
)
when fieldname = 'grp_cns' then
(
select sum(grp_cns) from contacts
)
END
,' )') as groupname
from contact_groups
Check here
http://sqlfiddle.com/#!2/a235f/4
I am assuming you have only 3 fieldname . If there are more than that then I do not think its the best solution , someone may have a better idea on this.
But for your case this should work.
try...
select fieldname ,
case fieldname
when 'grp_whs'
then groupname + '(' + convert(varchar(50),(select count(name) from contacts where grp_whs=1)) + ')'
when 'grp_rtl'
then groupname + '(' + convert(varchar(50),(select count(name) from contacts where grp_rtl=1)) + ')'
when 'grp_cns'
then groupname + '(' + convert(varchar(50),(select count(name) from contacts where grp_cns=1)) + ')'
END as groupname
FROM contact_groups
I have a MySQL table StudentName(id,name) that looks like this:
id | name
++++++++
1 | alex
1 | adam
1 | adnan
2 | ben
2 | bush
3 | cris
4 | daisi
4 | diana
And I'd like to make a new table like this:
id | name
+++++++++++
1 | alex, adam, adnan
2 | ben, bush
3 | cris
4 | daisi, diana
Is there a way to accomplish this?
The group_concat function is what you're looking for:
SELECT id, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ')
FROM my_table
GROUP BY id
If the table has the following data
num | date_ | s | i_id
-------+-------------+------------------------+---------
1 | 2013-12-12 | (2,1,2013-12-12,80.56) | 2
1 | 2013-12-12 | (3,1,2013-12-12,70.56) | 3
1 | 2013-12-10 | (4,1,2013-12-10,90.76) | 4
2 | 2013-12-10 | (5,2,2013-12-10,90.76) | 5
2 | 2013-12-06 | (6,2,2013-12-06,90.76) | 6
3 | 2013-12-06 | (7,3,2013-12-06,90.76) | 7
3 | 2013-12-06 | (8,3,2013-12-06,90.76) | 8
i want a query which will give num,i_id for the records which have different dates for same num.
It should return num-1,2 and the corresponding i_id.
How should i proceed?
This should work in PostgreSQL with an version number >= 9.0
SELECT
num
, array_to_string(array_agg(i_id ORDER BY num), ',')
FROM a
GROUP BY num
see demo http://sqlfiddle.com/#!15/25502/5
And it look like in your comment that you want to have the MAX(date_)
SELECT
num
, MAX(date_)
, array_to_string(array_agg(i_id ORDER BY num), ',')
FROM a
GROUP BY num
see demo http://sqlfiddle.com/#!15/25502/6
select num, group_concat(i_id) as iids
from your_table
group by num
having count(distinct date_) > 1
I have a table with following structure:
+-----+-------------------+
| ID | Name |
+-----+-------------------+
| 1 | abc |
+-----+-------------------+
| 2 | abc (duplicate) |
+-----+-------------------+
| 3 | bcd |
+-----+-------------------+
| 4 | bcd (duplicate) |
+-----+-------------------+
| 5 | bcd (duplicate) |
+-----+-------------------+
| 6 | efg |
+-----+-------------------+
| 7 | hij |
+-----+-------------------+
I need to count each Name occurance (with (duplicate) included), i.e.:
+-------------------+--------+
| Name | Count |
+-------------------+--------+
| abc | 2 |
+-------------------+--------+
| bcd | 3 |
+-------------------+--------+
| efg | 1 |
+-------------------+--------+
| hij | 1 |
+-------------------+--------+
I want to mention, that Name column is actually have type TINYTEXT. And there will be very lot of rows in it: 5396 in test mode already. I tried to make self join of table by TRIM(REPLACE(Name, '(duplicate)', '')) with grouping:
SELECT
DISTINCT TRIM(REPLACE(`t`.`Name`, '(duplicate)', '')) as `name`,
COUNT(`s`.`ID`) as `count`
FROM
`Table` as `t` INNER JOIN `Table` as `s` ON
TRIM(REPLACE(`t`.`Name`, '(duplicate)', '')) LIKE TRIM(REPLACE(`s`.`Name`, '(duplicate)', ''))
GROUP BY 1;
And... Well, it took 122.62 sec (?!) with result of 4846 rows on my development machine.
Q1: Was it a correct approach?
Q2: Is there any way to make it faster?
Any help would be appreciated.
Just remove the "duplicate" text:
select replace(name, ' (duplicate)', ''), count(*)
from mytable
group by 1
This should be quicker, although with that many rows you're basically storing a growing array of objects that you're counting and since it's a TINYTEXT field it can be immense.
select Name,count(ID) from Table group by Name
I see what you're saying now. Here's an updated SQL:
select DISTINCT TRIM(REPLACE(Name, ' (duplicate)', ''))
as name, count(ID) from Table group by name