MYSQL LEFTJOIN multiple ids same table - mysql

Im trying to get a combination of ids from a table like this:
Table activation:
user_id product_id reseller_id range_id Name
-------------------------------------------------
1 1 5 2 Oscar
1 1 5 3 Luis
2 1 5 4 Julian
Table prices (compType_id = reseller_id):
product_id compType_id price range_id
------------------------------------------------
1 5 38.60 2
1 5 48.60 3
1 5 58.60 4
Table users:
user_id name
----------------
1 lloyd
2 Mark
I want to select the activation Name and the price of prices based on the user_id.
How can i do that?
I tried something like this:
SELECT a.name
(SELECT price FROM prices WHERE product_id = 1 AND range_id =
AND compType_id = 5) AS price
FROM activation AS a
LEFT JOIN users AS u ON u.user_id = a.u_id
WHERE u.user_id = 1
The price columns have the same value in each row.
name price
Oscar 21.30
Luis 21.30
How can i change it to show the correct price?

I solved it with something like this:
SELECT a.company, a.name, a.email, a.phone, a.ruc, a.code, a.active,
a.numlic, (SELECT p.price FROM prices AS p WHERE p.product_id =
a.product_id AND p.range_id = a.range_id AND p.compType_id = c.type_id)
AS price
FROM activation AS a
LEFT JOIN users AS u ON u.user_id = a.u_id
LEFT JOIN companies AS c ON a.reseller_id = c.id
WHERE u.user_id = 1

Related

how to get max rows from 2 transactional tables. MySQL

I have two mysql transactional tables and and two lookup tables. I want to select max(id) from each of the transactional tables, combine the results with lookup tables and combine into one row. I seem unable to find solutions so far. Here is my tables. Stocks and Prices are transactional while Vehicle and Models are lookup tables.
Vehicles table
id name
1 Toyota
2 Suzuki
Models table
id vehicle_id name
1 1 Corolla
2 2 Swift
3 1 Prado
4 2 Vitara
Stocks table
id vehicle_id model_id qty
1 1 1 50
2 2 2 77
3 1 1 40
4 2 2 30
Prices table
id vehicle_id model_id price
1 1 1 500
2 2 2 777
3 1 1 600
4 2 2 1000
Expected results
id vehicle_id model_id qty price vname mname
1 1 1 40 600 Toyota Corolla
2 2 2 30 1000 Suzuki Swift
Here is what I've tried among countless trials
select s.*, b.name vehicle, m.name model, p.price
from stocks s, vehicles b, models m, prices p
where s.id in (select max(id) id from stocks
where s.vehicle_id = b.id and s.model_id = m.id and s.vehicle_id = p.vehicle_id and s.model_id = p.model_id
group by vehicle_id, model_id)
order by id;
Running the above query doesn't give me what I want and it crushes the PC. I have to restart. How can I achieve the expected outcome?
If you are using MySQL 8 you can use window functions and common table expressions for latest(based on maximum id per vehicle and model group) prices and qty for vehicle and models
with pricescte as (select *,
rank() over (partition by vehicle_id,model_id order by id desc) AS price_rank
from prices),
stockcte as (select *,
rank() over (partition by vehicle_id,model_id order by id desc) AS stock_rank
from stocks)
select v.id,
v.name,
m.id as model_id,
m.name,
s.qty,
p.price
from vehicles v
join models m on v.id = m.vehicle_id
join stockcte s on v.id = s.vehicle_id
and m.id = s.model_id
join pricescte p on v.id = p.vehicle_id
and m.id = p.model_id
where s.stock_rank = 1
and p.price_rank = 1
DEMO
If you are not on latest version of MySQL < 8 you could use a query like
select v.id,
v.name,
m.id as model_id,
m.name,
s.qty,
p.price
from vehicles v
join models m on v.id = m.vehicle_id
join (
select *
from stocks st
where id = (
select max(id)
from stocks
where st.vehicle_id =vehicle_id
and st.model_id = model_id
)
) s
on v.id = s.vehicle_id
and m.id = s.model_id
join (
select *
from prices pr
where id = (
select max(id)
from prices
where pr.vehicle_id =vehicle_id
and pr.model_id = model_id
)
) p on v.id = p.vehicle_id
and m.id = p.model_id
DEMO

SQL query in Mysql

I am using php and Mysql and need help to display UNIQUE results for a price comparison.
My tables are:
tblItems
ID | SKU | Shop | Name | Size
1 S1 Shop1 A S
2 S2 Shop1 B M
3 S3 Shop1 C L
4 S4 Shop2 A S
tblProductFeed
ID | SKU | Shop | Price
1 S1 Shop1 12
2 S2 Shop1 14
3 S3 Shop1 15
4 S4 Shop2 11
The idea is a price comparison.
I want to compare all records where Size='S'. But I only want to get the lowest price from tblProductFeed, and I only want one record where the price is lowest.
My thoughts are like:
Get SKU, Shop and Name from tblItems where Size='S'
Compare matching rows in tblProductFeed where SKU and Shop are matching
Get the lowest price from tblProductFeed
Display only one DISTINCT Name from TblItems and with the lowest price
Results:
Name | Price
A 11
B 14
C 15
Maybe my idea is bad, please correct me.
Before I decided to scrap data and build to tables my sql was like this, might help to get some inspiration.
SELECT tbl.*
FROM tblProductFeed tbl
INNER JOIN (SELECT Size
, MIN(Price) MinPrice
FROM tblProductFeed
WHERE Size = '". $str ."'
GROUP BY Size) tbl1
ON tbl1.Size = tbl.Size
WHERE tbl1.MinPrice = tbl.Price;
try this : to retrieve lowest price for data with size 'S'
select a.Name, a.Shop, b.Price as minprice from tblItems as a
inner join tblProductFeed as b on
a.id = b.id and
a.SKU = b.SKU and
a.Shop = b.shop
where a.size = 'S' and b.price in (select min(price) from tblProductFeed)
Result :
Or if you want to get all item with lowest price try this :
select name,min(price)minprice from
(select a.id, a.SKU, b.shop, a.name, a.size, b.Price from tblItems as a
inner join tblProductFeed as b on
a.id = b.id and
a.SKU = b.SKU and
a.Shop = b.shop
where b.price in (select min(price) from tblProductFeed group by price)) a
group by name
Result :

