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
Related
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 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
I have the following query:
SELECT col1, col2, col3, col4
FROM table
WHERE ...
So the result is like:
col1 col2 col3 col4
1 2 3 4
1.1 2.1 3.1 4.1
But i want a result like:
col1,col2,col3,col4
1
1.1
2
2.1
...
any idea?
edit:
I have to search for a String in 2 tables containing 9 and 1 column(s).
Then i need to sort it all IN 1 COLUMN and only get the 5 first items.
You can do this using union clause. Somthing like this:-
SELECT col1
FROM table
WHERE ...
UNION
SELECT col2
FROM table
WHERE ...
UNION
SELECT col3
FROM table
WHERE ...
UNION
SELECT col4
FROM table
WHERE ...
Hope this helps you.
If is here no more requirements you could do like this:
select first_col
from
(
select 'col1, col2, col3, col4' as first_col, 1 as rnk
union all
select col1,2
union all
select col2,3
union all
select col3,4
union all
select col4,5
) t
order by 2
Here is an order, but probably you don't need it
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
)
I have a table with the following structure
id(int) | col1(int) | col2(int) | col3(int)
------------------------------------------
1 | NULL | 10 | 20
2 | 5 | NULL | 30
3 | 8 | NULL | NULL
Given an 'id' value I need to find the minimum non-null value across all the columns in that record.
Like for example, for id=1, the value is 10. For id =2 the value is 5 and so on.
How do I do this in MySQL?
LEAST() would be best if it was NULL safe, but since it isn't, this is the cleanest solution i can think of:
SELECT MIN(cols)
FROM (SELECT col1
FROM table
WHERE id = 1
UNION
SELECT col2
FROM table
WHERE id = 1
UNION col3
FROM table
WHERE id = 1) AS dt
if you need it in one line, this is the best i can think of:
SELECT LEAST(COALESCE(col1, col2, col3)
, COALESCE(col2, col3, col1)
, COALESCE(col3, col1, col2))
FROM table
WHERE id = 1
Give this a shot:
SELECT id, LEAST(
ifnull(ifnull(col1, col2), col3),
ifnull(ifnull(col2, col1), col3),
ifnull(ifnull(col3, col1), col2)) FROM table;
SELECT LEAST(
COALESCE(col1, col2, col3),
COALESCE(col2, col1, col3),
COALESCE(col3, col1, col2)
)
FROM mytable
Alternatively, use session variables:
SELECT LEAST(
#r := COALESCE(col1, col2, col3),
#r := COALESCE(#r, col2),
#r := COALESCE(#r, col3)
)
FROM mytable
Been a while since I used them, but I think you can use MySQL's MIN and LEAST functions like so:
SELECT MIN(LEAST(col1,col2,col3)) FROM table WHERE id=1;