Re: [GENERAL] Delete from Join

2008-07-04 Thread Gwyneth Morrison




Tom Lane wrote:

  Gwyneth Morrison <[EMAIL PROTECTED]> writes:
  
  
What I am actually trying to get past is:

  
  
  
  
DELETE FROM data_table1
using data_table2 INNER JOIN
 data_table1  ON data_table1.fkey =
 data_table2.pkey;

  
  
The equivalent to that in Postgres would be

DELETE FROM data_table1
  USING data_table2
  WHERE data_table1.fkey = data_table2.pkey;

The fundamental issue here is that MSSQL expects the USING clause to
contain a second reference to the delete target table, whereas PG
does not --- if you write the table name again, that's effectively
a self-join and you probably won't get the behavior you want.

You can use JOIN syntax in USING in Postgres, but only for situations
where the query really involves three or more tables.

			regards, tom lane
  

Thank you Tom,

That was exactly what I needed to know and yes it does work.

I do know about the using/from clause and  and the second table
reference.

Sorry about the above example, I cut it from something much larger to
try and 
get my point  across. Yes it is invalid. I should be more careful.

I do have another question I will post as a separate posting. 

Gwyneth




Re: [GENERAL] Delete from Join

2008-07-03 Thread Tom Lane
Gwyneth Morrison <[EMAIL PROTECTED]> writes:
> What I am actually trying to get past is:

> DELETE FROM data_table1
> using data_table2 INNER JOIN
>  data_table1  ON data_table1.fkey =
>  data_table2.pkey;

The equivalent to that in Postgres would be

DELETE FROM data_table1
  USING data_table2
  WHERE data_table1.fkey = data_table2.pkey;

The fundamental issue here is that MSSQL expects the USING clause to
contain a second reference to the delete target table, whereas PG
does not --- if you write the table name again, that's effectively
a self-join and you probably won't get the behavior you want.

You can use JOIN syntax in USING in Postgres, but only for situations
where the query really involves three or more tables.

regards, tom lane

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Delete from Join

2008-07-03 Thread Lennin Caro



--- On Wed, 7/2/08, Gwyneth Morrison <[EMAIL PROTECTED]> wrote:

> From: Gwyneth Morrison <[EMAIL PROTECTED]>
> Subject: Re: [GENERAL] Delete from Join
> To: pgsql-general@postgresql.org
> Date: Wednesday, July 2, 2008, 7:12 PM
> --- On Wed, 7/2/08, Gwyneth Morrison
> <[EMAIL PROTECTED]> wrote:
> 
> 
> 
>   > From: Gwyneth Morrison <[EMAIL PROTECTED]>
> > Subject: [GENERAL] Delete from Join
> > To: pgsql-general@postgresql.org
> > Date: Wednesday, July 2, 2008, 3:15 PM
> > Hello,
> > 
> > Is it possible to use a join keyword in a delete?
> > 
> > For example:
> > 
> >  DELETE FROM data_table1
> > using data_table2 INNER JOIN
> > data_table1  ON
> data_table1.fkey =
> > data_table2.pkey;
> > 
> > 
> > It is not directly mentioned in the delete syntax but
> the
> > delete refers 
> > to the select clause where JOIN is valid.
> > 
> > G
> >   
>   
> 
> 
> >i have a example
> 
> >delete from t1 a using t2 b where a.id = b.oid
> 
> >A standard way to do it is
> 
> >delete from t1 a where id in (select a.id from t1 a
> inner join t2 b on (a.id = b.oid))
> 
> 
>  Thank you for your reply,
> 
>  You are absolutely correct, it IS the standard way. 
> 
> What I am actually trying to do here is write a program to
> convert MS SQL to Postgres.
> I have had quite a bit of success so far, but this is a
> sticking point. 
> 
> Apparently using the JOIN keyword directly in a delete
> statement is valid in MS.
> I am trying to determine if it is valid in postgres which I
> figure it is not but cannot 
> find it exactly in the documentation. 
> 
> So I guess the real question is, can the JOIN keyword be
> used directly in a delete as above.
> 
> G
> 
> 

