Jump to content

[SOLVED] PHP QUERY TO MULTIPLE TABLES


diesel_

Recommended Posts

Hi All,

 

I am asking users to enter a search term this will search the database and list the matches. here is a sample of the table structure

 

ASSET TABLE CATEGORY TABLE

Asset_id Category_id

name name

Category_id

 

now my problem is i want users to be able to search the ASSET TABLE records for a category `name` that is found in the CATEGORY TABLE but my query produces a cartesian product i want to find a way of checking if ASSET TABLE is using a category `name` via the PK->FK link

 

here is my failed query

 

$query = "SELECT * FROM asset, category, manufacturer, model, location, contact, deployment, condition

WHERE asset.category_id = category.category_id

AND asset.manufacturer_id = manufacturer.manufacturer_id

AND asset.condition_id = condition.condition_id

AND asset.location_id = location.location_id

AND asset.contact_id = contact.contact_id

AND asset.deployment_id = deployment.deployment_id

AND asset.asset_id LIKE \"%$trimmed%\"

OR asset.asset_name LIKE \"%$trimmed%\"

OR category.category LIKE \"%$trimmed%\"

OR manufacturer LIKE \"%$trimmed%\"

OR model_name LIKE \"%$trimmed%\"

OR condition LIKE \"%$trimmed%\"

OR location LIKE \"%$trimmed%\"

OR contact.name LIKE \"%$trimmed%\"

OR description LIKE \"%$trimmed%\"

OR comment LIKE \"%$trimmed%\"

OR deployment.deployment LIKE \"%$trimmed%\"

order by asset_id";

 

ignore the \"%$trimmed%\" this is just the result of the search term entered by users

 

Thanks

Link to comment
Share on other sites

Can some one show me a query that would display records from one table but in one of the fields it places the integer value with the varchar from another table the two tables are linked via the FK

 

TABLE1

Asset_id

Asset_name              TABLE2

Type_id  ---------->Type_id

                            Type_name

 

so i want a query to display

 

Asset_id -- Asset_name -- Type_name

 

 

Thanks

Link to comment
Share on other sites

Hi

 

TABLE1

Asset_id

Asset_name 

Type_id           

 

TABLE2

Type_id 

Type_name

 

I need a query to use the two tables  but exclude the type_id details, for example

 

SELECT table1.asset_id, table1.asset_name, table2.type_name

FROM table1, table2

Where table1.type_id = table2.type_id

 

It gets tricky becuase i use a search field to form a dynamic query. The search field allows a user to enter a type_name but type_name is not in table1 so it searches table2. The problem i get is that if it finds a match in table2 it prints the results but i only want to print the result if the type_id is used in table1 (i.eTABLE1 asset_id =1, asset_name= packaging, type_id = 2) (TABLE2 type_id=2, type_name = MEDIUM).

 

This database is an inventory database so table2 consists of box types (i.e big, small, medium etc) so when i run a query for all medium boxes in stock i want it to print only the medium boxes in table1. But ignores the condition i awant in place.

 

Thanks

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.