I am trying to create a product search feature on an E-commerce website I am building but am having a little trouble.
I have 3 tables (categories, sub_categories, products)
Categories table fields: (categoryID, categoryName, active, image)
sub_categories table fields: (categoryID, categoryName, parentCatID, active, image)
products table fields: (productID, shortDescription, longDescription, image, catID, subCatID, active, price, delivery, weight)
Im trying to get my search to find a product if a user types in any part of the short description or long description or if the user types in any part of the category or sub category names it should find all products within those categories.
I dont know whether to do a JOIN or multiple SQL queries. to be honest i've been tinkering with it for a few hours but havnt really gotten anywhere and am now back at the drawing board asking for help
my first attempt looked like this:
$catSelect = mysqli_query($con,"SELECT * FROM categories WHERE categoryName LIKE '%{$term}%'");
$row1 = mysqli_fetch_row($catSelect);
$subCatSelect = mysqli_query($con,"SELECT * FROM sub_categories WHERE categoryName LIKE '%{$term}%' OR parentCatID = '%{$row1[0]}%'");
$row2 = mysqli_fetch_row($subcatSelect);
$productSelect = mysqli_query($con,"SELECT * FROM products WHERE short_description LIKE '%{$term}%' OR long_description LIKE '%{$term}%' OR subCatID = '%{$row2[0]}%' OR catID = '%{$row1[0]}%'");
my final attempt looks like this
mysqli_query($con,"SELECT * FROM products INNER JOIN categories ON products.catID = categories.categoryID WHERE categories.categoryName LIKE '%{$term}%'") or die(mysqli_error());
Could someone help me with the SQL query I need to use?
Try this:
SELECT
p.productID
FROM
products p
LEFT JOIN categories c
ON c.categoryID = p.catID
LEFT JOIN sub_categories sc
ON sc.categoryID = p.subCatID
WHERE
p.shortDescription LIKE '%keyword%'
OR p.longDescription LIKE '%keyword%'
OR c.categoryName LIKE '%keyword%'
OR sc.categoryName LIKE '%keyword%'
You need to do a join, on all three tables, with a where clause that queries the fields you want to search. Something like this:
select * from
products
inner join categories on products.catID = categories.categoryID
inner join sub_categories on products.subCatID = sub_categories.categoryID
where
products.shortDescription like '%query%'
or products.longDescription like '%query%'
or categories.categoryName like '%query%'
or sub_categories.categoryName like '%query%'
;
where the query is the search query string.
this can be helpful to you.....
SELECT * FROM products AS PT
LEFT JOIN Categories AS CT ON CT.catID = PT.catID
LEFT JOIN sub_categories AS SCT ON SCT.subCatID = PT.subCatID
WHERE
PT.active = 'YES' AND
(PT.shortDescription LIKE '%shortDescription%' OR
PT.longDescription LIKE '%longDescription%' OR
CT.category LIKE '%category%' OR
SCT.categoryID LIKE '%sub category%' )
you just put your values using php varrible you can get your search results.
Why don't you use lucene-solr for this?
Related
I'm trying to join 2 tables with same categories but it's not quite working, its not returning results.
I have 2 tables tvshows and movies and both of them have columns categories now i want to go through all of them and see if categories match and return them but I don't know what is wrong with this. Please take a look, thanks!
$query = $connection->prepare("
SELECT
a.id,
a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.id,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.categories = b.categories
WHERE a.categories LIKE ? OR b.categories LIKE ? ORDER BY a.id DESC
");
You are using column named categories in WHERE part of your query, but there are two columns with this name in tables movies and series, so database won't know, which column to search in.
You have to specify probably WHERE a.categories LIKE ? OR b.categories LIKE ? if you want to find rows, where some category is set for movie or series.
If you want to search for movies and series pairs by categories, you should join them by that column. But if categories is something like "Comedy,Action" etc. it won't work. You should consider remodeling database and create table categories and then movies_categories with ID of movie and ID of category rows and series_categories with same structure. Than you will be able to do lot's of queries for selecting movies that has categories same as series etc.
I'm not sure I've understood very well, but give this a try:
$query = $connection->prepare("
SELECT a.hash,
a.categories,
a.thumb_location,
a.name,
a.year,
b.hash,
b.categories,
b.thumb_location,
b.name,
b.year
FROM movies AS a
LEFT JOIN series AS b
ON a.hash = b.hash
WHERE a.categories = b.categories ORDER BY id DESC
");
I am Using PHP, MYSQL. I have two tables
Table posts with following fields
id,
title,
body,
cat_id
Table categories with following fields
cat_id,
cat_name
Suppose, I have Following Data in posts table
id = 1
title = "This is Test Post"
body = "This is the Body of Test Pots"
cat_id = 3
and in categories table
cat_id = 3
cat_name = "Mobiles"
If I Use the Following SQL Statement
"SELECT * FROM posts WHERE id=1";
It will give me following output
id = 1
title = "This is Test Post"
body = "This is the Body of Test Pots"
cat_id = 3
But I Want the Following Output
id = 1
title = "This is Test Post"
body = "This is the Body of Test Pots"
cat_id = Mobiles
How can I get the above mentioned output.
Note: I know some kind of JOIN is used in this kind of situation, But I don't know how to use it. One More thing, Is it possible to get my desired output without using any JOIN, because I heard that JOINs Effect the Efficiency. If JOIN is necessary Please tell me the most efficient Way to get my desired output.
SELECT * FROM posts JOIN categories USING (cat_id) WHERE posts.id = 1
It's possible to achieve the same using a correlated subquery instead (however this is likely to be less efficient than a join):
SELECT *, (SELECT cat_name FROM categories WHERE cat_id = posts.cat_id)
FROM posts
WHERE id = 1
You have to use join querty to join posts and categories in order to get informations from both tables. So you try with the following query
SELECT t1.*,t2.cat_name FROM posts as t1 join categories as t2 on t1.cat_id = t2.cat_id WHERE t1.id=1
Use join query
try this
SELECT id, title, body, cat_name FROM posts
join categories on posts.cat_id = categories.cat_id WHERE id=1
SELECT post.id, post.title, post.body, cat.cat_name from posts inner
join categories cat on cat.cat_id = post.cat_id
and post.id = 1
I think you have to learn JOINS first read this articles
JOINS
JOINS VS SUB-QUERY
now about your question checkout the
SQL FIDDLE
SELECT * FROM posts As p
JOIN categories AS c ON c.id = p.cat_id
WHERE p.id = 1
now its your choice whether to use Joins or Sub-Queries both have its pros and cons
thus select as per your requirement.
hope this will help you ...!
I'm making two web pages one for SHOES and the other for BOOTS.
I would like to fetch all the shoes for one page and then all the boots for the second page.
So basically I would like to know how I can list all the products from one category from my tables.
First table
product_id, name, desc, price, qty, cat_id
Second table
cat_id, cat_name
In the result, I would look like to have the below details.
Boots
Name Desc Price Qty
Shoes
Name Desc Price Qty
You mean something like this?
SELECT *
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_id
AND Table2.cat_name = "Boots"
SELECT *
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_id
AND Table2.cat_name = "Shoes"
something like this:
SELECT t1.*, t2.cat_name
FROM first_table t1
LEFT JOIN second_table t2 ON t1.cat_id = t2.id
WHERE t1.cat_id = {the cat id for shoes or boots}
Making a few assumptions about your table names:
$pdo = new PDO(); // Insert your connection string here
$pdo->prepare("SELECT products.* FROM products LEFT JOIN categories ON products.cat_id = categories.cat_id WHERE cat_name = :Cateory");
$pdo->bindParam(":Category", $category);
$pdo->execute();
$data = $pdo->fetchAll(PDO::FETCH_ASSOC);
I have categories table:
cat_id
cat_name
Than categories - posts relationship table cat_rel:
cat_rel_id
cat_id
post_id
And posts table:
post_id
post_title
post_content
Now I have data in all these tables, I need to pull out categories without to repeat those that have more than 1 post. I get somehow category repeated if it has more than 1 post, eg. categories like:
php, python, c++, java, ruby
if I have 2 posts under PHP, I will get:
php
python
php
java
and this is the query I run:
SELECT categories.cat_name, cat_rel.post_id
FROM categories
LEFT JOIN cat_rel ON cat_rel.cat_ID = categories.cat_ID
Any help about making those categories not to repeat, would be appreciated
Use DISTINCT
Have a look here:
SQL SELECT DISTINCT Statement
This would list the categories with more than one post:
SELECT categories.cat_name as Category
, count(distinct cat_rel.post_id) as PostCount
FROM categories
LEFT JOIN cat_rel
ON cat_rel.cat_ID = categories.cat_ID
GROUP BY categories.cat_name
HAVING count(distinct cat_rel.post_id) > 1
Try this:
SELECT DISTINCT categories.cat_name, cat_rel.post_id
FROM categories
LEFT JOIN cat_rel ON cat_rel.cat_ID = categories.cat_ID
I'm a little bit confused about these kind of new query types for me (DISTINCT,GROUP BY etc.)
I have three tables in my database.
BRANDS
PRODUCTS
RECIPES
What I would like to do is to list BRANDS then look for PRODUCTS of that brand and look if there is a RECIPE of those products, then I want it to be written.
I'm able to write it with the basic queries;
$sql = "SELECT * FROM BRANDS";
$sql2 = "SELECT * FROM PRODUCTS WHERE BRANDID = 1";
$sql3 = "SELECT * FROM RECIPES WHERE PRODUCTID = 2";
but it repeats the brands.As far as I googled I have to use GROUP BY or DISTINCT in my query.
How should I create my query layout and which of them I should use?
Thank you.
I don't think you necessarily need to do all the distinct and grouping, unless your database has duplicate rows. You can select all of that in one query like so:
Select Brands.BrandId, Brands.BrandName, Products.ProductId, Products.ProductName, Recipes.*
From Brands
Inner Join Products On Products.BrandId = Brands.BrandId
Inner Join Recipes On Recipes.ProductId = Products.ProductId
Where
Brands.BrandId = 1 And
Products.ProductId = 2
Order By Brands.BrandId, Products.ProductId, Recipes.RecipeId
Please note that I assumed you have a Brands.BrandName and Products.ProductName column. This query will get you a complete recordset, filtered based on Brand and Product.