I am using PHP via Codeigniter for this scenario.
It's a basic category listing that has items assigned with to those categories. What I'm having problem with is showing the item count beside the category names on the category listing page.
How I was doing it was a very ineffective way i think, and I would really like to know what would be the common practice for this sort of thing.
So I was getting the category list in my controller, and then in my view, on the loop thru the categories, I call a model method to grab the count of the respective cat_id on every iteration. (horror!)
Should I be:
A) Getting the categories AND it's item count from 1 SQL statement
OR
B) Process the category list in the controller and get the categories' corresponding item counts in the controller
If so, how to ?
I would prefer to have one query with everything I need, if your DB design allow to do so.
Assuming you have a Categories table and a Products table you should be able to do something like this:
Select C.Category, Count(P.Id) as ProductsCount
From Categories C left join Products P on C.Id = P.Category_Id
Group by C.Category
Order by C.Category
Related
I am building an auction website. Right now, I am building the item description page, that has item details, as well as current bid history. My bids table has a FK of Item_id.
My current query looks something like this:
SELECT bids.Item_id, bids.User_email, bids.Bid_amount, products.*
FROM bids
INNER JOIN products
ON bids.Item_id=products.Item_id;
This returns all of the bid information I need - but also returns the item description for every bid row. I only need the product information once. Is it best to just use two queries on this?
Any help is appreciated
If you need the bids data separately from the products data, then you should use two queries.
One query cannot really be arrange to return different columns for different rows.
SELECT b.Item_id, b.User_email, b.Bid_amount, p.*
FROM bids b
INNER JOIN products p
ON b.Item_id=p.Item_id
WHERE p.Item_id=something;
This will not repeat products..
I am working on an e-commerce site from scratch using PHP and MYSQL. I have one table for categories, with a column for ID and a column for Parent_ID.
When displaying the products on categories.php, I have it set to display products where the Parent_ID OR the ID equals the $_GET['id'] field. But now I've run into a problem.
In my "Groceries" category, I have the "Cleaning & Home" subcategory, and under "Cleaning & Home" I have several categories like "Laundry", "Bathroom Supplies", etc.
My problem is that products in the third level don't display in the "Groceries" category, because technically the parent ID of "Laundry" is "Cleaning & Home". There will never be more than three levels (Parent, child, grandchild), but I would like categories in the grandchild level to also display in the parent level.
I've tried looking through MYSQL documentation and on other forums but so far no luck.
This requires a couple of joins to get to the top parent:
select c.*,
coalesce(cp2.id, cp.id, p.id) as MostParentId
from categories c left outer join
categories cp
on c.parent_Id = cp.id left outer join
categories cp2
on cp.parent_id = cp2.parent_id
where c.id = $_GET['id'] or cp.id = $_GET['id'] or cp2.id = $_GET['id'];
You can then compare the ids using or for a match to the parent category, subcategory, or whatever.
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.
I have a quick question in regards to the approach to displaying products on an e-commerce package I am putting together. The problem that I facing is that I would like visitors of the site narrow their search.
For example, a use case would be:
A Visitor is currently browsing a season of products (eg. Summer Collections)
He / She should then be able to filter by category and brand within that season, so for example they might decide that they only want to see Pants from Clothes Galore.
The problem, I ma facing is that doing a single SQL query in order to find products that match all three of these factors (so the product is in the summer collection, is a pair of pants and made by clothes galore). The thing that makes this overly difficult is that products can be in multiple categories and seasons. So there would need to be an insane amount joins in order to grab the right result.
My database structure is as follows:
product -> product_category <- category
product -> product_season <- season
product <- brand (a product can only be made by one brand)
Hope someone can share their wisdom on this...
If you have a big catalog, you might be better to use Apache Solr ( http://lucene.apache.org/solr/ ). Otherwise, there are a few approaches.
You don't need the overhead and straight SQL isn't that insane, will perform reasonably well:
SELECT product.*
FROM product
LEFT JOIN product_category
LEFT JOIN category
LEFT JOIN product_season
LEFT JOIN season
WHERE season = ? AND category = ?
Alternatively, If you don't like the number of rows returned, you can aggregate the category and season into the product table (new columns), then use like queries to find things:
SELECT product.*
FROM product
where product.categories like '% category %'
and product.seasons like '% season %'
Select product.* From product
Left Join product_category On (product.product_id = product_category.product_id)
Left Join product_season On (product.product_id= product_season.product_id)
Left Join brand On (product.product_id = brand.product_id)
Where product_category.category_id = '$catId'
And product_season.season_id = '$seasonId'
And brand.brand_id = '$brandId'
Order By product.product_name
Ok, this is assuming product, product_category, product_season, brand, season, category are all tables. You should pass in the ID's of the season and category table directly from whatever you are using to filter the search by so that you don't have to join these tables.
I have a categories table set up as so [id, name, parent_id] and a items table [id, name, category_id, visible]. What I'm trying to do is create a query that will return all the ids of all non-empty categories, non empty being that it or one of it's children has at least one item belonging to it. What would be the best way to do this in MySQL?
edit
SELECT DISTINCT category_id FROM Items
This works for categories containing items, but I also need the parent categories of all item containing categories. This query will be used as a subquery along with some other filters.
Top Level Category
->Second Level Category
-->Third Level Category
--->Item 1
--->Item 2
Maybe off topic, but I think it is still worth referencing: Extensive Article on Managing Hierarchical Data in MySQL.
All non-empty categories, and only those, have items with category_id pointing at them, therefore you could just select category_ids from items table:
SELECT DISTINCT category_id FROM Items
As far as I know, you can't select all the ancestors of these categories in one query, however you might want to use another tree model.
With the nested set model, your query could look like this:
SELECT DISTINCT c.id FROM Categories c JOIN Items ON c.id = category_id JOIN Categories ancestors ON c.lft BETWEEN ancestors.lft AND ancestors.rgt
I'm not sure if it'll work, but you can try.