This question already has answers here:
INNER JOIN ON vs WHERE clause
(12 answers)
Closed 7 years ago.
CREATE TABLE a (aid int);
CREATE TABLE b (bid int);
INSERT INTO a VALUES (1), (2), (3);
INSERT INTO b VALUES (2), (3), (4);
What would be the difference between
SELECT * FROM a, b WHERE aid = bid; and
SELECT * FROM a JOIN b ON a.aid=b.bid;
The result of explain analyze for both queries looks totally same.
Both queries do exactly the same.
Related
I'm having an issue with a simple update statement. I'm new to postgresql and I'm still stuck on MS Sql Server syntax.
What I want to do is to update all records from table1 which are not present / don't exist in table2. Table1 and Table2 are having an 1 to 1 relation. The join column is "colx" from my example
On Ms SQL Server I would have something like this:
UPDATE table1 set col1='some value' from table1 t1 LEFT JOIN table2 t2 on t1.colx=t2.colx WHERE t2.colx IS NULL
or
UPDATE table1 set col1='some value' from table1 t1 where not exists (select 1 from table2 t2 where t1.colx=t2.colx)
My issue is when performing the same on PostgreSql it updates all records from table1, not only the records matching the condition (e.g. I was expecting 4 records to be updated, but all records from table1 are updated instead).
I checked using a select statement the join condition for all possible approaches and I have the expected result (e.g. 4 records).
Is there anything I'm missing?
Your question is not very clear about the requirement.
What I understood is you want to update the value of col1 in table1 for those records which are not present in the table2.
You can try it this way in Postgresql:
UPDATE table1 t1 set col1='some value' where not exists(select 1 from table2 where colx=t1.colx)
DEMO
This question already has answers here:
What is easier to read in EXISTS subqueries? [closed]
(3 answers)
Closed 6 years ago.
Is it better (in terms of performance, speed, etc.) to write
SELECT * FROM a WHERE (EXISTS (SELECT * FROM b))
or
SELECT * FROM a WHERE (EXISTS (SELECT 1 FROM b))
in PostgreSQL?
p.s.
This question answers my question for MS SQL Server, but what about PostgreSQL?
Per the documentation:
Since the result depends only on whether any rows are returned, and
not on the contents of those rows, the output list of the subquery is
normally unimportant.
This question already has answers here:
incomplete information from query on pg_views [duplicate]
(2 answers)
Closed 7 years ago.
I would like to create a list of insert into strings from a table I have and use that to import the rows into another table on another server in anothr database. The method works fine for small tables. Now I have a table where I select to many columns with to much characters. My output ends with (...)" and does not fill all my columns. Is there a way of bypassing it?
select
'INSERT INTO table (col1, col2, col3, col4, col5) VALUES ('|| val1 ||', '|| val2 ||', '|| val3 ||','|| val4 ||', '|| val15 ||');'
AS sqlstring
from
table
where
col1 > 1018
;
I suppose you are using pgAdmin3 to run your query and copy from result grid, right?
Go to "File -> Options -> Query tool -> Max. characters per column " and set it to 1000000000.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can some one guide in implementing the relational algebra division operator using sorting with syntactic support in postgresql?
Edit: This is to deal with the source code of postgresql. I need to add the division functionality in Postgresql.
Not sure what you're meaning with sorting syntactic support, but the relational division operator seems like a mere convoluted join:
select a_.id
from (select a.id, array_agg(a.b_id) as b_ids from a group by a.id) as a_
join (select array_agg(b.id) as b_ids from b) as b_ on b_.b_ids <# a_.b_ids
http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29
denis=# create table a (id int, b_id int);
CREATE TABLE
denis=# create table b (id int);
CREATE TABLE
denis=# insert into a values (1,1), (1,2), (1,3), (2,1), (2,3), (3,1), (3,2);
INSERT 0 7
denis=# insert into b values (1), (2);
denis=# select a_.id from (select a.id, array_agg(a.b_id) as b_ids from a group by a.id) as a_ join (select array_agg(b.id) as b_ids from b) as b_ on b_.b_ids <# a_.b_ids;
id
----
1
3
(2 rows)
I am new to postgresql (and databases in general) and was hoping to get some pointers on improving the efficiency of the following statement.
I am inserting data from one table to another, and do not want to insert duplicate values. I have a rid (unique identifier in each table) that are indexed and are Primary Keys.
I am currently using the following statement:
INSERT INTO table1 SELECT * FROM table2 WHERE rid NOT IN (SELECT rid FROM table1).
As of now the table one is 200,000 records, table2 is 20,000 records. Table1 is going to keep growing (probably to around 2,000,000) and table2 will stay around 20,000 records. As of now the statement takes about 15 minutes to run. I am concerned that as Table1 grows this is going to take way to long. Any suggestions?
This should be more efficient than your current query:
INSERT INTO table1
SELECT *
FROM table2
WHERE NOT EXISTS (
SELECT 1 FROM table1 WHERE table1.rid = table2.rid
);
insert into table1
select t2.*
from
table2 t2
left join
table1 t1 on t1.rid = t2.rid
where t1.rid is null