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
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? :-)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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"] ???

 

Link to comment
Share on other sites

yeah what i meant was how,

but nevermind i found out that if i use MYSQLI_ASSOC (as i prefer), you cannot use multiple columns with the same name.

So i've used MYSQLI_NUM in this case and now it works like a charm :-)

 

thanks alot for all your help ;)

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.