Mysql join 3 tables query

I have 3 tables like following:
branch
id name
---------
1 abc
2 xyz
users
id branch_id name
-----------------
1 1 aa
2 1 bb
3 2 cc
4 1 dd
5 2 ee
sales
id user_id product price
1 1 xxxx 10
2 1 yyyy 20
3 2 zzzz 18
4 3 aaaa 12
5 2 bbbb 10
6 4 cccc 20
Now I want to get the total selling amount branch wise like:
branch_id total_price
---------------------
1 78
2 12
For that i write a sql query like:
SELECT SUM(s.price) , b.id
FROM sales s
JOIN branch b
GROUP BY id
HAVING s.user_id
IN (
SELECT id
FROM users
WHERE branch_id = b.id
)
But this does not provide the answer that I want. Please help me.
I think this should do the trick:
SELECT branch.id AS branch_id, SUM(s.price) AS total_price
FROM branch
JOIN users ON branch.id = users.branch_id
JOIN sales ON users.id = sales.user_id
GROUP BY branch.id;
Also you could use INNER JOIN instead of JOIN(Both are doing the same thing). With INNER JOIN it is possibly easier to read, especially your query contains other types of JOIN's like LEFT JOIN or RIGHT JOIN
Hope that helps!
You could use something like this:
SELECT u.branch_id, SUM(s.price) AS total_price
FROM sales AS s INNER JOIN users u ON s.user_id = user.id
GROUP BY u.branch_id
ORDER BY u.branch_id

How to get the last record using group by

My Query :-
SELECT
p.*,
b.brand_name
FROM
portfolio p,
branding_category b
WHERE
p.category = 'BRANDING'
AND
p.brand_category = b.id
AND
is_active = '1'
GROUP BY
p.brand_category
ORDER BY p.id DESC
LIMIT 10
Suppose portfolio table has :-
id category brand_category is_active title
1 test 8 1 abc
2 test 7 1 pqr
3 test 8 1 xyz
4 test 7 1 ijk
And I want to show Output has :- That is, the last record in each group should be returned.
id category brand_name is_active title
3 test Catalogs 1 xyz
4 test Posters 1 ijk
Edit :-
branding_category
id brand_name
8 Catalogs
7 Posters
i.e, Last row for each group. Please help me on this. I know it is there in stackoverflow Retrieving the last record in each group but I am not able to write for two table.
Try this :-
SELECT p.*,b.brand_name
FROM portfolio p
INNER JOIN branding_category b ON p.brand_category = b.id
INNER JOIN (
SELECT MAX(id) MaxMsgIDForThread
FROM portfolio
WHERE is_active = '1'
GROUP BY brand_category
) g ON p.id = g.MaxMsgIDForThread
Order by p.id Desc
LIMIT 10
Try this,
select x.id,x.category,x.brand_name,x.is_active,x.title from (
select
p.id,p.category,pc.brand_name,p.is_active,p.title,
ROW_NUMBER()over ( ORDER BY p.id) as Rnk
from portfolio p inner join branding_category pc
on p.brand_category=pc.id
) x where Rnk >2

mySQL - GROUP BY but get the most recent row

I've got a budget table:
user_id product_id budget created
-----------------------------------------------------------------
1 1 300 2011-12-01
2 1 400 2011-12-01
1 1 500 2011-12-03
2 2 400 2011-12-04
I've also got a manager_user table, joining a manager with the user
user_id manager_id product_id
------------------------------------
1 5 1
1 9 2
2 5 1
2 5 2
3 5 1
What I'd like to do is grab each of the user that's assigned to Manager #5, and also get their 'budgets'... but only the most recent one.
Right now my statement looks like this:
SELECT * FROM manager_user mu
LEFT JOIN budget b
ON b.user_id = mu.user_id AND b.product_id = mu.product_id
WHERE mu.manager_id = 5
GROUP BY mu.user_id, mu.product_id
ORDER BY b.created DESC;
The problem is it doesn't pull the most recent budget. Any suggestions? Thanks!
To accomplish your task you can do as follows:
select b1.user_id,
b1.budget
from budget b1 inner join (
select b.user_id,
b.product_id,
max(created) lastdate
from budget b
group by b.user_id, b.product_id ) q
on b1.user_id=q.user_id and
b1.product_id=q.product_id and
b1.created=q.lastdate
where b1.user_id in
(select user_id from manager_user where manager_id = 5);
I'm assuming here that your (user_id, product_id, created) combination is unique.
For what it's worth, here's the code that returned what I was looking for:
SELECT DISTINCT(b1.id),mu.user_id,mu.product_id,b1.budget,b1.created
FROM budget b1
INNER JOIN (
SELECT b.user_id, b.product_id, MAX(created) lastdate
FROM budget b
GROUP BY b.user_id, b.product_id) q
ON b1.user_id=q.user_id AND
b1.product_id=q.product_id AND
b1.created=q.lastdate
RIGHT JOIN manager_user mu
ON mu.user_id = b1.user_id AND
mu.product_id = b1.product_id
WHERE mu.manager_id = 5;
Thanks for the help Andrea!