Getting NULL in a SQL left JOIN as result - mysql

create or replace view vw_fill_thresholds as
SELECT
fill_thresholds.id,
fill_thresholds.tenant_id,
waypoint_locations.name,
material_type.description as material,
fill_thresholds.can_size,
fill_thresholds.threshold,
units.description as unit,
fill_thresholds.post_threshold_cost as ptc,
ptu_units.description as ptu_unit
FROM fill_thresholds
left join fill_locations on fill_thresholds.fill_id = fill_locations.id
and fill_thresholds.deleted = 0
left join units on fill_locations.unit_id = units.unit_id
left join waypoint_locations on `waypoint_locations`.`id` = `fill_locations`.`waypoint_id`
left join `material_type` on `material_type`.`id` = `fill_locations`.`material`
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = units.unit_id
I need to select the same column as units. So, In my fill_thresholds table I have two fields that contain an id from the units table: example 2 and 3.
Now, in my units table I have the following fields: id and description. example data: 1 km, 2 miles, 3 yards ,etc. so in my view according to the numbers I have in fill_thresholds it should show miles and yards. Right now I'm getting NULLs which I don't understand why.

Your last join should be as per below-
left join `units` `ptu_units` on fill_thresholds.post_threshold_unit_id = ptu_units.unit_id
Right now you are joining your units table based on 2 columns first with fill_locations.unit_id = units.unit_id and 2nd with fill_thresholds.post_threshold_unit_id = units.unit_id, so not getting corresponding rows ans showing null.

The problem with your query is that you're trying to guess what is wrong analysing the whole query. It is too complicated so try to join your tables one by one.
1)fill_thresholds LEFT JOIN fill_locations
2)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units
3)fill_thresholds LEFT JOIN fill_locations LEFT JOIN units LEFT JOIN waypoint_locations
and so on
This way you can find where is the problem

Related

MySql query Get all rows from left table and common from right table

Table 1 - customer_kitchen_service_plans and data https://prnt.sc/00_ip7uWiQuq
Table 2 - kitchen_service_plans_linking and data https://prnt.sc/e_GW64THTCFK
Above two are the tables and i want to join in such a way that it should return All the rows from Table 1 and Common rows from Table 2
Join using column kitchen_service_plan_id from Table 1 and kitchen_service_plan_parent_id from Table 2
Current query is as below
select * from `customer_kitchen_service_plans` as `cksp`
left join `kitchen_service_plans_linking` as `kspl` on
`kspl`.`kitchen_service_plan_parent_id` =
`cksp`.`kitchen_service_plan_id`
where `cksp`.`status` = 'ACTIVE' and `cksp`.`customer_id` = 2
You want a left outer join which returns all rows from the first table and matching rows from the right.
select * from `customer_kitchen_service_plans` as cksp
left outer join `kitchen_service_plans_linking` as kspl on
kspl.`kitchen_service_plan_parent_id` =
cksp.`kitchen_service_plan_id`
where cksp.`status` = 'ACTIVE' and cksp.`customer_id` = 2
Here's a discussion on Left Outer Join in MySQL
See if that's help
SELECT * FROM customer_kitchen_service_plans
LEFT JOIN kitchen_service_plans_linking ON
customer_kitchen_service_plans.kitchen_service_plan_id=
kitchen_service_plans_linking.kitchen_service_plan_parent_id;

MySql - having issues with double left join

I am having issues with getting this double left join to get the listingspecificsListPrice, but that info exists in the table, cant figure out why it would not include it. This is my sql.
SELECT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID AND mls_images.imgOrder = 0
WHERE userID = 413
GROUP BY mls_subject_property.mls_listingID
The result comes out like this..
All of the other fields come back, but it doesnt seem to want to bring back those two items.
This is a picture of the other table, to show that the data does in fact exist.
The mls_images.imgOrder = 0 condition should be in the join with mls_images, not mls_forms_listing_specifics.
Don't use GROUP BY if you're not using any aggregation functions. Use SELECT DISTINCT to prevent duplicates.
SELECT DISTINCT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID AND mls_images.imgOrder = 0
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID
WHERE userID = 413

How to divide a row by one of three counts depending on what its called?

So the query is that I have three different stores, I have a bunch of products in each store and I want to calculate the percentage of the product compared to the total number of products in that store. So with three stores I have three different total number of products that I want to divide the products by depending on what store they are in. However what ends up happening is that the Query divides every product by the first store value, how do I get it to specify which value to divide by depending on the store using this query if possible?
Image of my current table using this query.
SELECT
ss.name,
b.name,
y1.number,
(SUM(svs.facings) / y1.number) * 100 AS SecondaryPercentage
FROM
report.scene_visit_summary svs
LEFT JOIN
static.stores ss ON svs.store_fk = ss.pk
LEFT JOIN
static.template t ON svs.template_fk = t.pk
LEFT JOIN
static_new.product p ON svs.product_fk = p.pk
LEFT JOIN
static_new.brand b ON p.brand_fk = b.pk
LEFT JOIN
(SELECT
SUM(svs.facings) AS number
FROM
report.scene_visit_summary svs
LEFT JOIN static.stores ss ON svs.store_fk = ss.pk
LEFT JOIN static.template t ON svs.template_fk = t.pk
LEFT JOIN static_new.product p ON svs.product_fk = p.pk
LEFT JOIN static_new.brand b ON p.brand_fk = b.pk
WHERE
session_uid IN ('**********************' , '**********************',
'**********************')
GROUP BY ss.name) AS y1 ON y1.number
WHERE
session_uid IN ('**********************' , '**********************',
'**********************')
AND t.name NOT LIKE '%Main%'
GROUP BY b.name

