mysql, find frequenties in table - mysql

I have a table in PHPMyAdmin with six columns.
In each cell there is a name.
Now I want to know in what frequencies we have each name in each column.
For example:
column1 column2 column3
name1 name3 name2
name1 name2 name2
name2 name3 name1
Then I need a list with:
column1 column2 column3
name1 - 2 0 1
name2 - 1 1 2
name3 - 0 2 0
I tried to play with:
SELECT Count(*) FROM aanmeldingen2013 WHERE column1 LIKE name1.
Can someone help me with the SQL code to generate this output?

I think this is the most efficient method:
select name,
sum(n = 1) as Column1Cnt,
sum(n = 2) as Column1Cnt,
sum(n = 3) as Column1Cnt
from (select (case when n.n = 1 then column1
when n.n = 2 then column2
when n.n = 3 then column3
end) as name,
n.n
from t cross join
(select 1 as n union all select 2 union all select 3) n
) t
This should be more efficient that a union all query because it only scans the original table once. I've shown the example here for three columns (as in your sample data). It should be clear how to generalize this to six columns.

check this query
SELECT COUNT( * ) FROM 'tbl_name' where column1='name1';

Related

Selecting distinct 5 columns combination in mysql

I have a mysql table that looks like this: Col 1 is UNIQUE.
1 value1 value2 0 2
2 value1 value2 1 3
3 value3 value4 3 2
4 value4 value5 4 1
5 value3 value4 3 1
I need a query to select all the rows with distinct column 1 and 2, for example the output I want for this example will look like this:
1 value1 value2 0 2
3 value3 value4 3 2
4 value4 value5 4 1
I need distinct col 1 and 2 but altogether all columns combination will be distinct always. I want to display distinct col 1,2 and 3 without col 2,3 repeating.
I've found a few samples on how to do it but they all select distinct on each column individually. I tried many stackoverflow answers too. But my question is different.
One method that performs well is a correlate subquery:
select t.*
from t
where t.col1 = (select min(t2.col1)
from t t2
where t2.col2 = t.col2 and t2.col3 = t.col3
);
For best performance, you want an index on (col2, col3, col1).
I strongly advise having a primary key on all tables, but if you did not have one, then row_number() would be the way to go:
select t.*
from (select t.*,
row_number() over (partition by col2, col3 order by col2) as seqnum
from t
) t
where seqnum = 1;
This incurs a tad more overhead because row numbers need to be assigned to all rows before they are filtered for only first one.
It could be achieved by using ROW_NUMBER:
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY col2, col3 ORDER BY col1) AS rn
FROM tab) sub
WHERE rn=1

MySql how to use UPDATE with HAVING?

My table1 looks like:
id name_co name_r temp sld
1 name1 1 ... ...
2 name2 1 ... ...
3 name2 1 ... ...
4 name2 1 ... ...
5 name3 1 ... ...
6 name2 1 ... ...
I need to increment name_r if there are two or more identical name_co.
To be so:
id name_co name_r temp sld
1 name1 1 ... ...
2 name2 1 ... ...
3 name2 2 ... ...
4 name2 3 ... ...
5 name3 1 ... ...
6 name2 4 ... ...
I tried different options and I came to this:
UPDATE table1
SET name_r = name_r + 1
WHERE (SELECT COUNT(*)
GROUP BY name_co
HAVING name_co > 1)
The query works and returns 0 rows, but I know that in some way he's wrong, but I can't figure out what. Can anyone help? (And a bit of explanation, so I better understood)
--updated intended targets
UPDATE table1
SET name_r = name_r + 1
WHERE id IN
(
-- return those ids again (to avoid the mysql #1093 error)
SELECT id
FROM
( -- get all the ids for those names
SELECT id
FROM table1
WHERE name_co IN
( -- get all names that have more than one id
SELECT name_co
FROM table1
GROUP BY name_co
HAVING COUNT(id) > 1
)
) a
)

yes I tried lot but unable to find repeated value

Kindly answer if possible.
Query to select record of table repeatedly two or more times to each row.
Like:
ID1 Name1 Age1
ID1 Name1 Age1
ID1 Name1 Age1
ID2 Name2 Age2
ID2 Name2 Age2
ID2 Name2 Age2
.. .. An so on.
Combination of UNION ALL and ORDER BY will do.
(SELECT * FROM TABLE_NAME) UNION ALL
(SELECT * FROM TABLE_NAME) UNION ALL
(SELECT * FROM TABLE_NAME)
ORDER BY 1,2,3
The above example is for 3 repeated times. If you want more, just add more UNION statements.

Cannot figure a simple mysql query out

I feel very very stupid now because I have a problem and cannot seem to figure it out.
Very simple MySQL table with 2 columns :
ID1 | ID2
1 | 1
1 | 2
2 | 1
Don't know very good how to explain the conditions : I want to select the value 1 from the column ID1 because it has connections with the values 1 AND 2 from ID2.
It's somewhat the opposite of IN.
If I make
SELECT ID1 FROM X WHERE ID2 IN (1,2) I recieve both 1 and 2 because it is a reunion. I want an intersection, something like SELECT ID1 FROM X WHERE ID2 IN BOTH 1 AND 2.
I am fairly sure it has something to do with grouping.
1 solution is to make
SELECT * FROM
(SELECT ID1, GROUP_COCAT(ID2) y
FROM X
GROUP BY ID1)t
WHERE t.y = '1,2'
but this is NOT ok because I do not know the order ( 1,2 or 2,1 ) and I can have more values.
Hopefully this is clear enough, I am very tired.
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2)
GROUP BY t.id1 HAVING COUNT(*) = 2
OR
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2, 3, 4)
GROUP BY t.id1 HAVING COUNT(*) = 4

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