MYSQL - How can I get this left join to work? - mysql

SELECT
*
FROM
food_types ft
LEFT JOIN
member_has_food_types mhft ON (ft.food_id = mhft.food_id)
WHERE mhft.member_id = 3230
Okay, so here are the database tables
Food Types:
FOOD_ID | FOOD_TYPE | STATE
1 APPLE 1
2 ORANGE 1
3 BANANA 1
4 CAKE 1
5 BACON 1
member_has_food_types:
FOOD_ID | MEMBER_ID
1 3230
2 3230
4 3230
5 3230
So because member_has_food_types does not contain a number 3 food, then the report comes back like:
FOOD_ID | FOOD_TYPE | STATE | FOOD_ID | MEMBER_ID
1 APPLE 1 1 3230
2 ORANGE 1 2 3230
4 CAKE 1 4 3230
5 BACON 1 5 3230
But I want it to include a NULL like this
FOOD_ID | FOOD_TYPE | STATE | FOOD_ID | MEMBER_ID
1 APPLE 1 1 3230
2 ORANGE 1 2 3230
3 BANANA 1 NULL NULL
4 CAKE 1 4 3230
5 BACON 1 5 3230
My understanding is its not showing a NULL for those fields, because im joining on food_id. So is there any clever way I can get around this with a sub query??

move the filter to the join
SELECT * FROM food_types ft
LEFT JOIN member_has_food_types mhft ON ft.food_id = mhft.food_id
AND mhft.member_id = 3230

Related

Mysql Database: Joins sums and group by

I have two tables OrderUpdate and DailyProduction
ORDER_UPDATE
itemid packages ordercode
1 10 ORANGE
2 20 ORANGE
3 40 ORANGE
1 25 APPLE
2 50 APPLE
3 100 APPLE
DAILY_PRODUCTION
date itemid packages ordercode
28-Sep 1 5 ORANGE
20-Sep 1 2 ORANGE
1-Sep 3 4 ORANGE
1-Sep 1 5 APPLE
15-Sep 2 5 APPLE
30-Sep 3 1 APPLE
And I want following as a result
itemid packages ordercode ready remaining
1 10 ORANGE 7 3
2 20 ORANGE 0 20
3 40 ORANGE 4 36
1 25 APPLE 5 20
2 50 APPLE 5 45
3 100 APPLE 1 99
If I follow you correctly, you can left join table order_update with an aggregate query that sums the packages for each (itemid, ordercode) in table daily_production:
select ou.*, coalesce(dp.ready, 0), ou.packages - coalesce(dp.ready, 0) reamining
from order_update ou
left join (
select itemid, ordercode, sum(packages) ready
from daily_production
group by itemid, ordercode
) dp on dp.itemid = ou.itemid and dp.ordercode = ou.ordercode

How do you join two tables but include all rows even if blank or null?

I have two tables. I want to combine them but include rows that don't return a value. This question is probably going to be flagged as duplicate or something, but I have already tried reading the other posts and still failed. So I might be below the average MySQL Programmer. Hope somebody can help.
table_price_list
item_id price_type_id price_amount
1 1 100.00
1 2 95.00
1 3 90.00
1 4 85.00
1 5 80.00
1 6 75.00
2 1 201.56
2 2 196.45
2 3 191.78
2 4 186.36
3 1 1210.12
3 2 1205.45
3 3 1200.69
3 4 1195.48
3 5 1190.98
table_price_type
price_type_id price_type
1 srp
2 reseller
3 distributor
4 mega
5 depot
6 special
Desired output
item_id price_type_id price_type
1 srp 100.00
1 reseller 95.00
1 distributor 90.00
1 mega 85.00
1 depot 80.00
1 special 75.00
2 srp 201.56
2 reseller 196.45
2 distributor 191.78
2 mega 186.36
2 depot null
2 special null
3 srp 1210.12
3 reseller 1205.45
3 distributor 1200.69
3 mega 1195.48
3 depot 1190.98
3 special null
The best I could get so far is this, this leaves out the blank price_type
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left outer join table_price_list B on A.price_type_id=B.price_type_id
It doesn't have to be null, it could just be blank (' ').
You can use left join to get required result.
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left join table_price_list B on A.price_type_id=B.price_type_id

Select latest data per group from joined tables

