How to organize this in a mysql table? - php

I have some items, each item has a category and a sub-category. What is the best way to organize this in mysql ?

Create a table
catID
CategoryName
ParentCategory ....any other fields
Parent Category column will store the catID of Parent category if any.
or
CategoryTable
catID
CategoryName
CategoryRel Table
----------------------------------
ID
catID
SubcatID
where catID and subCatID are the IDs from category table.

First, you have a big decision to make. Will you design your database so that there are only two levels of category? Or will you allow for more than two levels in the future?
If you are willing to accept a permanent limit of category and sub-category, you can add two tables. Categories will list the allowed categories. Subcategories will list the allowed Subcategories and what Category they belong to. And your data table will include only the Subcategory. (If there is no information about a category other than its name, you could dispense with the Categories table and just include the category name in the Subcategories table.)
If you want to allow any number of levels, you will have only a single Categories table. Each row will have the category name and another column with the parent category. Your data table will still include a single category column.

categories
-----------
id
title
description
subcategories
-------------
id
title
description
category_id
items
-----------
id (*i had misspelled id as 'if'. corrected it here)
title
description
subcategory_id

Related

PHP how to make a relationship between two tables

I'm an absolute beginner to PHP and MySQL. I have a pre-coded project which I currently making personalised modifications.
Following are the screenshots of my database.
From the front-end I need to list all categories, and all respective products under each category.
I can't figure out how to bind two different tables and show products related to each category.
you need manyToOne relation with product table and categorie table:
one categorie have one or more product:
you need join between tow table:categorie(id_categorie,name,etc..),product(id_product,name,etc...,#id_categorie)id_categorie:foreignKey in product table.
In SQL table should have foreign keys. In PHP just take all categories and all products. When you list a category just list all of product with have categor_id equal to id of category with you listed:
categories: ID, name
peroducts: ID, name, category_id
List:
category 1
- products: all with have product.category_id = category.id

Adding Category and Subcategory in Database

I am doing a project in PHP, MySQL. I need to create a database in which there is a category to which there can be many subcategories ( like for a course book I can have physics, computer science, mathematics etc.).
Also there can be some categories which do not have subcategories (say Mythological Books).
The administrator has to add new category or subcategory via a form in PHP page which then updates the database.
How should I design the structure of my table where if admin for following two cases to be incorporated:
Admin only wants a subcategory to be added then upon selecting its parent category, it should be done.
Admin wants to add a new parent category and there exists no subcategory for it.
Please suggest how should I create my table(s).
I guess you should create table category with fields
id
parent_id
name
if it's root level category, its parent_id = 0,
otherwise its parent_id equals id of parent category.
You are talking about two entities, categories and subcategories. There is no further hierarchy (which would only make matters much more complicated). So I see two tables:
Category
category_id = primary key
name = not nullable
Subcategory
subcategory_id = primary key
name = not nullable
category_id = not nullable, foreign key to Category table

mysql bulk process data

I have a MYSQL DB with three tables
Table 1 - Business
Has 200,000 plus records.
Two of the 20 fields in this table are category and subcategory
Both fields store numbers that reference the tables 2 and 3 respectively.
Table 2 and 3 - Category and Subcategory
Each of these tables stores two fields a number Id and the text name for the category.
For example Category or Subcategory table - ID:32 /Name:Pet Supplies
I need to process table 1 so that i check each records category and subcategory, grab the text name reference from both category and subcategory tables and put the output into a new field in table 1.
I hope someone can help guide me in the right direction.
UPDATE business, category
SET business.categoryName = category.name
WHERE business.categoryID = categoy.ID;
Repeat for subcategory.
Better, delete the strings from the business table and get the category like this when you need it
SELECT b.name, b.whatever, c.name, s.name
FROM business AS b, category AS c, subcategory AS s
WHERE b.categoryID = c.ID AND b.subcategoryID = s.ID;

Query issue in Yii

Yii beginner here. I am facing some problem with how to query the database with the scenario I have. Here is what my two tables look like. The reason category is maintained in a separate table is because a product can belong to multiple categories.
Product Table
-------------
id
product_name
product_desc
product_color
product_price
Category Table
--------------
id
product_category
product_id
A product can belong to multiple category.
Now, let's say I want to find the products of category 'xyz' with color 'blue'. I am not sure how do I query both the tables using two different models (or not) to achieve this. Any help?
Here you have many to many relationship where one product can belong to multiple categories and one category can belong to multiple products.
You will definitely need a third table
Product Table
-------------
id
product_name
product_desc
product_color
product_price
Category Table
--------------
id
category_name
Product_Category Table
----------------------
product_category_id
product_id ( foreign key to Product->id )
category_id (foreign key to Category->id )
You can implement simple query without having a model like this:
$connection=Yii::app()->db;
$sql = "SELECT .....";
$command = $connection->createCommand($sql);
$dataReader=$command->query();
$rows=$dataReader->read();
print_r($rows);
Arfeen's solution is perfectly valid.
Although, I would assume that you have already read the official Relational Active Record documentation on yii website.

Multiple database joins

I have three tables: categories, content_info, and content.
The categories table contains the category's id and the ID of its parent category.
The content_info contains two columns: entry_id for the post's ID and cat_id for the ID of the post's category.
The content table contains multiple columns about the post - such as ID, title, etc.
I have a variable in the URL called parent_id which corresponds to the parent of a category. I want to list all the POSTS (not CATEGORIES) which belong to a category with a parent of the parent_id value.
For example, say the parent_id value is 5. Each post might belong to a category with an ID of 20, but that category belongs to the parent category (whose ID is 5). I want to list all the posts who belong to categories with a parent value of whatever the current parent_id happens to be.
Is there a way of doing this with MySQL joins instead of changing the PHP?
This should do it:
SELECT c.* FROM content
JOIN content_info ci ON ci.entry_id=c.id
JOIN categories cat ON cat.id=ci.cat_id
WHERE cat.parent_id=<parent_id>
This return all posts (content rows) which belong to a category which parent is parent_id
Or with subqueries:
SELECT c.* FROM content
JOIN content_info ci ON ci.entry_id=c.id
WHERE ci.cat_id IN (SELECT id
FROM categories cat
WHERE cat.parent_id=<parent_id>)
SELECT c.*
FROM content c,
categories cat,
content_info ci
WHERE c.id = ci.entry_id
AND cat.id = ci.cat_id
AND cat.parent_id = 5

Categories