How to left join on mysql between two tables? - mysql

Hi i'm trying to left join from a vessel table to daily_volume table. The two table contains the following data:
(vessel_table)
(daily_volume table)
Let's say today is 2019-07-08. I would like to left join vessel_table to daily_volume table in a way it will show this output as brand new:
My current sql statement is this but doesn't seem to achieve the above output:
SELECT * FROM vessel LEFT JOIN daily_volume ON vessel.vessel_id = daily_volume.vessel_id WHERE daily_volume.date is null
Can anyone help me how should i query to produce the output i need? Thanks
i have tried looking stackoverflow but to no avail
SELECT * FROM vessel LEFT JOIN daily_volume ON vessel.vessel_id = daily_volume.vessel_id WHERE daily_volume.date is null
vessel_id volume date
1 null null
2 null null
3 null null
4 null null
5 null null
6 null null

You seem to want a condition on the current date in the ON clause:
SELECT v.vessel_id, dv.volume, dv.date
FROM vessel v LEFT JOIN
daily_volume dv
ON dv.vessel_id = dv.vessel_id AND
dv.date = curdate();

Related

Whats wrong with this query? i am getting null result

SELECT `acart`.`order_number` AS `admin_order_number`,
`acart`.`user_id` AS `admin_user_id`,
`acart`.`created_by` AS `admin_created_by`,
`rcart`.`order_number` AS `renew_order_number`,
`rcart`.`user_id` AS `renew_user_id`,
`rcart`.`created_by` AS `renew_created_by`,
`scart`.`order_number` AS `shopping_order_number`,
`scart`.`user_id` AS `shopping_user_id`,
`scart`.`created_by` AS `shopping_created_by`
FROM `cdp_order_transaction_master` AS `master`
LEFT JOIN `cdp_admin_shopping_cart` AS `acart`
ON `acart`.`order_number`=`master`.`order_number`
LEFT JOIN `cdp_renew_cart` AS `rcart`
ON `rcart`.`order_number`=`master`.`order_number`
LEFT JOIN `cdp_shopping_cart` AS `scart`
ON `scart`.`order_number`=`master`.`order_number`
WHERE master.order_number IS NULL
Let me explain my problem,if the order is successfull then it will goes to cdp_order_transaction_master table and any other 3 table (cdp_admin_shopping_cart,cdp_renew_cart,cdp_shopping_cart) depending on the situation but if the order fails then it will not go to cdp_order_transaction_master table and remain in other tables,
so i want the failed order which is not present in cdp_order_transaction_master and can present in any other tables(cdp_admin_shopping_cart,cdp_renew_cart,cdp_shopping_c‌​art)
WHERE master.order_number IS NULL
This is your primary table, and the join condition for all other tables are on this column.
You are trying to join null to null
Based on your comment, try:
select 'cdp_admin_shopping_cart' as `err_table`, a1.order_number
from cdp_admin_shopping_cart a1
where not exists (select 1 from cdp_order_transaction_master a2 where a2.order_number = a1.order_number)
union all
select 'cdp_renew_cart' as `err_table`, a1.order_number
from cdp_renew_cart a1
where not exists (select 1 from cdp_order_transaction_master a2 where a2.order_number = a1.order_number)
union all
select 'cdp_shopping_c‌​art' as `err_table`, a1.order_number
from cdp_shopping_c‌​art a1
where not exists (select 1 from cdp_order_transaction_master a2 where a2.order_number = a1.order_number)

RIGHT JOIN is returning all NULL results

I have two tables:
I'm trying to retrieve fields name, quantity and custom_message from table 1 and join the correct associated field value from table 2 - to give me:
name | quantity | custom_message | value
This is my query:
SELECT vxu_4_wpsc_cart_contents.name, vxu_4_wpsc_cart_contents.quantity,
vxu_4_wpsc_cart_contents.custom_message
FROM vxu_4_wpsc_cart_contents
RIGHT JOIN vxu_4_wpsc_submited_form_data
ON vxu_4_wpsc_cart_contents.id = vxu_4_wpsc_submited_form_data.id
WHERE form_id =2
OR form_id =3
which is returning
name | quantity | custom_message
NULL NULL NULL
NULL NULL NULL + 3 more rows of nulls
vxu_4_wpsc_cart_contents:
table 1
vxu_4_wpsc_submited_form_data:
I just don't know where I'm going wrong!
I believe right join should be inner join. Try the following query:
SELECT
vcc.name,
vcc.quantity,
vcc.custom_message,
vfd.value
FROM
vxu_4_wpsc_cart_contents AS vcc
INNER JOIN vxu_4_wpsc_submited_form_data as vfd
ON vcc.purchaseid = vfd.log_id
WHERE
vfd.form_id IN (2,3);
(fyi: your second image link is the same as the first :))

