Jump to content

Need help with a Query!


esbenp

Recommended Posts

Hi Forum!

 

I'm doing a webpage for a client. It's basicly a webshop selling car tires.

Now i have a table in my db, called products (manufactor, tiretype, speedindex)

 

Each field contain a id which refers to 3 other tables (manufactors, tiretypes, speedindexes). Each of theese tables only got a id and a name,

now after i've done my search i need to pull out the names according to the id's from the first table.

 

I'm pretty sure this should be done with JOIN, but im really clueless about join, so could any of you help me? :-)

Link to comment
https://forums.phpfreaks.com/topic/189638-need-help-with-a-query/
Share on other sites

PRODUCTS TABLE
----------------------
id - Id of the product
manufactor - The manufactor
speedindex - -
tiretype - -

Example:
ID: 1
Manufactor: 1
Speedindex: 2
Tiretype: 2


--------------------------
MANUFACTORS TABLE
--------------------------
id 
name - name of the manufactor

--------------------------
SPEEDINDEXES TABLE
--------------------------
id
name - name of the speedindex

--------------------------
TIRETYPES TABLE
--------------------------
id
name - name of the tiretype

------------------------

 

Thats my tables, now i searched the products table for a product with theese values (manufactor=1,speedindex=2,tiretype=2)

and i need to show the name of theese 3 id's.

I could do this if i did multiple queries, but i would like to do it in 1.

 

Was that a bit easier? :-)

You have an id for each of the tables which is fine for indexing those tables alone, but for the single query you want, you will need to link the tables using foreign keys.

 

So for each entry in the products table you would have a manufacturerid, which would link to the id of the manufacturers table and a typeid and a speedindexid which would link to the ids of the relevant table. Then you could do a join query which would extract all the relevant information in one query

the foreign keys are in the product table and connect to primary keys in the other tables.

 

so using aliases to save typing table names too often

 

select * from products p,manufacturers m,speedindexes s,tiretypes t where p.man_id=m.id and p.speedid=s.id and p.tire_id=t.id and p.id=$product_id

ah great got it working! :-)

thx alot!

 

now the next problem, i couldn't find any info regarding how to use this sql sentence?

id	manufactor	tiretype	dimensions	speedindex	price	stock	id	name	id	name	id	name
1	1	       1	    1	                1	        525.25	  5     1	Bridg... 1	H: asldksaj... 1	Sommerdæk

 

so how do i select the names of each table?

like $result["m.name"], $result["s.name"] ???

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.