|
|
unambiguous delete from self-join?
Hmm. I've done self-joins with SELECT and with UPDATE, but I haven't
managed to handle it in DELETE & wonder if it is possible with MySQL.
Here's my situation:
I have a very simple table called "item_attr". It has two columns,
both keys. One column is an "item_id" and the other column is an
"attr_id".
Items with attr_id = 0 represent selected items; items with attr_id =
2 represent items in a particular category.
What I want to do is to "remove" all the items in a particular
category (attr_id = 2) that are also selected; more specifically: I
want to eliminate all rows in this table which have attr_id = 2 and
which have an item_id which also appears in this same table with
attr_id = 0.
I can select those items:
select iattr.item_id from item_attr iattr, item_attr isel where
isel.attr_id = 0 and isel.item_id = iattr.item_id and iattr.attr_id =
2
I can change the attr_id for those items:
update item_attr iattr, item_attr isel set iattr.attr_id = 0xffffffff
where isel.attr_id = 0 and isel.item_id = iattr.item_id and
iattr.attr_id = 2
But I can't seem to figure out how to delete them in one swoop. I can
use the previous query to create a "bogus" attr_id value 0xffffffff,
and then delete rows in the table with attr_id = 0xffffffff... but
technically that's not quite what I want, since it fails if there are
rows in the table with attr_id = 0xffffffff that mean something else.
Any thoughts?
|
|
0
|
|
|
|
Reply
|
jmsachs (76)
|
4/28/2007 5:59:38 AM |
|
Jason S wrote:
> Hmm. I've done self-joins with SELECT and with UPDATE, but I haven't
> managed to handle it in DELETE & wonder if it is possible with MySQL.
>
> Here's my situation:
> I have a very simple table called "item_attr". It has two columns,
> both keys. One column is an "item_id" and the other column is an
> "attr_id".
>
> Items with attr_id = 0 represent selected items; items with attr_id =
> 2 represent items in a particular category.
>
> What I want to do is to "remove" all the items in a particular
> category (attr_id = 2) that are also selected; more specifically: I
> want to eliminate all rows in this table which have attr_id = 2 and
> which have an item_id which also appears in this same table with
> attr_id = 0.
>
> I can select those items:
>
> select iattr.item_id from item_attr iattr, item_attr isel where
> isel.attr_id = 0 and isel.item_id = iattr.item_id and iattr.attr_id =
> 2
>
> I can change the attr_id for those items:
> update item_attr iattr, item_attr isel set iattr.attr_id = 0xffffffff
> where isel.attr_id = 0 and isel.item_id = iattr.item_id and
> iattr.attr_id = 2
>
> But I can't seem to figure out how to delete them in one swoop. I can
> use the previous query to create a "bogus" attr_id value 0xffffffff,
> and then delete rows in the table with attr_id = 0xffffffff... but
> technically that's not quite what I want, since it fails if there are
> rows in the table with attr_id = 0xffffffff that mean something else.
>
> Any thoughts?
My first thought the Multi-Table Delete syntax on the page:
http://dev.mysql.com/doc/refman/5.0/en/delete.html
|
|
0
|
|
|
|
Reply
|
Paul
|
4/28/2007 5:25:06 PM
|
|
On Apr 28, 1:25 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> My first thought the Multi-Table Delete syntax on the page:http://dev.mysql.com/doc/refman/5.0/en/delete.html
===
Note: If you provide an alias for a table, you must use the alias
when referring to the table:
DELETE t1 FROM test AS t1, test2 WHERE ...
===
Oh -- I didn't realize you could use aliases in a delete statement.
|
|
0
|
|
|
|
Reply
|
Jason
|
4/28/2007 9:49:37 PM
|
|
|
2 Replies
250 Views
(page loaded in 0.051 seconds)
Similiar Articles: unambiguous delete from self-join? - comp.databases.mysql ...Hmm. I've done self-joins with SELECT and with UPDATE, but I haven't managed to handle it in DELETE & wonder if it is possible with MySQL. Here's my ... Identitying duplicate values using self-join - comp.databases ...unambiguous delete from self-join? - comp.databases.mysql ... If I run the script again with the 3 unique id's that were found, it doesn't delete ... equi-join VS JOIN or INNER JOIN - comp.databases.mysqlunambiguous delete from self-join? - comp.databases.mysql ... equi-join VS JOIN or INNER JOIN - comp.databases.mysql unambiguous delete from self-join? - comp.databases ... remove any commas/data from mysql? - comp.databases.mysql ...> > How would I change the above to remove any commas ? Don't. Generally comma ... query($sql,$conn); while ($row = mysql_fetch_assoc($result)) { $outstr.= join ... How to merge with duplicate observation on both sides - comp.soft ...Create a self-join relationship from your Test field ... using that constant as the match field on both sides. ... unique field, like a serial number ... to remove ... Remove Duplicate Records Question - comp.databases.filemaker ...Remove Duplicate Records Question - comp.databases.filemaker ... Identitying duplicate ... Using a Self-Join Relationship; Using ScriptMaker to Remove Duplicate Records in ... removing duplicate records based on specific field. - comp.unix ...... got the file sorted by a specific field and now I would like to know how to remove ... Identitying duplicate values using self-join - comp.databases ... removing duplicate ... How best to detect duplicate values in a column? - comp.databases ...-- ===== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp ... > > So anyway, at present I'm leaning towards the self-join with > "LIMIT 1" clause but ... How to count the number of unique entry of one field - comp ...Create a self-join relationship from your Test field to your Test field. ... To do this you need a truly unique field, like a serial number ... to remove ... Paragraph cross references - comp.databases.filemaker... as stated in Paragraph 9 above) will change when paragraphs are added and deleted. ... no > paragraph no, " below)", " above)") , where the join relationship is a self join ... unambiguous delete from self-join? - comp.databases.mysql ...Hmm. I've done self-joins with SELECT and with UPDATE, but I haven't managed to handle it in DELETE & wonder if it is possible with MySQL. Here's my ... DELETE using INNER JOIN - SQL Server Central. Microsoft SQL Server ...I knew it was possible to do DELETE using JOIN I just couldn't figure out that you should do DELETE t1 FROM Table1 t1 INNER JOIN .. not DELETE FROM Table1 INNER JOIN 7/25/2012 4:34:24 PM
|
|
|
|
|
|
|
|
|