Cannot get data from two tables using join - php

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

Related

php - mysql join three tables and grouping

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

MySQL Query to get Data of Specific Item by All Months

Okay I have a table 3 Tables (Orders, Order_details, Products)
Orders
id | order_date
1 | March-01-13
2 | March-02-13
3 | April-01-13
4 | May-01-13
5 | June-01-13
and so on.....
Order_Details
id | order_id | product_id | total_price
1 1 1 100 /*mouse*/
2 1 3 200 /*monitor*/
3 2 2 50 /*keyboar*/
4 2 3 200 /*monitor*/
and so on....
Products
id | title
1 mouse
2 keyboard
3 monitor
and so on...
And here is my SQL Query that giving unexpected output
SELECT title, SUM(total_price) as total_price, orders.order_date as date
FROM products
LEFT JOIN order_details
ON order_details.product_id=products.id
LEFT JOIN orders
ON orders.id=order_details.order_id
WHERE title='monitor' /*this is just a try, prod_id should be the one*/
GROUP BY MONTH(date)
ORDER BY total_price DESC LIMIT 10
What I want to get is the total_price of product_id in all months
Expected Output
title | total_price | date
monitor 400 March
When using a GROUP BY, all of the fields in the SELECT list need to either be aggregated, or be part of the GROUP BY. So try adding the 'title' to the group by, and adding the MONTH function to the order_date in the SELECT list as follows:
SELECT title, SUM(total_price) as total_price, MONTH(orders.order_date) as date
FROM products
LEFT JOIN order_details ON order_details.product_id=products.id
LEFT JOIN orders ON orders.id=order_details.order_id
WHERE title='monitor'
GROUP BY title, date
ORDER BY total_price DESC LIMIT 10
This will get the total_price for each product for each month.
Do you want something like this?
SELECT p.title, SUM(total_price) as total_price
FROM products p LEFT JOIN
order_details od
ON od.product_id = p.id LEFT JOIN
orders o
ON o.id = od.order_id
GROUP BY p.title
ORDER BY total_price DESC
LIMIT 10;
I don't see why your sample output would have a month in it.

MQL, PHP select all from two tables?

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

Getting count from inner join table

I have tables as described below:
subscription_plans (Table for storing all plans)
id plan days_limit added_on status rate
------------------------------------------------
1 PlanA 15 1398249706 1 150.00
2 PlanB 15 1398249706 1 150.00
subscribed_videos (Table for storing details of video in each plans)
id plan_id videoid
----------------------
1 1 1
2 2 2
subscription_groups (Table for storing groups where a plan can be part of another plan. ie, Plan A be a plan with 2 other individual plans, Plan B and C )
id plan_id assosiated_plan_id added_on
----------------------------------------------
1 1 2 1398249706
usersubscription (Table for storing user subscribed plans)
id user_id plan_id subscribed_on
---------------------------------------
1 1 1 1398771106
Now, my problem is that how can I get the count of videos for each plans. If Plan A contains both Plan B and C (subscription_groups table), then the count should return the total video count for each individual plans in that particular plan. Now I have done with a query which will return plan details along with count of videos for a plan but I am not able to join it with subscription_groups. How can I accomplish this in a single query.
$data['planquery']=$this->db->query("select
us.plan_id,us.subscribed_on,sp.plan,sp.days_limit,sp.rate,count(sv.videoid) from
usersubscription as us INNER JOIN
subscription_plans as sp ON us.plan_id=sp.id INNER JOIN subscribed_videos as sv ON sp.id=sv.plan_id where sp.status=1 and us.user_id=1");
Expected Result:
plan_id subscribed_on plan days_limit rate count
-------------------------------------------------------
1 1398771106 PlanA 15 150.00 2
Can anyone help me to find a solution for this?
Thanks in advance.
You can do so
SELECT
us.plan_id,
us.subscribed_on,
sp.plan,
sp.days_limit,
sp.rate,
COUNT(sv.videoid)
FROM
usersubscription AS us
RIGHT JOIN subscription_plans AS sp
ON us.plan_id = sp.id
INNER JOIN subscribed_videos AS sv
ON sp.id = sv.plan_id
INNER JOIN subscription_groups g
ON(g.plan_id =sv .plan_id OR sv.plan_id= g.assosiated_plan_id)
WHERE sp.status = 1
AND (us.user_id = 1 OR us.user_id IS NULL )
Demo
Since user has only plan associated but the associated plan can also has another plan linked so the last condition will check the user id but for is null to for the second linked plan user id will be null due to right join on subscription_plans
Edit
SELECT
u.plan_id,
u.subscribed_on,
p.plan,
p.days_limit,
p.rate
,COUNT(DISTINCT v.`videoid`)
FROM `usersubscription` u
JOIN `subscription_groups` g
ON (u.`plan_id` = g.`plan_id`)
RIGHT JOIN `subscription_plans` p
ON(u.`plan_id` = p.`id` OR g.`assosiated_plan_id` = p.`id`)
INNER JOIN `subscribed_videos` v ON(v.`plan_id`=g.`assosiated_plan_id` OR u.`plan_id`= v.`plan_id`)
WHERE u.`id`=1 AND p.`status` = 1
Demo 1 Demo2
For video ids you can use group_concat
SELECT
u.plan_id,
u.subscribed_on,
p.plan,
p.days_limit,
p.rate
,COUNT(DISTINCT v.`videoid`) `video_count` ,
GROUP_CONCAT(DISTINCT v.`videoid`) `video_ids`
FROM `usersubscription` u
JOIN `subscription_groups` g
ON (u.`plan_id` = g.`plan_id`)
RIGHT JOIN `subscription_plans` p
ON(u.`plan_id` = p.`id` OR g.`assosiated_plan_id` = p.`id`)
INNER JOIN `subscribed_videos` v ON(v.`plan_id`=g.`assosiated_plan_id` OR u.`plan_id`= v.`plan_id`)
WHERE u.`id`=1 AND p.`status` = 1
Demo 1a Demo 2a

Select unique product id and order by stock id desc

I'm trying to get unique product from stock...
stock table:
id - product - quantity
1 2 0
2 3 5
3 2 19
4 4 3
5 2 8
result
id - product - quantity
5 2 8
4 4 3
2 3 5
it's working with
SELECT max(id) as id,
product
FROM stock
GROUP by product
ORDER by id DESC
but I can't get last quantity of product
with this query I get:
id - product - quantity
1 2 0
2 3 5
4 4 3
I need latest quantity of the product.
You can wrap your existing query in a subquery and join that on the table itself so you can get the other columns of the same row.
SELECT a.*
FROM stock a
INNER JOIN
(
SELECT product, MAX(ID) id
FROM stock
GROUP BY product
) b ON a.product = b.product
AND a.ID = b.ID
ORDER BY a.id DESC
Assuming your definition of "latest" is max(id), I think the easiest way is:
SELECT s.id, s.product, s.quantity
FROM stock s
WHERE NOT EXISTS (SELECT 1 FROM stock s2 WHERE s2.product = s.product and s2.id > s.id);
Basically give me the stock rows where there is no row for the same product with a greater id.
You can do it with a left join of the table with itself that filters only rows without one with higher id and same product, avoiding subquery and group by, which can be very expensive on large tables:
select p1.id, p1.product, p1.quantity from stock as p1
left join test as p2 on p1.product = p2.product and p2.id> p1.id
where p2.id is null
order by p1.id desc;

Categories