Hello.
So I'm doing the article submission atm and I need articles to have multiple categories... Article categories would be stored in articles table under categories field like this {"cat1","cat2","cat3"}. But a mind popped to my head... "What if I want to lookup articles by a category?". If I would do something like SELECT * FROM articles WHERE categories = ? then I can't define what categories which post has... So I can't lookup by a category this way. Any suggestions, solutions? Much appreaciated!
Edit: If I would retrieve all post from database and do some for each solution it would cause server loads or even crashes for several minutes. Right now I have 7,307 articles.
To resolve your actual problem, you can use this sql query
SELECT * FROM articles WHERE categories LIKE '%cat1%'
But, about database scheme, Best way should like to have articles categories and a pivot table article_categories table. A many to many relation would help
articles
id,
title
...
categories
id,
name, ...
article_categories
id,
article_id,
category_id
Related
I am using codeigniter and MySQL to build an ecommerce web application.
This one required three level of categories. So I have created 3 tables. These are-
category
category_id, category_name
subcategory
subcategory_id,subcategory_name,subcategory_category_id
subsubcategory
subsubcategory_id,subsubcategory_name,subsubcategory_subcategory_id
Here they are linked as parent of one another. Finally I have the product table
product
product_id, product_name, product_subsubcategory_id
Now, I need a sql query on this to fetch all product of any specific category.
Something like
$this->Mdl_data->select_products_by_category($category_id);
Please help me on this. I have tried PHP programming to solve this. But it was too slow with lot's of nested loops.
If you need to select all products, that match some specific category, try this request:
SELECT p.product_id, p.product_name, p.subsubcategory_id FROM category c
JOIN subcategory sc ON sc.subcategory_category_id = c.category_id
JOIN subsubcategory ssc ON ssc.subsubcategory_subcategory_id = sc.subcategory_id
JOIN product p ON p.subsubcategory_id = ssc.subsubcategory_id
WHERE c.category_id = 1;
But you should think about changing your database structure to make your requests faster and simpler.
Edit: Answering the comment about how to improve DB.
Current design of database looks correct, according to actual data relations. 1-many for cat-subcat and 1-many for subcat-subsubcat. But this leads to complicated (and possibly slow) queries while usage.
One way I see is to implement many-many relation with additional restriction. You can create additional table cat-subcat, just as you would do if you needed many-many. But in that table you can set unique limitation to subcat_id, so every subcat could belong only to 1 cat and it becomes in fact 1-many relation. But in this case you can move both up- and downwards the hierarchy. This approach will reduce number of JOINs in your query only by 1, but the whole logic of the query would be easier to understand.
Another way. As I understand this is the query for web-store filter. So, new products will be inserted much more seldom, than viewed by category. You can just add subcat_id and cat_id fields to your product, which is not good idea from the point of data structure, but for this particular situation this might be good solution. Every time new product is inserted to DB, you should control the correctness of those 2 fields by PHP or whatever you use on server. But when products are searched by category you will have simple request without JOINs at all.
Both approaches are based on the idea to sacrifice some space for speeding up and simplifying the queries, that are frequently used. Maybe there is even better solution, but I can't find it right now.
I want to store reviews in a flexible system of categories and subcategories, and am currently in the process of designing the database structure for that. I have an idea how to do that, but I'm not entirely sure if it couldn't be done more elegant and/or efficient. These are my thoughts - if anybody can comment on if/how this can be improved I'd be really grateful.
(To keep this post concise, I only list the important field for the tables)
1.) The reviews are stored in the table "reviews". It has the following fields:
id: uniquite ID, auto-incrementing.
title: the title that will show up in <head><title>, etc.
stub: a version of the title without spaces, special chars, etc. so it can be part of the URL/URI
text: the actual content
2.) All categories are in the same table "categories"
id: unique ID, auto-incrementing.
title: the full title/name of the categorie how it will be output on the website
stub: version of the title that will be shown in the URL/URI.
parent_id: if this is a subcategory, here is the categories.id of the parent category. Else this is 0.
order_number: simple number to order the categories by (for display in the navigation menu)
3.) Now I need an indicator which reviews are in what categories. The can be in multiple. My first idea was to add a "review_list" field to the categories and have it contain all reviews.id's that should be in this category. However I think that adding and removing reviews from categories would be a hassle and "unelegant". So my current idea is to have a table "review_in_category" and have an entry for every review-category relation. The structure is:
id: Unique ID, auto-increment.
review_id: the reviews.id
category_id: the categories.id
So if a review is in 3 different categories it would result in 3 entries in the "review_in_category" table.
The idea is, that when a user opens www.mydomain.de/animation/sci-fi/ the wrapper script will break up the URL into its parts. If it finds more than one category with category.stub = "sci-fi", it will check which of those has a parent category with the stub "animation". Once the correct category is identified (most the time the stubs are unique anyway so this check can be skipped) I want to SELECT all review_id's from "review_in_category" where the category_id matches the the one determined by the wrapper script. All the review_id's are put into an array. A loop will iterate through this array and compose the SELECT statement for listing all review titles (and create links to them using the stub values) by "SELECT title, stub FROM reviews WHERE id=review_list[$counter]" and then add "OR id=review_list[$counter]" until the array is completely travelled.
SO my questions are:
- Is the method my creating a single SELECT statement with potentially a large number of "OR id=" parts an "elegent" and/or efficient way to handle this situation or are there better variants?
- Does using a "taxonomy"-style table (review_in_category) make sense or would it be better to store the "membership"/"relation" directly in the reviews or category tables?
- Any other thoughts... I just started to learn this stuff and appreciate any feedback.
Thank you
Your design looks sound.
To retrieve all reviews in a category, you should use a join:
SELECT reviews.title, reviews.stub FROM reviews, review_in_category WHERE reviews.id = review_in_category.review_id AND category_id = $category
Im having trouble with an mySQL Query .What i need is a query that will display on the same page where im viewing a post a list of links to posts in the same category on the side of the page.Im almost finished a music site similar to youtube where users can listen to a track and see a list of tracks beside the post that are in the same category.
Any Information Ive found on this is just related to Wordpress pluggins which im not using.
My Database Tables are
Table_categories
cat_id
cat_name
Table - Mixes
mix_id
mix_subject
mix_description
mix_date
mix_cat -- mix_cat is the foreign key for categories.cat_id
The cat_id of Categories is linked to the foreign key mix_cat in table Mixes.I Know it has something to do with ?id= number of the id in one of the table,s
Cheers guys Thanks for your help
Trevor
You could try something like
Select * from Table_Mixes where id={insert id here, don`t forget to force the int type to avoid SQL Injection}
Get the row and now you have a $mix object with a mix_cat property (or $mix['mix_cat'], if you get it as an array).
Now you just
select * from Table_Mixes where mix_cat={insert $mix->mix_cat here} order_by {whatever} limit {how many links?}
and build the links.
I want to add categories to my website - but with more complex sorting.
Lets say I have article a. now, I want a to be under main category a, but also on category c thats its parent is b.
I thought of doing it like this:
An article would have a main category field, and also parent_ids
parent_ids would be every category this item is belong to.
my problem is: lets say i have: parent_ids: |1|5|2|7|.
now i am in category id - 7.
how would my select query look like?
select * from categories where parent_ids LIKE '%|7|%'
is it efficient? is there a better idea?
Never store multiple values in one column. All column values in a normalized database should be atomic (single-valued).
The relationship between articles and categories is many-to-many. That means you should have a separate table, such as article_category (article_id, category_id) containing one row for each category each article is in.
Now I have been trying to tackle this issue for a few hours now, and I been trying google and the documentation and read it over and over, but I can't seem to understand it.
Let me try explain what im doing, but simplify the project, assume I am making a blog with cakePHP. I got this 3 tables to make it simple we use only the needed fields.
Table: posts
Desc: This table store all our posts
Fields:
postid - Identifier for the post
title - The posts title
Table: sections
Desc: this table contains categories
Fields:
sectionid - Identifier
sectionname - Name
Table: postlinks
Desc: this table store relations between posts and categories.
Fields:
postid – Post ID to determine what post is linked to what section
sectionid – Section ID to determine the section this post belong to
Let me now try explain what I want to achieve with cakePHP by showing an example with regular php and SQL codes:
(Note this code was written up on the go and might not work but it should give you an idea on what I'm trying to do)
<?php
/**
*Block of code to fetch all posts
*/
while ( $rows = mysql_fetch_array($query) )
{
$postid = $rows['postid'];
/** Join postlinks and sections **/
$sql = “SELECT sections.*, postlinks.* FROM postlinks
INNER JOIN sections
ON postlinks.sectionid = sections.sectionid
WHERE postlinks.postid = '$postid'”;
$query = mysql_query($sql);
print “-- TITLE --”;
print “-- CONTENT --”;
print “Posted in “;
/** Loop all categories **/
while ( $rows = mysql_fetch_array($query) )
{
print $rows['sectionname'] . “ “;
}
}
?>
And now I will try explain with words what I am trying to achieve:
When a visitor is browsing the web page, and enter the blog part of the site, I wish to list X latest posts, and this posts can be put in more than one category, and we will store all category links in a separate table, and here is where I am stuffed, I can manage to get the links out but I can't seem to join the tables and loop out the name.
Hope someone can shed some light on this problem I got with either point me in the right direction and/or show me an example code.
As mentioned I have been reading the documentation, and I have attempted searching for the answer but
association is implement as a join in cakephp.
please follow the concept for HABTM i.e "Has And Belongs To Many"
make a declaration in you model association.
declare the association in your cakephp query.
recursive -> -1 or 0 or 1 or 2
to Know more about association please visit this link enter link description here