I'm currently migrating from SQL Server to PostgreSQL and got confuse with update query in postgres.
I have query like this in SQL Server:
UPDATE t1
SET col1 = 'xx'
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id is null
how do you do this in postgres?
thanks in advance
The left join is used to simulate a "NOT EXISTS" condition, so you can rewrite it to:
update table1 t1
set col1 = 'xx'
where not exists (select *
from table2 t2
where t1.id = t2.id);
As a side note: Postgres works differently than SQL Server and in general you should not repeat the target table of an UPDATE statement in the FROM clause in Postgres.
Related
I am trying to convert this update statement to spark sql
update table 1
set col1 = t2.col1,
col2=1
from
table1 t1
inner join
table2 t2
on t1.id = t2.id
Where right(t2.col3,2)<>'00'
and t1.col3>0
I used the following code but in the output its missing data
i converted t1 and t2 to temporaryview from spark dataframe
spark.sql("""select t2.col1,
1 as col2,
t1.col3
from
table1 t1
inner join
table2 t2
on t1.id = t2.id
Where right(t2.col3,2)<>'00'
and t1.col3>0 """).createOrReplaceTempView("outputTable")
thinking of selecting the remaining data in a temp view and union the outputs into a final one but not sure if its the proper way to deal with this issue.
Using spark 3.2.1
I am learning sql (postgres) and id like to find values that do not exist.
I have a table, table 1 with ids and i want to find those ids that are not in table 4.
I have to join between 3 tables as table 1 holds id and table 4 contact_id (not the same number)
The tables 2,3 need to be joined as that connects the ids.
So how do i do that with “not exists”?
Select t1.id, table4.contact_id
From table1 t1
Join table2 using(id)
Join table3 using(id)
Join table4 using(contact_id)
Where not exists (
Select 1
From table4
Where table4.contact_id=t1.id
);
It returns no values, but should
No error msg…
I have thinking error i assume
Your query probably returns no values because you join table4 on contact_id and then you exclude in the WHERE clause the rows which come from this join.
To find values that don't exist, you can usually use LEFT JOIN or RIGHT JOIN or FULL OUTER JOIN and then filter the rows with NULL values in the WHERE clause.
Try this :
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2 using(id)
LEFT JOIN table3 t3 using(id)
LEFT JOIN table4 t4 using(contact_id)
WHERE t4.contact_id IS NULL
I have tried to create TEMP table using "Database SQL Executor" node using expression:
CREATE TEMPORARY TABLE tmp_table AS
SELECT type_id, created_at
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
WHERE t2.id = (SELECT id
FROM t2
WHERE id = t1.id
AND event_date <= created_at
ORDER BY event_date DESC
LIMIT 1);
Unfortunately despite the fact there is neither no mistake in pure syntax (above code works when run from PSQL console) nor error after node execution, table does not exist in database.
EDIT : Thanks to #SABER-FICTIONALCHARACTER
If I have a query
select * into #tmp1 from dbo.t1 inner join dbo.t2 on t1.Sender_Id=t2.Id
I get an error
Column names in each table must be unique. Column name 'Id' in table '#tmp1' is specified more than once.
How can I do the same thing without resorting to
select t1.Id as t1id, t1.col2. ... t1.col30, t2.Id as t2id, ... t2.col40 as t2col40 from ...
notation.
I just need to quickly and manualy examine several tables, so I'd like to have a quick way of examining joins.
If you have to persist the result via select * into #tmp or want to create a view, every field name has to be unique and you will have to uses aliases for fields with identical names.
A simple query does not need unique names.
Yes, columns name must unique in select statement, in your case, you have for example two (02) id, one from tbl1, and the other one from tbl2, on esolution is list them as:
Select t1.id, t2.id
From tbl1 as t1 Inner Join tbl2 as t2 on t1.id = t2.id
Hope this help.
Or, if you have columns with same name in both tables, use this:
Select t1.*, t2.* From tbl1 as t1 Inner join tbl2 as t2 On t1.id = t2.id
Regards
I need a JPQL for the MySQL query:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.table1.id
INNER JOIN (SELECT * FROM table1 t3
INNER JOIN table2 t4 ON t3.id = t4.table1.id
WHERE t3.name = 'xxx') subTable
ON t1.number = subTable.number
WHERE t1.number = '5'
AND id = '3'
Your query seems quite pathological, perhaps say what result you are trying to query, and include your object model.
In general, JPQL does not support sub-selects in the from clause, so your query is not directly convertable to JPQL.
You can always just execute it as a JPA native SQL query, since you seem to be comfortable with SQL than JPQL.