Mysql-foreign keys and array [duplicate] - mysql

This question already has answers here:
MySQL many-to-many relationship with FOREIGN KEYS
(3 answers)
Closed 7 years ago.
I have two tables. One is Category and the other one is Products. The problem is that one product can have more than one Category and I don't know how to do it.
I've related the tables with a foreign key 1:n.
Thanks

You'd need another table, maybe called ProductCategories.
Each row contains a foreign key to a product and a foreign key to a category.
When you want to find the categories for a product, query for all ProductCategories with that product ID.

You should create 3 table and related them together, something like this:
Table: Items
Columns: ID, Item_ID, Item_Title, Content
Table: Tags
Columns: Tag_ID, Tag_Title
Table: Items_Tags
Columns: Item_ID, Tag_ID
Item_ID is a foreign key in Items table.
Items_Tags is a correlation table.
And for example this code prints all x tags:
SELECT * FROM items i
LEFT JOIN item_tags it ON i.item_id = it.item_id
LEFT JOIN tags t ON t.tag_id = it.tag_id
WHERE tag_title = 'x'

Related

Inner Join on many-to-many relationship on the same table

I came across a weird problem while I was trying to apply the Inner Join on the table which has 3 columns -
Table category_subcategory_relation
id (PK)
category_id_ref (FK)
subcategory_id_ref (FK)
The columns category_id_ref and subcategory_id_ref are the primary keys of the table category.
Table category
id (PK)
category_name (Varchar(200))
I want to Inner Join this two tables and get the columns as following -
Category_Name
Subcategory_Name
I am not able understand how would I be able to get the category_name column from the category table twice, once for the column Category_Name (FK) and again for the column Child_Category_Name (FK).
You would have to perform two joins, one each on category_id_ref and subcategory_id_ref:
SELECT a.*,
b.category_name AS Category_Name,
c.category_name AS Subcategory_Name
FROM category_subcategory_relation a
JOIN category b
ON a.category_id_ref = b.id
JOIN category c
ON a.subcategory_id_ref = c.id
While your category table contains category and subcategory information, you'll have to look at them as two separate tables (one for category and other for subcategory) to bring this information back to your category_subcategory_relation table.

Database model correction mysql?

I have created a database of 3 tables: "products", "brands" and "categories".
products table has product_id, brand_id, category_id, etc. So by using category_id and brand_id from other tables, I can pick up and show the different data from product table.
However, I want a solution like, for example, what if a product lies in two or more categories? What should be the database model and what table changes should I need to do more in my database?
My table structures are listed below:
Database:
Brand table:
Category table:
Product table:
Mapping cardinalities between product and category table are many to many. So you have to create a new table to keep records product and category relationship according to database normalization. SO remove category_id from the product table and create a table like as the below. For example
create table productCategory(
product_id integer references product_table(product_id),
category_id interger references category_table(category_id),
primary key (product_id,category_id)
);
there is two solution from my logic:
Change type of cat_id column in product table, using VARCHAR or TEXT, so each product have cat_id with separated by comma or colon, example value: 1,4,5
Remove column cat_id from product table, Add another table for mapping product into cat_id, example scheme:
CREATE TABLE product_mapping (
id INT(11) NOT NULL AUTO_INCREMENT,
product_id INT(11) NOT NULL,
cat_id INT(11) NOT NULL,
PRIMARY KEY (id)
)
So.. you can query by joining 3 table for product information, with example query as below:
SELECT pm.cat_id AS m_cat_id, pm.product_id AS m_product_id, p.*, c.*
FROM product_mapping AS pm
INNER JOIN products AS p ON p.id = pm.product_id
LEFT JOIN categories AS c ON c.cat_id = pm.cat_id
WHERE pm.product_id = :produc_id

How to join to a one to many relationship table?

I have three tables, lets say for example table 1 is locations, then table 2 is location_items then table 3 is items
location_items is the link between locations and items
each locations has one location_items and each location_items has either one or more items
Column names:
locations table: location_id primary key
location_items table: location_items_id primary key, location_id foreign key, item_id foreign key(one is to many, 1 location, many items).
items table: item_id primary_key, name(name of the item that i want to get)
Now what I want is select the locations, then join to location_items, then join to items to get the lets say the first items.name that is not null. Here is my sample query:
SELECT l.location_id,COALESCE(i.name)
FROM locations l
LEFT JOIN location_items li USING(location_id)
LEFT JOIN items i USING(item_id)
WHERE l.location_id LIKE '%P021%'
GROUP BY l.location_id
But I only get all the location_ids with null name

How to select entries WITHOUT matching dependents in same table

What I have is a table containing people. Some people are primary entries and others are secondary (partners, children etc)
The primary entries have a 'cls' of say 3 (where cls is a column name). The secondary entries have different cls, say 4. The secondary entries also have a 'primary' field linking them back to the unique ID of their primary person. (primary field is empty on primary people)
What I want to do is select all primary entries that do not have anyone linking back to them.
Here's where I got to, but it is obviously not right. I figure there is some other form of JOIN that I need? (pp1 is referring to secondaries and pp2 is referring to primaries)
SELECT pp2.per_ID
FROM person pp1 LEFT OUTER JOIN person pp2 ON pp1.primary = pp2.per_ID
WHERE pp1.cls = 4 AND pp2.cls =3
AND pp2.primary IS NULL;
.
TABLE person
COLUMN per_ID, cls, primary
SELECT p.* FROM person as p
LEFT JOIN person AS s
ON p.`primary` = s.per_ID
WHERE s.per_ID IS NULL AND p.cls =3

N:N Relation Mysql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
problem in many to many relationship
I have a table of movies and a table of categories. I would like to register one more category for a film. And a category for several films. That is, a relation N: N. How do I do that in php and Mysql?
For example:
Categorie 1 -> Movie 1
and Movie 2
Movie 2 -> Categorie 1
and Categorie 2
For a many to many relationship, you need a third table called the junction table
So it would look like that
Movie
id | desc
Category
id | desc
MoviesCategories
id | movieID | categoryId
Your selects would be on MoviesCategories and would look like this
SELECT *
FROM
MoviesCategories INNER JOIN Category ON MoviesCategories.CategoryId = Category.Id
INNER JOIN movie ON MoviesCategories.MovieID = Movie.ID
You need a table for that relationship. Like this:
MovieToCategory
ID
CategoryID
MovieID
Alternatively you can create a composite primary index only allowing each movie to category combination one time:
MovieToCategory
CategoryID
MovieID