MySQL how to select what I need, most likely an inner join

A bit of a newbie question, probably an INNER JOIN with an "AS" statement, but I can't figure it out...
This is for a MYSQL based competition app. I want to select the "img_title" for both img_id1 and img_id2. I can't figure out how to do it and still see which title is assigned to the associated _id1 or _id2.
My tables:
competitions
comp_id
img_id1
img_id2
on_deck
img_id
img_title
Desired results:
comp_id | img_id1 | img_title1 |img_id2 | img_title2
You need a join for each image:
SELECT comp.comp_id, img1.img_id, img1.img_title, img2.img_id, img2.img_title
FROM competitions comp
INNER JOIN on_deck img1 ON img1.img_id = comp.img_id1
INNER JOIN on_deck img2 ON img2.img_id = comp.img_id2
LEFT JOIN if img_id1 or img_id2 can be NULL.
select comp_id, img_id1, b.img_title as img_title1,
img_id2, b2.img_title as img_title2
from competitions a
left outer join on_deck b on b.img_id = a.img_id1
left outer join on_deck b2 on b2.img_id = a.img_id2
switch let outer join to inner join if you want to exclude rows in competitions that do not have two matching img_ids
This query should give you the results you want. This also assumes that comp.img_id1 and comp.img_id2 are never NULL.
SELECT comp.comp_id
, comp.img_id1
, deck1.img_title AS img_title1
, comp.img_id2
, deck2.img_title AS img_title2
FROM competitions AS comp
JOIN on_deck deck1 ON comp.img_id1 = deck1.img_id
JOIN on_deck deck2 ON comp.img_id2 = deck2.img_id
If you have plan on having a NULL or empty string comp.img_id1 and/or comp.img_id2 fields, you'll need to do some left joins.

Mysql - How to alias a whole table in a left join

I have a situation where a property table holds an address id (from the g_addresses table) and an applicant table also holds an address id from the g_addresses.
I'd like to left join these together but select all the fields in the table.
I know of using 'as' to make an alias for fields, but is there any way to produce an alias for a whole table?
SELECT *
FROM (`reference`)
LEFT JOIN `applicants` ON `applicants`.`id` = `reference`.`applicant_id`
LEFT JOIN `g_people` applicant_person ON `applicant_person`.`id` = `applicants`.`person_id`
LEFT JOIN `g_addresses` applicant_address ON `applicant_address`.`id` = `applicants`.`address_id`
LEFT JOIN `properties` ON `properties`.`id` = `reference`.`property_id`
LEFT JOIN `g_addresses` property_address ON `property_address`.`id` = `properties`.`address_id`
WHERE `reference`.`id` = 4
This produces a result containing only one address row and not both,
The row that is returned is the row from the final join and not the one previously, indicating it is overwriting when it is returned.
I don't think you should use masked references, like * or `reference`.*, in your case, because you may end up with a row set containing identical column names (id, address_id).
If you want to pull all the columns from the joined tables, you should probably specify them individually in the SELECT clause and assign a unique alias to every one of them:
SELECT
ref.`id` AS ref_id,
ref.`…` AS …,
…
app.`id` AS app_id,
…
FROM `reference` AS ref
LEFT JOIN `applicants` AS app ON app.`id` = ref.`applicant_id`
LEFT JOIN `g_people` AS ape ON ape.`id` = app.`person_id`
LEFT JOIN `g_addresses` AS apa ON apa.`id` = app.`address_id`
LEFT JOIN `properties` AS pro ON pro.`id` = ref.`property_id`
LEFT JOIN `g_addresses` AS pra ON pra.`id` = pro.`address_id`
WHERE ref.`id` = 4
Be more specific about columns you select
SELECT
applicant_address.*,
property_address.*,
applicants.*,
applicant_person.*,
properties.*
FROM (`reference`)
LEFT JOIN `applicants` ON `applicants`.`id` = `reference`.`applicant_id`
LEFT JOIN `g_people` applicant_person ON `applicant_person`.`id` = `applicants`.`person_id`
LEFT JOIN `g_addresses` applicant_address ON `applicant_address`.`id` = `applicants`.`address_id`
LEFT JOIN `properties` ON `properties`.`id` = `reference`.`property_id`
LEFT JOIN `g_addresses` property_address ON `property_address`.`id` = `properties`.`address_id`
WHERE `reference`.`id` = 4