Using UNION ALL with the same conditions - mysql

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'

Related

Checking multiple columns for multiple value mySql

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

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

MYSQL: display multiple columns in 1

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

Similar WHERE clause in a long UNION statement in SQL Server 2008 R2

In a stored procedure, I need to INSERT the result of a long UNION into a temp table.
The WHERE clause is the same for all tables, which is being in a SELECT DISTINCT.
Simplified for readability, it goes like this:
INSERT INTO #MyTemp
SELECT col1, col2, col3 FROM tab1 WHERE col1 in (SELECT DISTINCT myId FROM TabIds) UNION
SELECT col1, col2, col3 FROM tab2 WHERE col1 in (SELECT DISTINCT myId FROM TabIds) UNION
SELECT col1, col2, col3 FROM tab3 WHERE col1 in (SELECT DISTINCT myId FROM TabIds) UNION
.
.
.
SELECT col1, col2, col3 FROM tab20 WHERE col1 in (SELECT DISTINCT myId FROM TabIds)
Although TabIds is a small temp table, typically 3-6 records long, this seems to be pretty inneficient.
Is there a better way to do this?
Summarizing my question:
Is there a way I can do SELECT DISTINCT myId FROM TabIds just once and assign it to a kind of array/list/set (not to another temp table) and just use that in the WHERE clauses, and if there is a way, does it really matter for such a small (3-6 recs) temp table?
I'm ignoring your requirement ("not to another temp table") because I don't believe it is well-founded. Try and see if this solution gives you better performance:
SELECT i = myId
INTO #x
FROM dbo.TabIds -- please always use schema prefix
GROUP BY myId;
CREATE UNIQUE CLUSTERED INDEX x ON #x(i);
INSERT INTO #MyTemp(col1, col2, col3)
SELECT col1, col2, col3
FROM
(
SELECT col1, col2, col3 FROM dbo.tab1 WHERE EXISTS -- likely better than IN
(SELECT 1 FROM #x WHERE i = tab1.col1)
UNION ALL
SELECT col1, col2, col3 FROM dbo.tab2 WHERE EXISTS
(SELECT 1 FROM #x WHERE i = tab2.col1)
UNION ALL
...
UNION ALL
SELECT col1, col2, col3 FROM dbo.tab20 WHERE EXISTS
(SELECT 1 FROM #x WHERE i = tab20.col1)
) AS x
GROUP BY col1, col2, col3; -- likely more efficient than `UNION` to remove dupes
Of course this will work best if col1 is indexed in all 20 tables, and if that index includes col2 and col3.
The reason I suggested a view is not because I thought it would make this code run faster. Just that you could create a view that generates this UNION for you, making this code simpler (and any other code that repeats this monotonous UNION). It was a suggestion for convenience, not for performance - though I need to make it clear that using a view does not magically make things slower. Sometimes I can, but that's a dangerous and illogical reason to avoid views.
Finally, I'd strongly consider normalization. Why are these 20 different tables in the first place, when they could all be in one single table?
CREATE TABLE dbo.Normal
(
SourceTableID INT,
col1 <data type>,
col2 <data type>,
col3 <data type>
);
-- indexes / constraints
INSERT dbo.Normal
SELECT 1, col1, col2, col3 FROM dbo.tab1
UNION ALL
SELECT 2, col1, col2, col3 FROM dbo.tab2
UNION ALL
...
UNION ALL
SELECT 20, col1, col2, col3 FROM dbo.tab20;
Now all your queries can simply reference this new table. If you will commonly look for only one of the sources (e.g. tab5), then indexing or partitioning on SourceTableID would be useful.
What you're doing, conceptually, is fine for one-offs and data loads. I hope this isn't part of a bigger pattern in production code, though.
What you're looking for is a Common Table Expression.
My T-SQL is a bit rusty, but with a CTE, your query would go something like:
WITH TabIds_CTE AS (SELECT DISTINCT myId FROM TabIds)
INSERT INTO #MyTemp
SELECT col1, col2, col3 FROM tab1 WHERE col1 IN (SELECT * FROM TabIds_CTE)
UNION ALL ...
I think the following might be better for small tables, but still - it's horrible idea to leave it like this in some production process :)
INSERT INTO #MyTemp (col1,col2,col3)
select distinct
x.col1,x.col2,x.col3
from (
SELECT col1, col2, col3 FROM tab1 union all
SELECT col1, col2, col3 FROM tab2 union all
SELECT col1, col2, col3 FROM tab3 union all
-- ...
SELECT col1, col2, col3 FROM tab20
) x
join (
SELECT DISTINCT myId FROM TabIds
) y
on x.col1=y.myid

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
)