User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 22 Dec 2004, 17:55   #1
NEWSBOT3
NEWSBOT
 
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
NEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriend
SQL query (deleting things based on other table contents)

ok, my brain hurts.

I have two tables,

table ServiceItems, which has a value ServiceID

table manage_domains which has ServiceID value too

I need to delete the whole row from serviceitems if the serviceID doesnt match one on manage_domains

(i need to make sure domains we don't manage arent being billed)

I can't for the life of me think of how to structure a query to do it.
not that it should matter, but this is for a php script.
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
NEWSBOT3 is offline   Reply With Quote
Unread 22 Dec 2004, 18:17   #2
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: SQL query (deleting things based on other table contents)

DELETE FROM ServiceItems WHERE ServiceID NOT IN (SELECT serviceID FROM manage_domains)

or something like that. Most likely there is something far more clever, but that is what i would try. Not sure if you use mysql, if yes, it probably wont work. mysql doesnt like subselects.
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 22 Dec 2004, 18:20   #3
NEWSBOT3
NEWSBOT
 
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
NEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriend
Re: SQL query (deleting things based on other table contents)

aha, NOT IN

thats a point, i am using mysql. shoulda said that
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
NEWSBOT3 is offline   Reply With Quote
Unread 22 Dec 2004, 18:49   #4
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: SQL query (deleting things based on other table contents)

Quote:
Originally Posted by wu_trax
DELETE FROM ServiceItems WHERE ServiceID NOT IN (SELECT serviceID FROM manage_domains)

or something like that. Most likely there is something far more clever, but that is what i would try. Not sure if you use mysql, if yes, it probably wont work. mysql doesnt like subselects.
I recently learned about the keywords IN and NOT IN and subqueries. I use them in combination with MS Access. They are really cool.
Now I only need a multi table UPDATE an INSERT INTO and I'm set.

What is the easiest way dealing with auto-increment fields where the auto-increment value is used in other tables too?
Say I have two linked tables and want to insert a record into both. The primary key of table A is an auto increment field. The foreign key of table B is linked to the primary key of table A. I now have to do an INSERT INTO on table A, and then a SELECT MAX(auto_inc_field) to get the auto increment value, and then do another INSERT INTO in table B. This is not 100% safe because in theory another person can insert a record between the first INSERT and SELECT and then the MAX(auto_inc_field) will return the wrong value. Is there any way of inserting and selecting the last inserted value in one query?
__________________
"Yay"
Structural Integrity is offline   Reply With Quote
Unread 22 Dec 2004, 20:47   #5
Dante Hicks
Clerk
 
Join Date: Jun 2001
Posts: 13,940
Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: SQL query (deleting things based on other table contents)

Subselects work in MySQL 4.1
Dante Hicks is offline   Reply With Quote
Unread 22 Dec 2004, 21:38   #6
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: SQL query (deleting things based on other table contents)

Quote:
Originally Posted by NEWSBOT3
aha, NOT IN

thats a point, i am using mysql. shoulda said that
If dante says it works in a newer version maybe you are lucky.
But i forgot about the best solution. thats foreign keys. iirc from my DB-lectures in uni there is a keyword that lets you delete a row in a specific table and with it all rows in other table that reference to it by a defined foreign key. That, ofc, only works if your mysql-version and the table type support foreign keys. i've been working with a rather old version of mysql at work for the last year, so i dont remmeber that keyword
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 22 Dec 2004, 22:00   #7
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: SQL query (deleting things based on other table contents)

Quote:
Originally Posted by Structural Integrity
I recently learned about the keywords IN and NOT IN and subqueries. I use them in combination with MS Access. They are really cool.
Now I only need a multi table UPDATE an INSERT INTO and I'm set.

What is the easiest way dealing with auto-increment fields where the auto-increment value is used in other tables too?
Say I have two linked tables and want to insert a record into both. The primary key of table A is an auto increment field. The foreign key of table B is linked to the primary key of table A. I now have to do an INSERT INTO on table A, and then a SELECT MAX(auto_inc_field) to get the auto increment value, and then do another INSERT INTO in table B. This is not 100% safe because in theory another person can insert a record between the first INSERT and SELECT and then the MAX(auto_inc_field) will return the wrong value. Is there any way of inserting and selecting the last inserted value in one query?
for php/mysql there is the php function mysql_insert_id that returns the last inserted auto increment id. maybe there is something like that in your programming language of choice?
If not, you could do the select max on table A first, add one and then use and then use the number for both tables. There is still a 'hole' between the select(max) and the insert, but you always have the same id for both tables and if you check if the insert in table a worked before inserting in table b you are at least sure that there is no wrong data in table b.
or maybe you could do something with threads (make sure that there are never more than one thread in that part of the code)? (im guessing here, i forgot far too much stuff in the last year )

But you are right, there should be some more elegant way to do this.
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 22 Dec 2004, 22:44   #8
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
Re: SQL query (deleting things based on other table contents)

SI: LAST_INSERT_ID() is your friend here. And yes, it's thread safe.
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!

Last edited by Gayle29uk; 23 Dec 2004 at 10:05.
Gayle29uk is offline   Reply With Quote
Unread 23 Dec 2004, 08:06   #9
NEWSBOT3
NEWSBOT
 
Join Date: Dec 2000
Location: The enby cave!
Posts: 4,872
NEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriendNEWSBOT3 needs a job and a girlfriend
Re: SQL query (deleting things based on other table contents)

gah, i dont think mysql likes me

still, i shall play more when i get in in two hours.
__________________
[20:27:47] <nodrog-aawy> **** i think my housemate just caught me masturbating
[11:25:32] <idimmu> you are a little piggy arent you
[13:17:00] <KaneED> i'm so closet i'm like narnia
__________________
Pretty parks and funky scrap metal things here
NEWSBOT3 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 07:37.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018