I have two tables like this:
survey:
survey_id | store_code | timestamp
product_stock:
survey_id | product_code | production_month | value
How can I get latest value, based on survey timestamp and grouped by store_code, product_code, and production_month?
for example if I have
survey_id | store_code | timestamp
1 store_1 2015-04-20
2 store_1 2015-04-22
3 store_2 2015-04-21
4 store_2 2015-04-22
survey_id | product_code | production_month | value
1 product_1 2 15
2 product_1 2 10
1 product_1 3 20
1 product_2 2 12
3 product_2 2 23
4 product_2 2 17
It'd return result like this
survey_id | store_code | time_stamp | product_code | production_month | value
2 store_1 2015-04-22 product_1 2 10
1 store_1 2015-04-20 product_1 3 20
1 store_1 2015-04-20 product_2 2 12
4 store_2 2015-04-22 product_2 2 17
and it needs to be as fast as possible, seeing the database is quite large in size
UPDATED - please run query again
Here is my answer:
SELECT survey.survey_id, survey.store_code, survey.timestamp, product_stock.survey_id, product_stock.product_code, product_stock.production_month, product_stock.value
FROM survey
INNER JOIN product_stock
ON survey.survey_id = product_stock.survey_id
WHERE survey.timestamp = (SELECT MAX(timestamp)
FROM survey)
GROUP BY survey.store_code,product_stock.product_code,product_stock.production_month;

mysql join get info from 2 rows

i have 2 tables:
players
id | dni | name
-----------------
1 222 mike
2 333 gerard
3 444 mark
4 555 alfred
5 666 thomas
5 777 nicolas
teams
id | dni1 | dni2 | cat
------------------------
1 222 333 1
2 444 555 1
3 666 333 2
4 777 222 2
what i want is to make a select statement depending on the cat. so for
cat=1 should be:
mike gerard
mark alfred
and for cat=2 should be:
thomas gerard
nicolas mike
i tryed many join statements but i cant figure it out, can you help me please?
EDIT to make it more difficult, the players table have a new colum= points
players
id | dni | name | points
--------------------------
1 222 mike 1
2 333 gerard 2
3 444 mark 3
4 555 alfred 4
5 666 thomas 5
5 777 nicolas 6
now the result give us the sum of the points colums from both players and order by biggest sum number.
cat=1 should be:
mark alfred 7
mike gerard 3
select t.cat, p1.name name1, p2.name name2, p1.points + p2.points points
from teams AS t
join players AS p1 ON p1.dni = t.dni1
join players AS p2 on p2.dni = t.dni2
where cat = 1
order by points desc
DEMO

SQL query to show zero instead of not shown record

I have these two tables:
Reception
id reception_date tin_lot company_id client_id quantity weight
1 2013-12-03 00:00:00 1 1 1 10 1980.00
2 2013-12-03 00:00:00 2 1 1 1 150.00
3 2013-12-13 00:00:00 3 1 2 10 4500.00
4 2013-12-13 00:00:00 4 2 5 5 2300.00
Payment
id payment_date amount reception_id
1 2013-12-03 00:00:00 500.0 1
2 2013-12-03 00:00:00 1200.0 3
The result I want to obtain is the following:
Expected result
id reception_date tin_lot client_id weight payment_made
1 2013-12-03 00:00:00 1 1 1980.00 500.0
2 2013-12-03 00:00:00 2 1 150.00 0.0
3 2013-12-13 00:00:00 3 2 4500.00 1200.0
4 2013-12-13 00:00:00 4 5 2300.00 0.0
I'm trying this query:
select rec.id
rec.reception_date,
rec.tin_lot,
rec.client_id,
rec.weight,
pay.payment_made
from liquidation.reception rec, liquidation.payment pay
where pay.recepcion_id=rec.id
But it doesn't list the receptions with no payment.
Please help me. Thanks in advance.
you need to Left Join the payment table:
from liquidation.reception rec
left join liquidation.payment pay on ( pay.recepcion_id=rec.id)
That is because you need to learn to use left outer join and proper join syntax. Just don't use a comma in a from clause any more.
Here is the query you want:
select rec.id, rec.reception_date, rec.tin_lot, rec.client_id, rec.weight,
coalesce(pay.payment_made, 0) as payment_made
from liquidation.reception rec left outer join
liquidation.payment pay
on pay.recepcion_id = rec.id;