1.select col1,col2 from table1 where condition1=<<value1>>
UNION ALL
select col1,col2 from table2 condition1=<<value2>>;
2. `select col1 from table3;`
3. I need to write the 3rd query where i need the output of col1,col2 from table1 based on the col1 (sort based on col1 from table3).
I can do this way
create table as temp_table as
select col1,col2 from table1 where condition1=<<value1>>
UNION ALL
select col1,col2 from table2 condition1=<<value2>>;
SELECT t1.col1,t1.col2
FROM temp_table t1
LEFT OUTER JOIN table3 t2 ON t1.col2=t2.col2
order by t2.col1;
I want in a single query(in mysql ) & not using temp table.
Any thoughts? Thanks.
You can use the unioned result as subselect.
Look into subqueries. They're kind of like a temporary table that only exists while the query is running.
Select T.A, T.B from
(select col1 as A,col2 as B from table1 where condition1=<<value1>>
UNION ALL
select col1,col2 from table2 condition1=<<value2>>) as T,
table3 as T2
where T.A = T2.col1
order by t2.col1
Related
I have the following sql situation
Table1
id name
Table2
id name
_Table1ToTable2
id, id_table1, id_table2
Result:
id, name, table2_name, table2_id
I would like to join Table1 and Table2 into a single query, but I cannot find a way to make a join between them, any ideas?
It seems like you want to use bridge table Table1ToTable2 to join Table1 and Table2. This can be done simply with 2 INNER JOINs :
SELECT
tt.id AS tt_id,
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
Table1ToTable2 AS tt
INNER JOIN Table1 AS t1
ON t1.id = tt.id_table1
INNER JOIN Table2 AS t2
ON t2.id = tt.id_table2
if there is no relationship between table1 and table 2 then,
select table1.id, table1.name, table2.id, table2.name from
table1, table2
if table1 and table2 are related with ID then,
select table1.id, table1.name, table2.id, table2.name from
table1
inner join table2
on table1.id = table2.id
If your intention is to retrieve a result set with every possible combination of the id columns from both tables then you can do as follows:
select id = identity (int, 1, 1), * into #a from (select distinct table1. id as id1, table2. id as id2 from table1 cross apply table2) d; select * from #a; drop table #a
If you do not wish to use a temp table an alternative would be to use some variation of the row_number() function.
The above solves for an assumed requirement as your question lacks context. Elaborate on your requirements and we will be able to provide you with a more relevant and detailed answer.
I must write a Query like this in MySQL:
SELECT *
FROM Tab1
EXCEPT
SELECT *
FROM Tab1
WHERE int_attribute_of_Tab1>0
but MySQL doesn't support the keyword EXCEPT.
Is there a standard mode to use correctly another operator that simulate the except in MySQL?
You could use NOT IN
SELECT *
FROM Tab1
WHERE id NOT IN (
SELECT id
FROM Tab1
WHERE int_attribute_of_Tab1>0
)
Try this
SELECT *
FROM Tab1
WHERE [....] NOT EXISTS
(SELECT *
FROM Tab1
WHERE int_attribute_of_Tab1>0)
A couple of definitions
SqlServer https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql EXCEPT
Returns any distinct values from the query to the left of the EXCEPT operator that are not also returned from the right query.
PLsql https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm MINUS
statement combines results with the MINUS operator, which returns only unique rows returned by the first query but not by the second
A pedantic translation to mysql would be
SELECT distinct t1.*
FROM Tab1 as t1
left outer join
(SELECT *
FROM Tab1
WHERE int_attribute_of_Tab1>0) as t2 on t1.id = t2.id
where t2.id is null;
Assuming there is an id column, And I wouldn't like to use distinct on a lot of columns.
You can use multiple NOT IN operators combined with AND operators for multiple columns.
For example:
SELECT col1, col2 FROM table1 WHERE col1 NOT IN(SELECT col1 FROM table2) AND col2 NOT IN(SELECT col2 FROM table2)...;
Since MySQL version 8.0.31 update, the EXCEPT operator has become available to use in this DBMS. If you are allowed to update your MySQL version, you are free to use the notation:
SELECT * FROM Tab1
EXCEPT
SELECT * FROM Tab1
WHERE int_attribute_of_Tab1>0
If Tab1 has a primary key (f.e. ID) then you could use a NOT EXISTS to itself like this :
SELECT *
FROM Tab1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM Tab1 AS t2
WHERE t2.ID = t1.ID
AND t2.int_attribute_of_Tab1 > 0
)
But it's kinda pointless in this case.
And it's not what an EXCEPT/MINUS tries to do.
(excluding identical rows)
The question's query with the EXCEPT uses the same table twice.
So reversing that WHERE criteria on Tab1 would give the same results.
SELECT *
FROM Tab1
WHERE (int_attribute_of_Tab1 <= 0 OR int_attribute_of_Tab1 IS NULL)
If it were 2 different tables then this
SELECT t1col1, t1col2, t1col3
FROM Table1
EXCEPT
SELECT t2col4, t2col5, t2col6
FROM Table2
WHERE int_attribute_of_Tab1 > 0
Could be replaced by comparing each selected column
SELECT DISTINCT t1col1, t1col2, t1col3
FROM Table1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM Table1 AS t2
WHERE t2.t2col4 = t1.t1col1
AND t2.t2col5 = t1.t1col2
AND t2.t2col6 = t1.t1col3
AND t2.int_attribute_of_Tab1 > 0
)
i have have two identical tables
lets call them t1 and t2
and i want to show data from both of them BUT they may have identical rows (lets say these rows have the same id )
in that case i only want to get the row from table 1 and ignore the one from second table .
so
SELECT column_name(s) FROM t1 UNION SELECT column_name(s) FROM t2
but how should i handle duplicates ?
you mean this ?
select column1 , column2 from (
SELECT column1 , column2 FROM t1
UNION
SELECT column1 , column2 FROM t2
)t
group by column1
you will have distinct column1 here
If you want to remove duplicates from a SELECT result set, you could use the DISTINCT clause with a sub-query
SELECT DISTINCT * FROM (SELECT value FROM t1 UNION SELECT value FROM t2) AS S
Or better, you might use the UNION DISTINCT syntax:
SELECT value FROM t1 UNION DISTINCT SELECT value FROM t2;
BTW, for UNION the default is UNION DISTINCT (whereas for SELECT, SELECT ALL is the default), so this could be rewritten as:
-- without specifier UNION is implicitly DISTINCT
SELECT value FROM t1 UNION SELECT value FROM t2;
... which is in fact the query you proposed. What was wrong with that? It works with my test set: http://sqlfiddle.com/#!2/d4812/1
Maybe a sqlfeedle with your actual table content might help to provide a better answer.
Here is one way:
SELECT column_name(s)
FROM t1
UNION
SELECT column_name(s)
FROM t2
where not exists (select 1
from t1
where t1.col1 = t2.col1 and t1.col2 = t2.col2 and . . .
)
IN mysql you have something like this:
Select * from (select * from t1, t2 where t1.c1=t2.c1 ) tbl1 ,tbl2
where tbl1.col1=tbl2.col2;
is there anything in like this in oracle, because when I am trying to do same thing in oracle I am getting this error :
invalid identifier "tbl1"."col1".
Try this way:
Select tbl1.col1
from (select c1 as col1 from t1, t2 where t1.c1=t2.c1 ) tbl1 ,tbl2
where tbl1.col1=tbl2.col2;
Here you can find more information.
Try this:
Select tbl1.*, tbl2.*
from (
select t1.c1 as col1, t2.* from t1, t2 where t1.c1=t2.c1
) tbl1 ,tbl2
where tbl1.col1=tbl2.col2;
Because you join 2 tables * doesn't work. Use tbl1.* and tbl2.* instead.
As already suggested by Parado you also have to rename t1.c1 to col1 in your inner select.
I have a query say,
select col1,col2 from table1;
which return 2 columns of multiple rows. I want to use these two values in where condition of another query. Something like
select col3,col4 from table2 where col5=col1 and col6=col2;
where col1 and col2 are the resultant values of the first query.
Currently I have used inner query something like
select col3,col4 from table2
where col5 in (select col1 from table1)
and col6 in (select col2 from table1);
But I dont want to use inner query like the one shown above as it slows down bring results.
Please suggest.
JOIN them instead of using IN's like so:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN
(
SELECT col1, col2
FROM table1
) t1 ON t2.col5 = t1.col1 AND t2.col6 = t1.col2
Note that, you didn't need to select specific columns in the second table. You can JOIN the second table table1 directly like so:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN table1 t1 ON t2.col5 = t1.col1
AND t2.col6 = t1.col2