Jump to content

joining two queries


ratcateme

Recommended Posts

i have been coding in php/mysql for a while but stayed with very basic SELECT INSERT UPDATE DELETE

basically at the moment i want to do this

i have two tables

leaves(ID,
branchID,
type,
typeTableID)
and
types(ID,
table)

each has some more fields but they are dont matter here

leaves.type corresponds to a ID in types

basically i have a ID for leaves and i want to use it to get leaves.branchID, leaves.typeTableID and types.table

i tried

SELECT `leaves.branchID`, `types.table`, `leaves.typeID` FROM `types`, `leaves` WHERE `leaves.ID` = $id AND `leaves.type` = `types.ID`  LIMIT 1

and got

Failed. Error: Unknown column 'leaves.branchID' in 'field list'

is this possible and if so how?

 

Scott.

Link to comment
Share on other sites

thanks corbin ill start to go without them

 

ok let me explain a bit further i am trying to remove a row from types.table and leaves at the moment i have it done with 3 queries

the first one to get types.table the second to delete from types.table and the third to delete from leaves

$row = get_row("SELECT leaves.branchID AS branchID, types.table AS table, leaves.typeID AS typeID FROM types, leaves WHERE leaves.ID = $id AND leaves.type = types.ID  LIMIT 1");
query("DELETE FROM {$row['table']} WHERE ID = {$row['typeID']} LIMIT 1");
query("DELETE FROM leaves WHERE ID = $id LIMIT 1");

(get_row() is a function that handles errors and returns mysql_fetch_assoc(). query() handles errors and returns the result object)

is there a way i could join all there queries to make one?

 

Scott.

 

Link to comment
Share on other sites

Well, you can use JOIN in DELETE statements, so you could possibly do that.

 

 

Example:

 

DELETE FROM table1 JOIN table2 ON table2.somecol = table1.somecol WHERE table2.someothercol = 'something';

 

 

That would delete only from table1 even though it takes conditions from table2.  It's possible to delete from multiple tables with JOINs, but I don't remember the syntax.

 

 

 

Ahhh....  http://dev.mysql.com/doc/refman/5.0/en/delete.html Look towards the end of the article (not the comments).

 

 

Edit:  Hrmmm, actually, maybe it's not possible to delete from more than 1 table at once.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.