list one category from list - php

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"

Related

query 3 tables form db and get all columns of all tables

I have 3 tables products , category and product_category here are strutures of tables
products = id(pk), name , sku, ...
category = id(pk),name,parent_id(self relation)
product_category = category_id(fk), product_id(fk)
here is my query so for running
SELECT `products`.*,`product_category`.`category_id` as catid FROM `products` LEFT JOIN `product_category` ON products.id = product_category.item_id GROUP BY `products`.`id`
it brings all products and category_id from product_category table i want data from category table as well like category.name . how i can do this?
Is this not as simple as..?
SELECT *
FROM products p
JOIN product_category pc ON p.product_id = pc.product_ID
JOIN category c ON pc.category_id = c.category_id;

MYSQL Query Calculation in relationship

MySQL Schema
product_categories:
id, name
products
id,category_id,name,description
product_offers
id,product_id,name,email
product_offer_negotiations
id,product_offer_id,offer(float),by_user(ENUM-Seller/buyer),status
This is there query i have tried...
select products.*,
product_categories.name as category_name,
COUNT(product_offers.id) AS total_offers,
ROUND( AVG(product_offer_negotiations.offer), 2) AS avg_offer
from products
inner join product_categories on product_categories.id = products.category_id
inner join product_offers on product_offers.product_id = products.id
inner join product_offer_negotiations on product_offer_negotiations.product_offer_id = product_offers.id
and product_offer_negotiations.by_user = ?
group by products.id;
I would like to get all fields from product table, total_offers and avg_offer offer for product (only highest value from negotiation table per offer_id - no duplicatation)
This query only returns first row of products table. Thanks for help.

Select product id from product genre table but show by exect macting

This can be question which can ask on this site but I unable to find answer for it.
My question is that I have 3 tables
a . Product table
b. category table
c. product_category table
Any product can have multiple categories
So if i want to know all products which contain any category so i can use mysql query like
Select *
From products
left join product_category on (product.id = product_category.product_id)
Where category id in (45,56,78)
but if I want all only those products which have all these categories then how i find it ?
Select p.id
from products p
join product_category pc on p.id = pc.product_id
where category_id in (45,56,78)
group by p.id
having count(distinct category_id) = 3

Pull only one category from tables

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

Fetching product categories in one row with product info

I have 3 tables
products categories products_categories
------------ ------------- --------------------
product_id category_id product_id
product_title category_title category_id
Each product can belong to multiple categories as the db schema shows.
I join the three tables to get a listing of the three last inserted products.
SELECT p.product_id,
p.product_title,
c.category_id,
c.category_title
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id GROUP BY p.product_id LIMIT 3
With the above query I get the products but with the first category that the product belongs to.
My question is, is it possible by modifying the query to get all the categories that each product belongs to, in one row along with the product info?
Or the only way is to execute another query for each product while fetching the list to get its categories?
You could try:
SELECT p.product_id, p.product_title,
GROUP_CONCAT(c.category_title)
FROM products p
INNER JOIN products_categories pc ON p.product_id = pc.product_id
INNER JOIN categories c ON pc.category_id = c.category_id
GROUP BY p.product_id
See GROUP_CONCAT syntax here
Dont use "Group By". Then in your PHP Code group manually to product_id and collect all categories.

Categories