What should be PHP/MySQL query for the following requirement? - php

It is a shopping cart, where user can add product to watchlist. Have a seprate page of User's watchlist where all product apepears that has been added to watchlist by that particular user. product_images table is separated from product table since 1 image can have multiple images.
MySQL Tables
Table "product" :
pid(pk),name,price, etc...
Table "watchlist" :
wid(pk),pid(fk),userid(fk)
Table "p_images" :
image_id(pk),pid(fk),image_name
I want to fetch all information from product table and only 1st image of that particular product from p_images table.
product should be fetch by pid in watchlist table.

select * from product p
inner join p_images i on p.pid = i.pid
where p.pid = 1
group by p.pid
order by image_id

I found it accurate using LEFT JOIN check following query.
SELECT *
FROM product p
LEFT JOIN P_images i ON p.pid = i.pid
GROUP BY i.pid

Related

how to get the distinct data from a table and then join other table and take sum of 2 columns

I have two tables one table consisting of my order details and other table having the product details. I want to get the distinct products ordered and their id also sum of same products ordered and price
$query = $this->db->query('SELECT
distinct("productid,picture"),SUM(quantity) as `quantity`,SUM(price) as
`amount` FROM `order_details` LEFT OUTER JOIN `products` ON
`products.serial`=`order_details.productid` where `order_details.user_id` =
`$data[results]`');
return $query->result();
Above is the query i used and if an user ordered a product multiple times and multiple days means i want to get the distinct of product and its id from the table order_details as well as i need the sum of same orders as quantity and sum of price of the same product and join it with another table named products to get the product name based on productid stored on order_details table
Tables details:
My order_details table consists of (user_id,productid,quantity,price).
Under the productid 3,3,2 are the productids, and under the quantity 10,10,11 etc, and under the price: 64,64,396 etc.
The table products table have (serial,name,price,picture).
What i want is to show products with id 3 and 2 then take sum of quantity of each product and also take sum of price of each product. Actually this is for an ecomerce project. the user logged in to view his orders.
Try next query, first you have to inner join products with order_details, this way you can discard all products that was never ordered. Second, you group by the products common attributes, and then use aggregation methods over those groups.
$query = $this->db->query(
'SELECT
p.serial,
p.picture,
p.name,
SUM(o.quantity) AS total_ordered_quantity,
SUM(o.price) AS total_amount
FROM
products AS p
INNER JOIN
order_details AS o ON o.productid = p.serial
WHERE
o.user_id = $the_user_id
GROUP BY
p.serial, p.picture, p.name'
);
return $query->result();
You can check a working example of the query on next link:
http://sqlfiddle.com/#!9/c2a1b0/1

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.

MySQL update query with where condition only updating one record

I have two tables:
Product details - For storing product information.
Product Listings - For multiple copies that may be sold by different users.
I'm using this select query to gather the lowest price of products with the same name on the listings table = (SELECT product_name, MIN(price) FROM product_listings GROUP BY product_name). $product & $min_price are the variables I'm using for the two fields respectively.
This works fine and gives me the lowest price of all products with the same product_name.
product_name | Lowest price
Title | £0.69
Title | £7.98
Now I want to update the product details table (specifically the "lowest_price" column) with this data so that each dynamically rendered product shows the price of the cheapest copy available.
So far I have managed to update only one record with this update query = ("UPDATE product_details SET lowest_price='$min_price' WHERE product_name='$product'");
Is it possible to update all records with an update query like this? Why is it only updating one record?
Assuming you have a product_id in your product_listings table, which relates to an id field in your product_details table, you can do this:
update products p
inner join (
select product_id, min(price) min_price
from product_listing
group by product_id
) pl
on p.id = pl.product_id
SET p.min_price = pl.min_price
If that's not the case, and you only have the product name to join on (not such a good idea), it'd be this:
update products p
inner join (
select product_name, min(price) min_price
from product_listing
group by product_name
) pl
on p.product_name = pl.product_name
SET p.min_price = pl.min_price
Example fiddle: http://sqlfiddle.com/#!9/9b928/1

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

Select non registred values in MYSQL

I have two tables in Mysql, Products and Inventory...
Products
int id_product
varchar name
Inventory
id_inventory
product_id
existence
So when my product '3' is out of stock I delete the row in Inventory where product_id = 3
And now I need to get all values that I have deleted on Inventory cause I need to make a report of missing products...
How can I make this query?
I used SELECT i.* FROM inventory i, products p WHERE i.product_id!=p.id_product but it doesn't works..
Thank you
LEFT JOIN would do as you wish as the rows you are looking for would have no corresponding row in inventory for a row in products, and so that gives the query ...
SELECT
p.product_id
FROM product p
LEFT JOIN inventory i
ON i.product_id = p.product_id
WHERE i.product_id IS NULL
Alternatively you could simply just not delete the row from inventory when unavailable. Just then do something like
SELECT
p.product_id
FROM product p
INNER JOIN inventory i
ON i.product_id = p.product_id
AND i.quantityInStock = 0
Either way works
or
select p.* from product p
where not exists (select null from inventory i where i.product_id=p.id_product)

Categories