I have two table. First is product in which Admin will add the Products and another is product_detail in which vendors will add the detail. Multiple vendors can add same product with the product detail like selling price, offer price etc.
"I want to select products from products table and want to select product detail from product_detail table but only one row of product detail which has less selling price".
So i want to use CI active records to get the final output as product list with its detail.
Take this for example :
Create table #Product(id int, name varchar(100));
Create table #Product_detail(id int, product_id int, detail varchar(100));
insert into #Product values
(1,'Item1'),
(2,'Item2')
insert into #Product_detail values
(1,1,20),
(2,1,10),
(3,1,40),
(4,2,70),
(5,2,50)
this is your basic query:
select
a.*, b.price
from
#product a inner join
(select product_id, min(price) price
from #product_detail
group by product_id
) b
on a.id = b.product_id
In CI, try to do something like this:
$var = $this->db->query("select a.*, b.price from #product a
inner join
(select product_id, min(price) price
from #product_detail
group by product_id
) b
on a.id = b.product_id")->result_array();
sample output :
id name price
----------- ------ -----------
1 Item1 10
2 Item2 50
You first should make getters and setters in your model and using CI active record + pdo It'as more safe from my point of view.
Best regards.
Related
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
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
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)
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
ok here is it. i have two tables: products and warehouse
product table consist of (pid(primary),pname,pcolor) while my
warehouse table has (pid(primary foreign key=> products table),
date_delivered,quantity).
my question is can i display the warehouse table with pid,pname,pcolor,date_delivered and quantity since my primary key in warehouse table is the pid from products table? can i use join with these? if so how?
thank you.
SELECT *
FROM product
INNER JOIN warehouse
ON product.pid = warehouse.pid
Here an Inner Join is used.
select a.pid, pname, pcolor, date_delivered, quantity
from products a, warehouse b
where a.pid = b.pid;