Multiple Value for Custom Column - mysql

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

Related

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

MySQL: copy data from table to another

I need to copy "user1_id" & "user2_id" from "battle" table to new table called "user_battle",
tables structure:
"battle" table (structure):
id user1_id user2_id
1 1 2
2 1 3
3 2 3
4 2 4
5 1 4
"user_battle" table (structure): (should be filled like this)
battle_id user_id
1 1
1 2
2 1
2 3
3 2
3 3
4 2
4 4
5 1
5 4
thanks,
I think you can use union all to get the data you want:
select id, user1_id
from battle
union all
select id, user2_id
from battle;
You can put insert into user_battle(battle_id, user_id) before this statement for the insert.
insert into allows you to use a select to effectively "copy" data from one table to another.
By using union you can get separate rows for user1_id and user2_id.
insert into user_battle (battle_id, user_id)
(
select id, user1_id from battle
union
select id, user2_id from battle
)
insert into user_battle select id, user1_id from battle
And
insert into user_battle select id, user2_id from battle
Use a union.
INSERT INTO user_battle (battle_id, user_id)
SELECT id, user1_id
FROM battle
UNION ALL
SELECT id, user2_id
FROM battle;

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

MySQL fields from different rows to one string

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`