Find more than three columns having equal value in mysql - mysql

I have multiple columns have equal value. I want to find the row id which have more than 3 columns have equal value.
#each column is text/blob
Table structure is like -
id col1 col2 col3 col4 col5 col6 col7 col8 col9

Unpivot the data and check for equality, Try this trick.
SELECT DISTINCT id
FROM (SELECT id,col1 AS col from Yourtable
UNION ALL
SELECT id,col2 from Yourtable
UNION ALL
SELECT id,col3 from Yourtable
UNION ALL
SELECT id,col4 from Yourtable
Union ALL
.......) A
GROUP BY id,col
HAVING Count(1) > 3

Related

Using UNION ALL with the same conditions

I have 22 tables where each has many columns. I want to select 10 columns conditioning on 4 column values using WHERE. For this, I have to repeat these 4 conditions and 10 columns for all 22 tables, which is inconvenient. Is there a more efficient way to do this?
If the 22 tables have the same structure and the conditions on 4 columns are equal or similar, you can union tables in a subquery and put only one where externally.
Example:
select x.*
from
(
select col1, col2, col3, col4
from tab1
union all
select col1, col2, col3, col4
from tab2
union all
select col1, col2, col3, col4
from tab3
) x
where x.col1 = 'value'
and x.col2 = 'other value'

Averages by multiple columns separately

I have a collection table1 with the following columns:
id (INT)
col1 (VARCHAR)
col2 (VARCHAR)
value (INT)
I want to calculate the average separately by col1 and by col2 to have a response like this:
{
averageByCol1: {col1Value1: 23, col1Value2: 44},
averageByCol2: {col2Value1: 33, col2Value2: 91}
}
Tried to use multiple columns in GROUP BY, but this combines the columns:
SELECT
CONCAT(col1, col2, AVG(value))
FROM table1
GROUP BY col1, col2
Also tried with subquery but it gives me Subquery returns more than 1 row error:
SELECT
(SELECT
CONCAT(col1, AVG(value))
FROM table1
GROUP BY col1) AS col1Averages,
(SELECT
CONCAT(col2, AVG(value))
FROM table1
GROUP BY col2) AS col2Averages;
Using Mysql v5.5.
edit with sample data:
id col1 col2 value
1 v1 b1 34
2 v2 b1 65
3 v1 b1 87
4 v1 b2 78
5 v2 b2 78
6 v1 b2 12
Want average of value by v1, v2, b1, and b2 independently.
Use a UNION for each column you want to calculate an average for
SELECT col1 as col_key, avg(value) as average
FROM test
GROUP BY col1
UNION
SELECT col2, avg(value)
FROM test
GROUP BY col2
this will work:
select avg(value),col1 from Table1 group by col1
union all
select avg(value),col2 from Table1 group by col2
sql fiddle:http://sqlfiddle.com/#!9/c1f111/5/0
If you want 2 queries for separate results:
SELECT col1, AVG(value) AS average1
from table1
GROUP BY col1
ORDER BY col1
and
SELECT col2, AVG(value) AS average2
from table1
GROUP BY col2
ORDER BY col2

MySQL Counting the number of occurrences of a value from a column in another column and storing in new column