LEFT OUTER JOIN with four table

Here is the table structure
User table
Column
fb_id
email
name
fname
lname
gender
fb_link
created
referral table
Column
id
referred_by
joinee
created
Currently implemented one SQL is (Example 1)
SELECT u.fb_id
,fb_link
,name
,r.referred_by
,u.created
FROM users u
LEFT OUTER JOIN referral r
ON u.fb_id=r.joinee
result of above query
joinee referer
10152250261037651 NULL
10152604594389921 NULL
10154430845000507 1518673071699780
....
....
....
....
547146735389782 NULL
Here is the sql (Example 2)
SELECT u_joinee.fb_id joinee_fb_id
,u_referer.fb_id referer_fb_id
,u_joinee.NAME joinee_name
,u_referer.NAME referer_name
FROM users u_joinee
,users u_referer
,referral r_j
,referral r_r
WHERE u_referer.fb_id = r_r.referred_by
AND u_joinee.fb_id = r_j.joinee
result of above query
joinee_fb_id referer_fb_id joinee_name referer_name
10154430845000507 1518673071699780 Saselsdein Bsdasd
What I am looking out for is I need both name in joinee_name and referer_name in the output. But I ma not able to use four tables in LEFT OUTER JOIN clause like i did for example 1 I want to fetch both records which include both non referral and referral joinees Is there any way to expect required result as shown below
Expected result
joinee_fb_id referer_fb_id joinee_name referer_name
10154430845000507 1518673071699780 Saselsdein Bsdasd
10154430845000347 Null asd NULL
10154430845000567 Null asd asdm NULL
10154230845000567 Null Dsd asdm NULL
10154330845000567 Null sdm NULL
101544553045000567 Null Esd aedm NULL
You can join with the users table twice by giving it different aliases, just like you do with your implicit inner join.
I don't think you need to join with referral twice. A single row in the referral table links a referrer and a joinee.
SELECT u_joinee.fb_id joinee_fb_id ,u_referer.fb_id referer_fb_id, u_joinee.name joinee_name, u_referer.name referer_name
FROM users AS u_joinee
LEFT JOIN referral AS r ON r.joinee = u_joinee.fb_id
LEFT JOIN users AS u_referer ON r.referred_by = u_referer.fb_id

sql multiple join and count

I have tables like this. but it seems MySQL doesn't count my second join currently. i want to know what i missed for process count of reports for my comment list.
and i want to have average of rates also count of reports
SELECT *, avg(rate.score), count(report.id) FROM `comment`
left join rate on (comment.id = rate.comment_id)
left join report on (comment.id = report.comment_id)
group by comment.id
id text id comment_id score id comment_id type avg(rate.score) count(report.comment_id)
1 good article 1 1 2 1 1 1 4.0000 20
2 bad article NULL NULL NULL NULL NULL NULL NULL 0
good article have 2 reports.
count(report.id) give me wrong value. what's my mistake?
SELECT
*,
avg(rate.score),
(SELECT
count(report.comment_id)
FROM
report
WHERE
comment.id = report.comment_id) AS num_reports
FROM
comment
left join
rate ON (comment.id = rate.comment_id)
group by comment.id
Here's the example:
http://sqlfiddle.com/#!2/cf313/15
You dont need *. Try this
SELECT comment.id, avg(rate.score), count(report.id) FROM `comment`
left join rate on (comment.id = rate.comment_id)
left join report on (comment.id = report.comment_id)
group by comment.id

Mapping table MySQL / Access

I have a short access/mySQL question. I have a mapping table on the format below.
ID Category_A Category_B Category_C Team
1 a b T1
2 a d T2
I have a second table which also includes Category_A, Category_B, and Category_C. I would like to join the Team value to the my second table based on the mappingtable. My problem is that when there is a blank (e.g. ID=2, Category_B) the mapping should assign the T2 to any row that contains Category_A=a and Category_C=d regardless of the value in Category_B.
Can this type of mapping be done?
Grateful for your help!
In MS Access, I think you would need something on the lines of:
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_C = t.Category_C)
AND (m.Category_B = t.Category_B)
AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Not Null
AND m.Category_B Is Not Null
AND m.Category_A Is Not Null
UNION ALL
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_B = t.Category_B)
AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Null
AND m.Category_B Is Not Null
AND m.Category_A Is Not Null
UNION ALL
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_C = t.Category_C)
AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Not Null
AND m.Category_B Is Null
AND m.Category_A Is Not Null
UNION ALL
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_C = t.Category_C)
AND (m.Category_B = t.Category_B)
WHERE m.Category_C Is Not Null
AND m.Category_B Is Not Null
AND m.Category_A Is Null