I am very new to mysql and databases, so I need help here…
How should I use JOIN to fetch a record from the tables below, when the only given variable is the ad_id?
category and category options are filled in manually i.e. the users may not alter them. So they are only reference tables, but I am not sure this is how I should do it…
I have 6 tables:
category:
cat_id (PK)
cat_name
category_options:
option_id (PK)
cat_id (FK)
option_name
option_value:
value_id (PK) (AI) // I think this should have Auto Increment
option_id (FK)
classified_id (FK)
value
classified: (THIS IS THE MAIN TABLE YOU COULD SAY)
classified_id (PK) (AI)
ad_id
cat_id
headline
description
area: // I am thinking about moving these fields over to the posters table, right?
area_id (PK)
classified_id (FK)
area
description
Here is how I insert a classified into the tables:
mysql_query("INSERT INTO classified (ad_id, cat_id, headline, description) VALUES ('$ad_id', $cat_id, '$headline', '$description')");
$last_classified_id=mysql_insert_id();
mysql_query("INSERT INTO poster (classified_id, name, email, tel) VALUES ($last_classified_id, '$name', '$email', '$tel')");
mysql_query("INSERT INTO area (classified_id, area, community) VALUES ($last_classified_id, '$area', '$community')");
I am new to JOIN!
Every category has sub options (CARS -> color) and every option has a value.
I want to, only by having the ad_id, select all this information.
How can I do so?
And should I merge the area and posters table?
Also, please take a careful look at my db, and tell me if there is anything I might have missed…
This is really out of my knowledge-base so detailed explanations are appreciated!
Thanks
It looks like you have two fields that could be the primary key in classified: classified_id and ad_id. Then you have two other tables, poster and area, that have a one-to-one correlation with classified. If this is the case, you could put all the fields in classified.
A query to join the tables in your insert statements would like like this: