I have this sql statement:
SELECT
t0.col0,
t0.col1,
t2.col0,
t2.col1,
t1.col0,
t1.col1,
t3.col3,
t3.col5,
t1.col2
FROM table0 t0
INNER JOIN table1 t1 ON t0.col0 = t1.col1
INNER JOIN table2 t2 ON t2.col0 = t1.col0
INNER JOIN table3 t3 ON t1.col0 = t3.col1
WHERE t1.col1 in (300, 301, 302, 302)
AND t2.col5 like 'V-%'
AND t3.delete = 'false'
this is working perfectly and shows a virtual table with the joined columns. So i try to directly update this Table like this way:
UPDATE T SET T.col1 = 1, T.col2 = '01.01.2012'
FROM (
SELECT
t0.col0,
t0.col1,
t2.col0,
t2.col1,
t1.col0,
t1.col1,
t3.col3,
t3.col5,
t1.col2
FROM table0 t0
INNER JOIN table1 t1 ON t0.col0 = t1.col1
INNER JOIN table2 t2 ON t2.col0 = t1.col0
INNER JOIN table3 t3 ON t1.col0 = t3.col1
WHERE t1.col1 in (300, 301, 302, 302)
AND t2.col5 like 'V-%'
AND t3.delete = 'false'
) as T
without success... The only way to made it was to create a View and proceed update on it.
But can i update in one statement a virtual Table?
You are not trying to update a table, but the output of a select statement. It's not a virtual table, because the output goes nowhere.
I think you need something like this.
UPDATE T SET t0.col0 = 1, t0.col1 = '01.01.2012'
FROM table0 t0
INNER JOIN table1 t1 ON t0.col0 = t1.col1
INNER JOIN table2 t2 ON t2.col0 = t1.col0
INNER JOIN table3 t3 ON t1.col0 = t3.col1
WHERE t1.col1 in (300, 301, 302, 302)
AND t2.col5 like 'V-%'
AND t3.delete = 'false'
Related
this query returns multipart identifier could not be bound for
table2.AId and table1.LId = table2.LId
the query is
insert into table1(col1)
select col2 from table2
where table1.AId = table2.AId and table1.LId = table2.LId;
your help is highly appreciated
You need to include all tables in the SELECT in the FROM clause. So, this is syntactically correct:
insert into table1(col1)
select t2.col2
from table2 t2 join
table1 t1
on t1.AId = t2.AId and t1.LId = t2.LId;
However, you probably intend UPDATE:
update t1
set col1 = t2.col2
from table1 t1 join
table2 t2
on t1.AId = t2.AId and t1.LId = t2.LId;
This modifies the rows in t1. The version with insert will insert new rows with new values only in one column.
Your select query does not "know" the table in the insert into part, so you should use a join:
insert into table1(col1)
select col2
from table2
inner join table1 on table1.AId = table2.AId and table1.LId = table2.LId;
However, I suspect you are looking for update rather then insert:
update t1
set col1 = t2.col2
from table1 t1
inner join table2 t2 on t1.AId = t2.AId and t1.LId = t2.LId;
Missing join condition
insert into table1(col1)
select t2.col2
from table2 t2 join
table1 t1
on t1.AId = t2.AId and t1.LId = t2.LId;
I need to join two pairs of tables. If there is an ID in Table1 that can also be found in Table3 I need to join the tables. If there is no matching ID from Table1 in Table3, I need to not join the tables.
Ex.
If there is at least one id in Table1 in Table3; do something that is effectively this:
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
LEFT JOIN Table3 AS t3 ON t1.ID = t3.ID
LEFT JOIN Table4 AS t4 ON t3.ID = t4.ID
If there are no IDs that match between Table1 and Table3; do something that is effectively this:
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
Just translating your question into SQL, you can do this:
IF EXISTS(SELECT * FROM Table1 T1 INNER JOIN Table3 T3 ON T1.ID=T3.ID)
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
LEFT JOIN Table3 AS t3 ON t1.ID = t3.ID
LEFT JOIN Table4 AS t4 ON t3.ID = t4.ID
ELSE
SELECT *
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.ID = t2.ID
I use next SQL-query in Oracle DB:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
This query is very simple and fast (about 1 min, T1 contain about 1 million rows, T3 more then 10 million rows). Now I want to use MyTable4 from dblink1 for filtering selected rows data. For it, I use subquery:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN (SELECT Sub_T1.*
FROM MyTable3#dblink1 Sub_T1
INNER JOIN MyTable4#dblink1 Sub_T2 ON Sub_T2.Id3 = Sub_T1.Id
WHERE
Sub_T2.MyColumn1 = 'required value') T3 ON T3.Id2 = T2.Id
But this query is too slow (more then 20min). If I rewrite this query to:
SELECT T1.*,
T3.*
FROM MyTable1 T1
INNER JOIN MyTable2 T2 ON T2.Id1 = T1.Id
LEFT JOIN MyTable3#dblink1 T3 ON T3.Id2 = T2.Id
LEFT JOIN MyTable4#dblink1 T4 ON T4.Id3 = T3.Id
WHERE
T4.MyColumn1 = 'required value'
Then my query work fast again, but I donn't like result (I want to see columns of T3 as null, if WHERE return false).
How to improve my second query, for speed up it?
Does phrasing the query with parentheses solve the problem?
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
(MyTable3#dblink1 T3 JOIN
MyTable4#dblink1 T4
ON T4.Id3 = T3.Id AND
T4.MyColumn1 = 'required value'
)
ON T3.Id2 = T2.Id;
Or, also:
SELECT T1.*,
T3.*
FROM MyTable1 T1 INNER JOIN
MyTable2 T2
ON T2.Id1 = T1.Id LEFT JOIN
MyTable3#dblink1 T3
ON T3.Id2 = T2.Id
EXISTS (SELECT 1 FROM MyTable4#dblink1 T4 WHERE T4.Id3 = T3.Id AND T4.MyColumn1 = 'required value'
)
I am new to Hibernate. And here I successfully used SQL query as is using session.createSQLQuery():
SELECT t2.col1, t2.col2, t2.col3, t5.col4, t1.col5, t4.col6, t4.col7,
DECODE(t1.col8,null,t1.col9,t1.col8), t1.col10, t1.col11, t8.col12
FROM table1 t1
JOIN table2 t2
ON t1.xyz = t2.xyz
JOIN table3 t3
ON t2.col3 = t3.col3
LEFT JOIN view1 t4
ON t1.abc = t4.abc1
LEFT JOIN view2 t5
ON t1.abc10 = t5.abc2 AND t5.xyz1 = 1
JOIN table6 t6
ON t2.abc8 = t6.abc9
JOIN table7 t7
ON t6.xyz2 = t7.xyz2
LEFT JOIN table8 t8
ON t2.col1 = t8.abc3 AND t8.abc5 = 'XYZ' AND t8.abc6 = 1234
WHERE t2.DISPLAY = 'true'
AND t2.abc4 = 0
AND t6.abc7 = 0
AND t2.col2 = 0
So I don't have all those entities Java objects. And no mapping xml file. But this query does not work when I use "SELECT COUNT(*) FROM " It is giving me an error "unexpected token ON". So how to fix select COUNT(*)? Thank you.
You need a group by to do COUNT(*).
group by the values you select and you should have your count ;)
I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server???
the following is what i am trying to do :
Update Table1
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)
any idea??
If you need to use a subquery to perform the UPDATE you can do it this way:
UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
SELECT id, value
FROM table2
) t2
ON t1.id = t2.id
One way is to alias the table:
update t1
set table1.value1 = t2.value1
from table1 as t1
join table2 as t2
on t1.id = t2.t1_id
You should try
UPDATE table1 SET t1.value1 = t2.value2
FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field2
UPDATE Table1 t1
INNER JOIN (
SELECT id, value
FROM table2
) t2 USING(id)
SET t1.value = t2.value