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
)
Related
I am using this query to get duplicate rows in sql
SELECT
col1, col2, COUNT(*)
FROM
emp
GROUP BY
col1, col2
HAVING
COUNT(*) > 1
I have used col1, col2 here to match duplicates but what if I have 1000 more columns will I write all of there names? Like :
SELECT
col1, col2,...,col1000 COUNT(*)
FROM
emp
GROUP BY
col1, col2...,col1000
HAVING
COUNT(*) > 1
Or is there any better way?
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'
I have table with columns say:
col1, col2, col3
I want to find if either values say 22 or 33 or 3 is/are in the columns.
For single value assume 22, I could have done:
SELECT * from table_name
WHERE 22 IN (col1, col2, col3)
How can I find 22, 33 in columns col1, col2, col3.
Any help is highly appreciable.
Thanks!
SELECT * from table_name
WHERE col1 IN (1,2,3) or
col2 IN (1,2,3) or
col3 IN (1,2,3)
Maybe also like this
with search as
(
select 1 v
union all
select 2 v
union all
select 3 v
)
select distinct t.*
from table_name t
join search s on t.col1 = s.v or t.col2 = s.v or t.col3 = s.v
dbfiddle demo
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
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