Pull only one category from tables - php

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);

Related

Error joining 3 tables php/sql

I'm new to this, so I know I'm missing something simple, but I can't figure it out. I'm trying to join 3 tables together and I've got it working with 2 joins, but when combined in the same query, there ends up being an error.
My 3 tables are:
TBL_Authors
Author_ID
Author_Name
TBL_Publishers
Publisher_ID
Publisher_Name
TBL_Books
Title
Author_ID
Publisher_ID
ISBN
Genre
Price
Cost
Rating
What I have that isn't working:
$query = 'SELECT * FROM TBL_PUBLISHERS
JOIN TBL_BOOKS ON TBL_PUBLISHERS.Publisher_ID = TBL_BOOKS.Publisher_ID
SELECT * FROM TBL_AUTHORS
JOIN TBL_BOOKS ON TBL_AUTHORS.Author_ID = TBL_BOOKS.Author_ID
ORDER BY TBL_BOOKS.Title ASC;';
This query assumes that each book was published.
SELECT
*
FROM
TBL_Books b
INNER JOIN TBL_Publishers p ON b.Publisher_ID = p.Publisher_ID
INNER JOIN TBL_Authors a ON b.Author_ID = a.Author_ID
ORDER BY
b.Title
Book will always have an Author, but not necessarily a Publisher, if it's not published. If you need to fetch all the books irrespective of whether published or not, you will have to change INNER join on TBL_Publishers to LEFT.
This Query Will show you the result of all details of book, publisher name, author name.
SELECT
t1.* , t2.Publisher_Name , t3.Author_Name
FROM
TBL_Books as t1
INNER JOIN TBL_Publishers as t2 ON t1.Publisher_ID = t2.Publisher_ID
INNER JOIN TBL_Authors as t3 ON t1.Author_ID = t2.Author_ID
ORDER BY
t1.Title
Check This and update if this query helps you.

MySQL query from two tables with multiple joining

I've two tables, example - category & items.
1. category table has two fields- cat_id, cat_name.
2. items table has 4 fields field1, field2, field3, field4 which value
is cat_id.
Now, I need to print cat_name instead of cat_id in each row.
How can I do that? What will be the MySQL query?
For more info please, have a look here-
Url: http://smartmux.com/demo_roundflat/admin/
username: test
password: test
You need to use Join and your query should be like this :
"select t1.*, t2.cat_name FROM table1 as t1 JOIN table2 as t2 on t1.cat_id=t2.cat_id";
SELECT c.id as cat_name from category as c , items as i where c.id=i.cat_id
If I understood right you can do something like this:
SELECT items.*, c1.cat_name, c2.cat_name, c3.cat_name, c4.cat_name
FROM items
LEFT JOIN category AS c1 ON c1.cat_id = items.field1
LEFT JOIN category AS c2 ON c2.cat_id = items.field1
LEFT JOIN category AS c3 ON c3.cat_id = items.field1
LEFT JOIN category AS c4 ON c4.cat_id = items.field1;
Let me know if this is what you needed.
SELECT
category.cat_name
FROM
category
JOIN
items ON category.cat_id = items.field4

list one category from list

I want to list one category from the tables, how can I do this?
Table name: products
product_id desc name price qty
Table name: category
cat_id cat_name
I want it to look like this
Category 1 (shirts)
Name Desc Price Qty
I have this but I get an error.. Not sure if its the right way to do it
SELECT products.*, category.cat_name
FROM prodcuts
LEFT JOIN category ON products.cat_id = prodcuts.cat_id
WHERE category.cat_name = "shirts"
You have an error in your ON statement (you have to have one column from each table to join them) and products is misspelled (prodcuts):
Try this:
SELECT products.*, category.cat_name
FROM products
LEFT JOIN category ON products.cat_id = category.cat_id
WHERE category.cat_name = "shirts"
EDIT:
One other thing is that you say your products table is:
Table name: products
product_id desc name price qty
This has no mention of cat_id. You really have to put that in products table (if this wasn't an error on writing the question)
You have missed category table column name in join.
SELECT products.*, category.cat_name
FROM prodcuts
LEFT JOIN category ON products.cat_id = prodcuts.cat_id
WHERE category.cat_name = "shirts"
should be
SELECT products.*, category.cat_name
FROM products
LEFT JOIN category ON products.cat_id = category.cat_id
WHERE category.cat_name = "shirts"

product search across 3 database tables

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?

PHP SQL - Joining three tables

I have three tables called product, product_category and category with product_category being the linking table.
How can I join these tables using SQL in PHP?
The linking table has only productID linking to the product table and catID linking to the category.
Something like this?
SELECT
*
FROM
product
INNER JOIN
product_category
ON product_category.productID = product.productID
INNER JOIN
category
ON category.catID = product_category.catID
Your query should look something like this:
Added the requirement that there should be a productId and categoryId from a variable:
$query = "SELECT * FROM
product p
JOIN product_category pc ON p.id = pc.productId
JOIN category c ON c.id = pc. categoryId
WHERE p.id = {$productId}
AND c.id = {$categoryId}";

Categories