MySQL fields from different rows to one string - mysql

I have table with data like this:
Id sort name
1 1 abc
1 2 kci
2 1 asd
2 2 eww
2 3 der
2 4 acu
3 1 awq
3 2 see
3 3 eee
is it possible to create query that will give me result
Id ConcName
1 abc,kci
2 asd,eww,der,acu
3 awq,see,eee

use GROUP_CONCAT()
SELECT ID,
GROUP_CONCAT(name ORDER BY sort ASC) ConcName
FROM tableName
GROUP BY ID
SQLFiddle Demo
GROUP_CONCAT()

SELECT `id`, GROUP_CONCAT(`name` ORDER BY `sort`)
FROM `myTable`
GROUP BY `Id`

Related

Need MySql query that should return result by ordering values in particular field

Need MySql query that should return result by ordering values in particular field.
From below my result set should contain order like parent_id (1,4,6) should come first parent_id(2,3,7) come next and other should come last.
d data parent_id
----------------------
1 a1 1
2 abc 3
3 abcd 4
4 xyz 2
5 zxyy 6
2 abc 8
3 abcd 9
4 xyz 2
5 zxyy 15
Use a CASE expression in your ORDER BY clause:
SELECT d, data, parent_id
FROM yourTable
ORDER BY CASE WHEN parent_id IN (1, 4, 6) THEN 1
WHEN parent_id IN (2, 3, 7) THEN 2
ELSE 3 END,
parent_id
Follow the link below for a running demo:
SQLFiddle
Use below mentioned query
SELECT d, data, parent_id FROM table_name ORDER BY FIELD(parent_id,1,4,6,2,3,7);

Get the duplicate rows with the number of occurrence in MYSQL

I need to list all the duplicate IDs with the number of occurrence of each ID in a single MYSQL query.
ID
____
1
1
2
3
4
4
4
5
5
6
7
Output must be:
ID | Occurrence
_______________
1 | 2
4 | 3
5 | 2
Just use a simple GROUP BY query:
SELECT ID, COUNT(*) AS Occcurrence
FROM yourTable
GROUP BY ID
HAVING COUNT(*) > 1
This can be done simply by using Group By clause and Count() function
select
Id, count(id) as Occurance
from
tableName
group by id
having Occurance > 1;
use mysql GROUP BY
select ID,count(*) from table_name group by ID having count(*) > 1

Find duplicate records in MySQL without named column

I have a table like this:
**lead_id** **form_id** **field_number** **value**
1 2 1 Richard
1 2 2 Garriot
2 2 1 Hellen
2 2 2 Garriot
3 2 1 Richard
3 2 2 Douglas
4 2 1 Tomas
4 2 2 Anderson
Where field_number = 1 is the name and field_number = 2 is the surname.
I would like to find entries that are equal by name OR surname and group them by lead_id, so the output could be like this:
1
2
3
Any thoughts on how this can be done?
This should work and be reasonably efficient (depending upon indexes):
select distinct lead_id
from tablename as t1
where exists (
select 1
from tablename as t2
where t1.field_number = t2.field_number
and t1.value = t2.value
and t1.lead_id <> t2.lead_id
)
Select leadid from (
Select DISTINCT leadid,value from tablename
Where fieldnumber=1
Group by leadid,value
Having count(value) >1
Union all
Select DISTINCT leadid,value from tablename
Where fieldnumber=2
Group by leadid,value
Having count(value) >1
) as temp
Surely there is a faster option

Update Order column with number increasing (depend on ID column)

I'm using the SQL Server 2008.
Now I have the scenario as description below:
1 table with 3 columns: ID, Name, Order.
They has 8 records.
5 records have the same data: ID='1', Name='AAA'
3 records have the same data: ID='2', Name='BBB'
Now I want to update Order column with number increasing (start from 1) for each ID,Name:
No Name Order
1 AAA 1
1 AAA 2
1 AAA 3
1 AAA 4
1 AAA 5
2 BBB 1
2 BBB 2
2 BBB 3
How can I get this result without using Cursors?
I'm very appriciated for your help.
Thanks.
You can try the following. I use CTE to drive the update statement
WITH data AS
(
SELECT Order
, ROW_NUMBER() OVER (PARTITION BY ID, NAME
ORDER BY ID, NAME) AS Seq
FROM TableA
)
UPDATE data
SET Order = Seq
Substitute TableA with your table name

Multiple Value for Custom Column

I would like to each ID have 2 Custom_Column values.
How can I do that?
Example Table:
ID
1
2
3
Example Query:
SELECT id, (id*2) as Custom_Value, (id*4) as Custom_Value FROM numbers
Response:
ID Custom_Value
1 2
1 4
2 4
2 8
3 6
3 12
use UNION
SELECT ID, (ID*2) as CustomVal FROM tableNAme
UNION ALL
SELECT ID, (ID*4) as CustomVal FROM tableNAme
ORDER BY ID
SQLFiddle Demo