Mysql show other different cell data with under group by same name - mysql

Let's assume I have a table which store register user data, the records might have same registered name but different email, like following:
I want to create a front view to manipulate those data but I don't want those same name show repeatedly, can mysql statement query to output result like
this is the result so far I can do but it can't bind same name into one.
select * from `register`
where `fullname` in (
select `fullname` from `register`
group by `fullname` having count(*) > 1
)

One thing you could do is to do a SELECT DISTINCT on the duplicate row, and make use of the GROUP_CONCAT(); function in MYSQL to concatenate your desired values into one row, and GROUP BY fullname to get the order you wanted.
Note that I am also putting the user ids into a grouped row, so that you can track which ids belong to which name.
SELECT
DISTINCT fullname as full_name,
GROUP_CONCAT(id SEPARATOR ', ') as user_ids,
GROUP_CONCAT(email SEPARATOR ', ') as emails
FROM
tbl_register
GROUP BY
tbl_register.fullname
Working SQL Fiddle
This would be the logical way to do it. Hope this helped. :)
More information on the GROUP_CONCAT(); function here: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat

Try this:
SELECT DISTINCT *duplicate_column* FROM *table_name1* WHERE *col_id* IN (SELECT *cols_to_dusplay* FROM *table_name1* GROUP_BY *duplicate_column*

Related

How can I use an IF or Case function to summarize a GROUP_CONCAT column? AND then apply it to the original data table?

I am quite the novice at MYSQL and would appreciate any pointers - the goal here would be to automate a categorical field using GROUP_CONCAT in a certain way, and then summarize certain patterns in the GROUP_CONCAT field in a new_column. Furthermore, is it possible to add the new_column to the original table in one query? Below is what I've tried and errors to an unknown column "Codes" if this assists:
SELECT
`ID`,
`Code`,
GROUP_CONCAT(DISTINCT `Code` ORDER BY `Code` ASC SEPARATOR ", ") AS `Codes`,
IF(`Codes` LIKE '123%', 'Description1',
IF(`Codes` = '123, R321', 'Description2',
"Logic Needed"))
FROM Table1
GROUP BY `ID`
Instead of nested if statements, I would like to have a CASE statement as a substitute. Reason being is that I already have around 1000 lines of logical already written as "If [column] = "?" Then "?" else if" etc. I feel like using CASE would be an easier transition with the logic. Maybe something like:
SELECT
`ID`,
`Code`,
GROUP_CONCAT(DISTINCT `Code` ORDER BY `Code` ASC SEPARATOR ", ") AS `Codes`,
CASE
WHEN `Codes` LIKE '123%' THEN 'Description1'
WHEN `Codes` = '123, R321' THEN 'Description2'
ELSE "Logic Needed"
END
FROM Table1
GROUP BY `ID`
Table Example:
ID,Code
1,R321
1,123
2,1234
3,1231
4,123
4,R321
Completed Table:
ID,Codes,New_Column
1,"123, R321",Description2
2,1234,Description1
3,1231,Description1
4,"123, R321",Description2
How then can I add back the summarized data to the original table?
Final Table:
ID,Code,New_Column
1,R321,Description2
1,123,Description2
2,1234,Description1
3,1231,Description1
4,123,Description2
4,R321,Description2
Thanks.
You can't refer to a column alias in the same query. You need to do the GROUP_CONCAT() in a subquery, then the main query can refer to Codes to summarize it.
It also doesn't make sense to select Code, since there isn't a single Code value in the group.
SELECT ID, Codes,
CASE
WHEN `Codes` = '123, R321' THEN 'Description2'
WHEN `Codes` LIKE '123%' THEN 'Description1'
ELSE "Logic Needed"
END AS New_Column
FROM (
SELECT
`ID`,
GROUP_CONCAT(DISTINCT `Code` ORDER BY `Code` ASC SEPARATOR ", ") AS `Codes`
FROM Table1
GROUP BY ID
) AS x
As mentioned in a comment, the WHEN clauses are tested in order, so you need to put the more specific cases first. You might want to use FIND_IN_SET() rather than LIKE, since 123% will match 1234, not just 123, something

MySQL group by issue on text field (Field contain HTML)

I have a template table. A single template can have multiple HTML pages that stored in template_pages table.
A complete template is combination of multiple pages.
My requirement is to check duplicate templates. For that I have to match concatenate pages and match to check duplicates. I have build following query for that
Select Count(id) as count, GROUP_CONCAT(id SEPARATOR ', ') as duplicate_templates from
(select template_id as id, GROUP_CONCAT(TRIM(template_pages.template) SEPARATOR ', ') as full_template from template_pages
left join templates on templates.id = template_pages.template_id
where template_pages.template != ""
and templates.deleted_at IS NOT NULL
group by template_id
) as templates
group by full_template having count(full_template) >1;
But this query not giving exact results. Its matching some unmatched records. Some template have some similar data but not exactly same also coming in duplicates.
#ajay, seems you have issue at Where cluse with DELETE_AT column. Please verify that.
It's working for me.

Masking values in SQL select return

This is an odd question, but I was wonder if you could display one column in the results, but really have another column as the values (MYSQL). Suppose I have this table:
ID Name
1 Soccer
2 Football
I was wonder if it was possible to select all IDs from this table (select ID from table), but the results would display the name instead.
Or is it possible to display as the result (as a single column)?:
1 (Soccer)
2 (Football)
SELECT ID, CONCAT("(", name, ")") FROM <TABLENAME>
This will have results concatenated into single column.
SELECT CONCAT(ID, ' (', Name, ')')
FROM tableName
SQLFiddle Demo

MySQL GROUP_CONCAT multiple fields

I'm probably having a no-brain moment.
I want to return a series of numbers using GROUP_CONCAT from two fields in my database. I have done this so far using the following:
SELECT t_id,
CONCAT(GROUP_CONCAT(DISTINCT s_id),',',IFNULL(GROUP_CONCAT(DISTINCT i_id),'')) AS all_ids
FROM mytable GROUP BY t_id
This works fine but if i_id is NULL then of course I get an unnecessary comma. Is there a better way to do this so I don't end up with a comma at the end if i_id is NULL?
You need to use CONCAT_WS to avoid extra comma for NULL values, try this:
SELECT t_id,
CONCAT_WS(',', GROUP_CONCAT(DISTINCT s_id),
GROUP_CONCAT(DISTINCT i_id)) AS all_ids
FROM mytable
GROUP BY t_id;

MySQL query results used in LIKE

I am trying to write a query to pull all the rows that contain a username from a large list of usernames in a field.
For example, the table contains a column called 'Worklog' which contains comments made by users and their username. I need to search that field for all user names that are contained in a list I have.
I have tried a few different things but can't get anything to work. So far, this is kind of what I have tried:
SELECT *
FROM `JULY2010`
WHERE `WorkLog`
IN (
SELECT CONCAT( '%', `UserName` , '%' )
FROM `OpsAnalyst`
)
The problem is I need to use LIKE because it is searching a large amount of text, but I also have a large list that it is pulling from, and that list needs to be dynamic because the people that work here are changing frequently. Any ideas?
SELECT *
FROM `JULY2010`
WHERE `WorkLog` REGEXP
(SELECT CONCAT( `UserName`, '|')
FROM `OpsAnalyst`)
I slightly modified this and used GROUP_CONCAT() and now my query looks like this:
SELECT *
FROM JULY2010
WHERE `WorkLog`
REGEXP (
SELECT GROUP_CONCAT(`UserName` SEPARATOR '|') FROM `OpsAnalyst`
)
I am now getting a result set, but it seems like it isn't as many results as I should be getting. I'm going to have to look into it a little more to figure out what the problem is