How do I structure my query so I can count how many occurrences of a value in column 1 appears in column 2 and then store that result in a new column in the same table? (If a value is duplicated in the first column I still want to store the same value in the new column) For example if I had a table like this:
COL1 COL2
1 2
1 4
2 1
3 1
4 1
4 2
The resulting table will look like this:
COL1 COL2 COL3
1 2 3
1 4 3
2 1 2
3 1 0
4 1 1
4 2 1
Any help is appreciated I am new to sql! Thanks in advance!
Select
col1,
col2,
COALESCE(col3,0) as col3
FROM
mytable
LEFT JOIN
( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
And if you want the update (thanks Thorsten Kettner ) :
UPDATE mytable
LEFT JOIN ( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
SET mytable.col3 = COALESCE(temp.col3,0)
You can easily count on-the-fly. Don't store this redundantly. This would only cause problems later.
select
col1,
col2,
(
select count(*)
from mytable match
where match.col2 = mytable.col1
) as col3
from mytable;
If you think you must do it; here is the according UPDATE statement:
update mytable
set col3 =
(
select count(*)
from mytable match
where match.col2 = mytable.col1
);
To do that, you can try :
SELECT COL1, COL2, (SELECT COUNT(COL1) FROM `tablename` AS t2
WHERE t2.COL1 = t1.COL1) AS COL3 FROM `tablename` AS t1
Enjoy :)

mySQL - Selecting all records where all columns are unique

Let's say I have a table like this
id col1 col2 col3 col4 col5 col6
1 1 2 3 4 5 6
2 2 1 4 3 6 5
3 1 1 2 3 4 5
I would want to select the rows where every field has a different value. The out put would be
id col1 col2 col3 col4 col5 col6
1 1 2 3 4 5 6
2 2 1 4 3 6 5
I know you can do this
SELECT * FROM table
WHERE col1 <> col2 AND col2 <> col3...
but that would take forever with this many columns. Is there a specific function for determining if all columns are unique?
You can unpivot your table using UNION ALL:
SELECT id
FROM (
SELECT id, col1 AS col
FROM mytable
UNION ALL
SELECT id, col2
FROM mytable
UNION ALL
SELECT id, col3
FROM mytable
UNION ALL
SELECT id, col4
FROM mytable
UNION ALL
SELECT id, col5
FROM mytable
UNION ALL
SELECT id, col6
FROM mytable) AS t
GROUP BY id
HAVING COUNT(DISTINCT col) = 6
If you want all columns selected then you do something like:
SELECT *
FROM mytable
WHERE id IN ( .... above query here ...)
Unfortunately MySQL does not have an UNPIVOT operator that would make the above query less verbose.
A little less verbose than your solution, although it doesn't scale perfectly if the number of columns is excessively large:
SELECT
* -- In actual live code you would of course never use '*'
FROM
MyTable
WHERE
col1 NOT IN (col2, col3, col4, col5, col6) AND
col2 NOT IN (col3, col4, col5, col6) AND
col3 NOT IN (col4, col5, col6) AND
col4 NOT IN (col5, col6) AND
col5 NOT IN (col6) -- Done as an IN just to be consistent
Try this i hope this will helping you
select
(SELECT group_concat(DISTINCT col1) FROM table) as col1,
(SELECT group_concat(DISTINCT col2) FROM table) as col2,
(SELECT group_concat(DISTINCT col3) FROM table) as col3,
(SELECT group_concat(DISTINCT col4) FROM table) as col4

Selecting distinct 2 columns combination in mysql

I have a mysql table that looks like this:
1 value1 value2 3534
2 value1 value1 8456
3 value1 value2 3566
4 value1 value3 7345
5 value2 value3 6734
I need a query to select all the rows with distinct column 2 and 3, for example the output I want for this example will look like this:
1 value1 value2 3534
2 value1 value1 8456
4 value1 value3 7345
5 value2 value3 6734
i've found a few samples on how to do it but they all select distinct on each column individually.
Update 1
Better you use this against above.
SELECT id, col2, col3, col4
FROM yourtable
GROUP BY col2, col3;
Demo
The reason I am saying is because using CONCAT, I am not getting desired result in this case. First query is returning me 5 rows however CONCAT is returning me 4 rows which is INCORRECT.
Hope you got my point.
Assumed the columns in the table are (id, col2, col3, col4).
SELECT DISTINCT(CONCAT(col2, col3)) as "dummy column", id, col2, col3, col4
FROM yourtable
GROUP BY CONCAT(col2, col3);
OR
SELECT id, col2, col3, MIN(col4)
FROM yourtable
GROUP BY col2, col3;
live working example
Assuming that the first column is unique, you can do this:
SELECT id, col2, col3, col4
FROM yourtable
WHERE id IN
(
SELECT MIN(id)
FROM yourtable
GROUP BY col2, col3
)
See it working online: sqlfiddle
Assuming the columns in the table are (id, col1, col2, col3), you could:
SELECT *
FROM YourTable yt
JOIN (
SELECT MIN(id) as minid
FROM YourTable
GROUP BY
col1, col2
) filter
ON filter.minid = yt.id
This query makes sure that the combination of column1 and column2 is unique, while selecting the minimum value of column three
SELECT col1, col2, MIN(col3)
FROM yourTable
GROUP BY col1, col2
THe simplest query for this is
SELECT col1, col2, MIN(col3)
FROM myTable
GROUP BY col1, col2
Using the group by method is returning me extra rows, where as explicitly checking each field although longer returns the same no of records as count(Distinct ..)
SELECT id, col2, col3, col4
FROM yourtable yt
WHERE id =
(
SELECT MIN(id)
FROM yourtable yt1
WHERE yt.col2 = yt1.col2
AND yt.col3 = yt1.col3
)