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;
Related
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
Table categories:
id, name,
-------------
1 category1
2 category2
3 category3
4 category4
Table posts:
id, category, title
--------------------
1 1, 3 title1
2 4, 2 title2
3 1, 4 title3
I would like to have a show categories and counter posts in this category. In the category column in the table posts I IDs of categories, which are separated by ', '. How to do that is searched when category = category ID and show counter with a minimum of SQL queries.
You should fix your data structure to have one row per post and category. A comma-separated list is not a SQLish way to store data for many reasons. You should change this to a junction table.
Here are some reasons why:
Numbers should be stored as numbers and not strings.
Foreign key relationships should be properly defined, and you can't do that with a string to a number.
An attribute should contain one value.
MySQL doesn't have very good support for string processing functions.
Queries using the column cannot be optimized using indexes.
You should have a table called PostCategories for this information.
Sometimes, we are stuck with other peoples bad design decisions. If so, you can use a query such as:
select c.id, count(*)
from posts p join
categories c
on find_in_set(c.id, replace(p.category)) > 0
group by c.id;
I have a form with two select inputs. In each input there are products as options. Each visitor must select two products as their favorites. Their choice goes into a table named: visitors_fav
The structure of table is:
visitors_fav:
vis_ID - productID_1 - productID_2
As you see i fetch the product IDs not their names and store them into the table.
I have another table named: Products, the structure is like:
Product_ID - Product_Name
How can I get the Product_Name for both productID_1 & productID_2 in a query?
(PHP Codeigniter active record is preferred, and also my database is mySQL)
you must join visitors_fav with Products two times to get the results you want. try the following query:
select vis_ID, prod1.Product_Name as Product_Name1, prod2.Product_Name as Product_Name2
from visitors_fav vis join Products as prod1 on vis.productID_1 = prod1.Product_ID
join Products as prod2 on vis.productID_1 = prod2.Product_ID
I have two tables.
TABLE PRODUCT
product_id
category_id
product_name
TABLE CATEGORY
category_id
name_eng
name_ita
name_rus
When I save the information of the product into database, I choose the category from a list (previously created > TABLE CATEGORY). Each category has an ID and three fields (the name in English, Italian and Russian).
Now I would like to show the right category name depending on the language of the page.
How can I show the values of the second table (TABLE CATEGORY) depending on the value cat_id, which of course is the same for both tables?
Hope you can help me.
Thanks,
Alessandro
Using a JOIN:
SELECT tp.*. tc.* FROM `TABLE PRODUCT` tp JOIN `TABLE CATEGORY` ON tp.category_id=tc.category_id
You could do something like
'SELECT *, name_'.$lang.' AS name '.
'FROM product AS p '.
'JOIN category AS c ON c.category_id = p.category_id'
where $lang is a php variable containing the language of the page (ita, eng or rus). In this way you will obtain a column called name with the correct category name
I am trying to have a section in a shop I am creating to display 5 random products, from that Category ID.
Firstly every category has an ID, and a set of Sub Catrgories, every Sub Cat has an ID, and within every Sub Cat is a number of products. Every Product also has an ID.
The Products table contains the ProductID and the SubCatID.
The SubCat Table Contains the SubCatID and The CatID
The Cat table contains only the CatID.
SO I need to display 5 random products by the CatID. I can get random products using a query similar to this:
$randomprod = mssql_query("SELECT TOP 5* FROM Products WHERE SubCatID = '1' ORDER BY NEWID()");
while ($echorand = mssql_fetch_array($randomprod)) {
I need a way to join the tables so I can display all products under a certain CatID however, and am finding it difficult because my Products table doesn't contain a CatID. I am aware there are a number of joins, but am fairly new to PHP and even newer to MS SQL. Can anyone tell me what join is best, or point me in the correct direction please?
Join products to sub categories, join sub categories to categories, like this:
SELECT TOP 5 Products.Name, SubCatergory.Name, Category.Name
FROM Products
INNER JOIN SubCatergory ON Products.SubCatID = SubCatergory.SubCatID
INNER JOIN Catergory ON SubCatergory.CatID = Category.CatID
WHERE Category.CatID = 1
ORDER BY NEWID()
I have used INNER JOIN in the above example.