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.
Related
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 need to query a date with a value between two other dates that come from another table. Something like this:
select * from table1 where date_table1 BETWEEN (select date1,date2 from table2 where id=1)
How can I do that in SQl?
Just use join or exists. As you have written it:
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.date between t2.date1 and t2.date2
);
I am not sure what you are trying to do but the way you phrased it the question seems incomplete. Based on what you have provided, I think something like this will work:
select t1.*
from table1 t1
inner join table2 t2 on t2.date1 <= t1.date_table1 and t1.date_table1 < t2.date2
where t2.somefield = [somelimitingcondition]
This should work provided each sub-query returns exactly one value
SELECT * from table1 t1
WHERE t1.date_table1
BETWEEN
(SELECT t2.date1 from table2 t2 WHERE t1.id=1)
AND (SELECT tt2.date2 from table2 tt2 WHERE t1.id=1);
I wrote a query in this format..
Select * INTO #xyz FROM ()
which I later want to use to create a view, as...
CREATE VIEW aaa
AS
Select * INTO #xyz FROM ()
but getting the following errors:
Incorrect syntax near the keyword 'INTO'.
Views or functions are not allowed on temporary tables
Can someone please suggest a workaround this? Is it possible to use temporary tables to create views?
You certainly cannot use a select into in a view. And a termp table is not approraite either. Use a derived table that is the equivalent of the temp table instead. Example:
Suppose you had:
select t1.id, t2.field1 into #temp from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest'
select t1.id, t2.field1, t1.field1
from mytable2 t1
join #Temp t2 on t1.id = t2.id
where t2.somefield = 'test'
Then you could use:
select t1.id, t2.field1, t1.field1
from mytable2 t1
join (select t1.id, t2.field1
from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest') t2
on t1.id = t2.id
where t2.somefield = 'test'
You could also usea a CTE
Just as the error message says it is not possible to use temp tables.
You should use a permanent table or a CTE which can also be specified in a view.
A CTE could help you out depending on your situation. Describe your problem with some context if you think it suitable after researching what a CTE is. In short a CTE is a query that you can reference multiple times which in the past people used temp tables for.
http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx
I have two table created as
create table table1(id int,);
create table table2(id int, tb2_id int,...)
But when i try out
Select * from table2 where tb2_id=table1.id;
I have got an error that table1.id is an unknown column.
Could someone point out where the mistake I made is ?
You probably want to JOIN tables:
SELECT table2.* FROM table2 JOIN table1 ON (table2.tb2_id=table1.id)
Select * from table2, table1 where tb2_id=table1.id;
You need either a join or a subquery.
Select t2.*
from table2 t2
Inner join table1 t1
On t2.tbl2_id = t1.id
Or
Select t2.*
from table2 t2
where tbl2_id in ( select id from table1 )
try this:
SELECT *
FROM Table2
WHERE ID IN (SELECT ID FROM Table1)
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