i have the same problem. i try use JOIN keyword in DELETE syntax but dont work. 
I assume cant use JOIN keywork


  


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Delete from Join

2008-07-03 Thread Gwyneth Morrison

Scott Marlowe wrote:

Take a look here, in the notes section:

http://www.postgresql.org/docs/8.3/interactive/sql-delete.html

on the using keyword.

  

Thank you for your reply Scott,

I guess this is where the confusion started for me.

It says here in your reference  that the using clause is not standard but
that is fine as I am doing a subst. MS actually allows:

DELETE from table from table.

I change the second from to a using and it works fine most of the time.

What I am actually trying to get past is:


   DELETE FROM data_table1
   using data_table2 INNER JOIN
data_table1  ON data_table1.fkey =
data_table2.pkey;



Where the INNER JOIN keyword is used in the delete.

In the documentation section you cited, they refer to the from clause
in the usinglist. The from clause link there refers to select which  states
that a join keyword is valid in a from clause.

What I have found is it sometimes compiles but doesn't work.

I am just seeking verification  if it is supposed to work.

Gwyneth

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Delete from Join

2008-07-02 Thread Scott Marlowe
Take a look here, in the notes section:

http://www.postgresql.org/docs/8.3/interactive/sql-delete.html

on the using keyword.

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Delete from Join

2008-07-02 Thread Gwyneth Morrison






--- On Wed, 7/2/08, Gwyneth Morrison <[EMAIL PROTECTED]> wrote:



  > From: Gwyneth Morrison <[EMAIL PROTECTED]>
> Subject: [GENERAL] Delete from Join
> To: pgsql-general@postgresql.org
> Date: Wednesday, July 2, 2008, 3:15 PM
> Hello,
> 
> Is it possible to use a join keyword in a delete?
> 
> For example:
> 
>  DELETE FROM data_table1
> using data_table2 INNER JOIN
> data_table1  ON data_table1.fkey =
> data_table2.pkey;
> 
> 
> It is not directly mentioned in the delete syntax but the
> delete refers 
> to the select clause where JOIN is valid.
> 
> G
>   
  


>i have a example

>delete from t1 a using t2 b where a.id = b.oid

>A standard way to do it is

>delete from t1 a where id in (select a.id from t1 a inner join t2 b on (a.id = b.oid))


 Thank you for your reply,

 You are absolutely correct, it IS the standard way. 

What I am actually trying to do here is write a program to convert MS SQL to Postgres.
I have had quite a bit of success so far, but this is a sticking point. 

Apparently using the JOIN keyword directly in a delete statement is valid in MS.
I am trying to determine if it is valid in postgres which I figure it is not but cannot 
find it exactly in the documentation. 

So I guess the real question is, can the JOIN keyword be used directly in a delete as above.

G


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general






Re: [GENERAL] Delete from Join

2008-07-02 Thread Lennin Caro



--- On Wed, 7/2/08, Gwyneth Morrison <[EMAIL PROTECTED]> wrote:

> From: Gwyneth Morrison <[EMAIL PROTECTED]>
> Subject: [GENERAL] Delete from Join
> To: pgsql-general@postgresql.org
> Date: Wednesday, July 2, 2008, 3:15 PM
> Hello,
> 
> Is it possible to use a join keyword in a delete?
> 
> For example:
> 
>  DELETE FROM data_table1
> using data_table2 INNER JOIN
> data_table1  ON data_table1.fkey =
> data_table2.pkey;
> 
> 
> It is not directly mentioned in the delete syntax but the
> delete refers 
> to the select clause where JOIN is valid.
> 
> G
>   

i have a example

delete from t1 a using t2 b where a.id = b.oid

A standard way to do it is

delete from t1 a where id in (select a.id from t1 a inner join t2 b on (a.id = 
b.oid))


  


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[GENERAL] Delete from Join

2008-07-02 Thread Gwyneth Morrison

Hello,

Is it possible to use a join keyword in a delete?

For example:

DELETE FROM data_table1
   using data_table2 INNER JOIN
   data_table1  ON data_table1.fkey = data_table2.pkey;


It is not directly mentioned in the delete syntax but the delete refers 
to the select clause where JOIN is valid.


G
 


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general