So i have two tables:
order_product
--------------------------------------------------
| ProductID | Quantity
--------------------------------------------------
products
-------------------------------------------------------------
| ProductID | productname | Desc | Price | Stock | Image
------------------------------------------------------------
and i need to get all of the same product ID, show their quantity then times that by their price and show the grand total of all.
My problem is i'm trying to show a checkout page which shows everything in a list, but how do i combine the two tables? Also, there are no foreign keys for the first table.
I need this in an sql statement as well preferably, like:
$sql = 'SELECT...'
Would this work?
$sql = "SELECT * FROM order_products
UNION
SELECT * FROM products"
If so, how do i know which row is which?
My desired output is all entries, now looking like this:
ProductID | Quantity | Productname | Desc | Price | Stock | Image
You need a classical JOIN clause:
SELECT *
FROM products
LEFT JOIN order_products on products.ProductId = order_products.ProductId
Not sure exactly what's the output you wish to get is but you should use something like this
SELECT o.ProductID, o.Quantity, p.Price, o.Quantity * p.Price
FROM order_product o
LEFT JOIN products p ON o.ProductID = p.ProductID
My problem is i'm trying to show a checkout page which shows
everything in a list, but how do i combine the two tables?
You need to simply use mysql JOIN to show cart items.
Your table data should be like as i added in demo.
See SQL Fiddle Demo
SELECT
o.id 'orderId',
o.ProductID 'pid',
SUM(o.Quantity) 'qty',
p.productname 'product',
p.`Price` 'price'
FROM
order_product o
INNER JOIN products p
ON p.`ProductID` = o.`ProductID`
Edit Required output
My desired output is all entries, now looking like this:
ProductID | Quantity | Productname | Desc | Price | Stock | Image
Modify above query's SELECT part SELECT tablename.your_column .....
Use inner join.
Select * from order_products op INNER JOIN products p ON p.ProductID = op.ProductID;
w.r.t. your need, the above query needs to be modified to:-
"SELECT *,
SUM(op.quantity * p.price) AS grandTotal
FROM order_products op
INNER JOIN PRODUCT p ON p.ProductID = op.ProductID WHERE p.ProductID =".$prodId(your php variable of product id);
select table1.ProductID
, table1.Quantity
, table2.Productname
, table2.Desc
, table2.Price
, table2.Stock
, table2.Image
FROM table1
JOIN table2 ON table1.productid=table2.productid
Related
assuming we have a database category structure like this
# Categories Table
| category_id | parent_category_id | status |
# Products Table
| product_id | category_id |
how do i get the list of products in a category only if the parent_category_id has an active status = 1 ?
i guess i could use some sub-query in the SELECT statement, but i don't know how! :(
something like:
SELECT p.*, (SELECT * FROM ? WHERE ? ) AS x
FROM products AS p
LEFT JOIN categories AS c ON p.category_id = c.category_id
WHERE ...
AND p.product_id = '?'
AND ...
Thank you in advance for any advice!
NB: i'm using PHP as backend language, just in case i need some sort
of data manipulation to pass to the mysql query.
try not to use subquery, because it can affect the speed of loading data
maybe it can help
SELECT *
FROM categories c
INNER JOIN products p ON p.category_id=c.category_id
WHERE c.status = 1
Here a example query, I don't use left join or join, becasue you require that the record exists in order to get the status.
SELECT
*
FROM
products, categories
WHERE
products.category_id = categories.category_id
AND status = '1'
SELECT p.* FROM products p
INNER JOIN categories child ON child.category_id = p.category_id
INNER JOIN categories parent ON parent.category_id = child.parent_category_id
WHERE parent.status=1
First you have to get all active categories
SELECT child.*
FROM Categories child
JOIN Categories parent
ON child.parent_category_id = parent.category_id
AND parent.status = 1
But you also need the root categories with not parent_id
SELECT *
FROM Categories
WHERE parent_category_id IS NULL
AND status = 1
So you UNION both
SELECT *
FROM Categories child
JOIN Categories parent
ON child.parent_category_id = parent.category_id
AND parent.status = 1
UNION ALL
SELECT *
FROM Categories
WHERE parent_category_id IS NULL
AND status = 1
Now you get the products for those categories
SELECT *
FROM ( ... UNION ALL ... ) active_categories
JOIN Products
ON active_categories.category_id = Product.category_id
I have three tables:
products:
id name
1 juice
2 chips
3 water
orders:
id product_id order_id
1 1 special1
2 3 special1
3 2 special1
4 1 special2
5 2 special2
final_orders:
id order_id date
1 special1 25-3-2017
2 special2 25-3-2017
I want to select all products names in every order using order_id to show:
ID: Special1
Date: 25-3-2017
Products List:
juice
water
chips
ID: Special2
Date: 25-3-2017
Products List:
juice
chips
I use this:
$sql = "select * from products,orders where products.id = orders.product_id";
but it doesn't work and show me duplicated results.
thank you.
You need to join with final_orders as well:
SELECT *
FROM final_orders AS f
JOIN orders AS o ON f.order_id = o.order_id
JOIN products AS p ON p.id = o.product_id
ORDER BY f.order_id
To prevent duplication in the output, your loop that prints the output should only show the information from final_orders when it changes. See How can i list has same id data with while loop in PHP?
If you want to see one final order per record in your result set, then you will have to aggregate the products which appear in each order. One option then is the following query which aggregates order products into CSV using MySQL's GROUP_CONCAT():
SELECT t1.order_id,
t1.date,
t2.products
FROM final_orders t1
INNER JOIN
(
SELECT a.order_id, GROUP_CONCAT(b.name) AS products
FROM orders a
INNER JOIN products b
ON a.product_id = b.id
GROUP BY a.order_id
) t2
ON t1.order_id = t2.order_id
Demo here:
Rextester
First table: Product
id | Name | Price | Discount
-------------------------------
1 xyz 200
2 xyz 250
3 yz 100 50
Second table : buy
id | userid | Product_id Card_details
------------------------------------------
1 1 1 55555
2 1 2 88888
3 3 1 77777
Now i have $user_id in my php variable. If user id is 3 i want following output:
id Name Price Discount user_id Product_id Card_details
3 yz 100 50 3 1 77777
How can i achieve this.
Use JOIN or where
SELECT * FROM Product p JOIN buy b
ON p.id = b.Product_id
WHERE b.userid ='3'
SELECT * FROM Product JOIN buy ON Product.id = buy.id WHERE buy.userid = $user_id;
Try the following query
$query = "SELECT Product.id, Product.Name, Product.Price, Product.Discount,
buy.userid , buy.Product_id, buy.Card_details
FROM Product
JOIN buy ON Product.id = buy.Product_id
WHERE buy.userid = 3";
First in your Product table xyz is to time you have to use unique name for your product for better understanding.
Now in Product table id is your product id. Check your Buys tables productid there is no entry for product id 3. When you want to buy product 3 for user id 3 then buy table productid entry should be three for userid 3.
Now check the below query it will definitely work for you.
SELECT * FROM Product
INNER JOIN buy ON Product.id = buy.Product_id
WHERE buy.userid = '3';
It means you want to get product buy by userid 3.
Try This
DECLARE #USER_ID INT=3 --I/P of USER_ID
SELECT P.ID,
P.NAME,
P.PRICE,
P.Discount,
B.user_id,
B.PRODUCT_ID,
B.CARD_DETAILS
FROM #PRODUCT P
JOIN #BUY B
ON P.ID = B.ID
WHERE B.USER_ID = #USER_ID
i am using a category , product relation to find data. I am in situation like i have a category table and product table with categoryid and product type. No suppose i have a particular type and i want to find those category which a product of a specific type does not have.
Table struture are like
Category Table
____________
|Id|Category|
| 1| X |
_____________
Product Table
____________________________
|ID | Product|Category| Type|
| 1 | Y | 1 | 2 |
_____________________________
I can find it by using a sub query like
SELECT *
FROM category
WHERE id NOT IN(SELECT category
FROM product
WHERE type = 2);
Is there a way to get it by another way
I also use a JOIN like
SELECT *
FROM category AS c
JOIN products AS p
ON c.id <> p.category
WHERE p.type = 2
Why does this not give the appropriate result.
Use LEFT JOIN and use IS NULL predicate:
SELECT *
FROM category AS c
LEFT JOIN products AS p
ON c.id = p.category
AND p.type = 2
WHERE p.category IS NULL;
The unmatched rows from the second table will have null values in the category field, the WHERE p.category IS NULL will filter and give you them
I got a skill test for a quick mysql query. I am given the tables:
Orders OrderItems
----------------------------------------
id id
date order_id(Orders.id)
shipping_amount product_id
order_status price
customer_id quantity
I need to show Orders id and totals (including shipping amount) and records prior to June 1, 2003.
output should be...
| OrderID | OrderTotal |
+-----------+------------+
| 13230 | $55.00 |
| 54455 | $40.00 |
| 59694 | $33.04 |
| 39495 | $21.05 |
The hint is to use Group by or subselect. I have the following statement so far but not sure what to do next.
Select id AS OrderId,***(not sure what to do here)
from Orders join OrderItems on Orders.id=OrderItems.id
I don't have access to a mysql database to test, but I would imagine it looks something like this:
select
o.id OrderID,
(select sum(oi.price * oi.quantity) from order_items oi where oi.order_id = o.id) + o.shipping_amount OrderTotal
from
orders o
where
o.date < str_to_date('2003-06-01', '%Y-%m-%d');
I'd say it must look like this:
SELECT O.ID as OrderID, SUM(OI.price * OI.quantity) AS OrderTotal
FROM Orders O
INNER JOIN OrderItems OI ON O.id = OI.order_Id
WHERE date < '2003-06-01'
GROUP BY O.id
ORDER BY SUM(price * quantity) DESC
Not sure how to format dates in MySQL though or if you can order by an agregate function in this version but I'm pretty sure this is a good start.