If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > MYSQL Query in PHP

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-22-10, 07:42
punky79 punky79 is offline
Registered User
 
Join Date: Jul 2010
Posts: 5
MYSQL Query in PHP

Hi,
I have 3 tables
asset
monitoring
protocols
Each asset can have monitoring setup and each monitoring type can have protocols setup.

I need to select all the assets that have monitoring setup of type 'calibration' and also that have protocols setup for the monitoring type.

Asset is linked to monitoring by 'asset_id'
Monitoring is linked to protocols by 'monitor_id'

I have attached a diagram of my tables that might help.
MYSQL Query in PHP-db-tables.jpg

I think I need to build the query using joins but I am not sure where to even start.

Any help or suggestions would be great.

Thanks
Reply With Quote
  #2 (permalink)  
Old 11-22-10, 10:11
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
Quote:
Originally Posted by punky79 View Post
Hi,
I have 3 tables
asset
monitoring
protocols
Each asset can have monitoring setup and each monitoring type can have protocols setup.

I need to select all the assets that have monitoring setup of type 'calibration' and also that have protocols setup for the monitoring type.

Asset is linked to monitoring by 'asset_id'
Monitoring is linked to protocols by 'monitor_id'

I have attached a diagram of my tables that might help.
Attachment 11352

I think I need to build the query using joins but I am not sure where to even start.

Any help or suggestions would be great.

Thanks
The MySQL manual is a good start

as ever Google is your matey

BTW I reckon you are missing several tables from this design, I'd suggest you re examine your entity Asset
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 11-22-10, 10:31
punky79 punky79 is offline
Registered User
 
Join Date: Jul 2010
Posts: 5
thanks for the reply.
I actually came to a solution.
Here it is:

Code:
"SELECT asset.asset_id, asset.asset_date, asset.asset_num, asset.asset_serialNum, asset.asset_name, asset.asset_description, asset.asset_type, asset.asset_department, asset.asset_owner
FROM   
asset 
INNER JOIN monitoring ON asset.asset_id = monitoring.asset_id
WHERE monitoring.monitor_type='callibration'
AND EXISTS
       ( SELECT protocols.protocols_id
           FROM protocols
          WHERE protocols.monitor_id = monitoring.monitor_id )"
Hope this helps someone with a similar query.
Reply With Quote
  #4 (permalink)  
Old 11-22-10, 11:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by punky79 View Post
I actually came to a solution.
only because i handed it to you

but you changed my nice formatting

and you took out my 'booyah' !!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-22-10, 11:11
punky79 punky79 is offline
Registered User
 
Join Date: Jul 2010
Posts: 5
you got me

Thanks again

(link to original solution)
http://www.sitepoint.com/forums/show...09#post4745009

Last edited by punky79; 11-22-10 at 11:14.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On