i'm able to join 3 tables but i want to get the specific result based on user_id i'm getting null result why?
here is demo:http://sqlfiddle.com/#!9/c58f14/2
here is my query:
SELECT ifc.*,jp.* FROM interview_for_candidate_inbox ifc
LEFT JOIN jobs_applied_by_jobseeker_for_employer jbe on jbe.employee_id = ifc.user_id
LEFT JOIN jobs_posted_by_employer jp on jbe.job_id = jp.id
after the table being constructed
the same constructed table in demo:http://sqlfiddle.com/#!9/c58f14/2 (please run)
when i try this for above result
WHERE user_id = 151 AND job_is_deleted_or_not = 0
i'm getting blank result where BLANK RESULT DEMO:http://sqlfiddle.com/#!9/c58f14/3
please help me thanks in advance
Simply because
WHERE user_id = 152
Didn't match anything.
If you rewrite your query like this:
SELECT DISTINCT user_id FROM interview_for_candidate_inbox ifc
LEFT JOIN jobs_applied_by_jobseeker_for_employer jbe on jbe.employee_id = ifc.user_id
LEFT JOIN jobs_posted_by_employer jp on jbe.job_id = jp.id
WHERE job_is_deleted_or_not = 0;
You will find distinct user_id values, none of them is 152
Related
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
Please find below a link to the table-structure I have set up and to the query I am running.
Link to tables and query.
The present result is that only the firstnames, lastnames and "education_finished" are showing. But all the option_id's and their related lang_values still show "NULL".
The desired result:
Any suggestions how to solve?
Below is the query that you are using:
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_data d
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_name
LEFT JOIN phpbb_profile_fields_lang fl
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
The reason why you are getting null value is because of this condition:
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_name
You are trying to join on pf_education_finished (int) field of one table and field_name (int) field of another table. Also, there are no matching values (e.g. pf_education_finished contains numbers whereas field_nameis 'education finished').
If you want the query to return something then you need to join on field_id and phpbb_profile_fields needs to have some records with matching field id, e.g.:
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_data d
LEFT JOIN phpbb_profile_fields f
ON d.pf_education_finished = f.field_id
LEFT JOIN phpbb_profile_fields_lang fl
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
Here's the updated SQL Fiddle.
SELECT d.pf_firstname, d.pf_lastname, f.field_id, fl.option_id,
d.pf_education_finished, fl.lang_value
FROM phpbb_profile_fields_lang fl
inner JOIN phpbb_profile_fields f
ON f.field_id = fl.field_id
inner JOIN phpbb_profile_fields_data d
ON f.field_id = fl.field_id
ORDER BY d.pf_lastname ASC
This is the optional query if you want to display data from 3-4 tables but in this query names are repeats as per the count of field_id present in phpbb_profile_fields_lang.
The exact solution you are looking is, when you have the same primary key in all the tables from which you are retrieving the data.
Thank you.
So I have some code I'm trying to figure out... I have two tables:
TABLE: matches
event_id
match_id (primary)
match_score
match_p1
match_p2
match_win
TABLE: results
event_id
user_id
result_id (primary)
result_name
result_extra
The weird thing about the data is the content of of the matches table actually links to the results table in multiple fashions.
There will be an integer in match_p1 and match_p2 that link to the results_extra field on the results table. This is designed because each match has two players in it (p1 and p2), and each player has one result for each event.
If I wanted to get a list of all matches in an event, I would do the following:
SELECT *
FROM matches
WHERE event_id = 324
If I wanted to get a list of all matches belonging to a single player, I would do:
SELECT matches.*
FROM matches
LEFT JOIN results
ON ((results.result_extra = matches.match_p1) OR
(results.result_extra = matches.match_p2))
WHERE results.user_id = 1566
However, this is where things get a bit complicated... What if I wanted to get a list of matches where player 1566 fought player 2058? Its the logic for this query I can't figure out. Could you guys help me out?
Here is one way. Join results twice, and match the 2 player combinations.
select a.*
from matches a
join results b on a.match_p1 = b.result_extra
join results c on a.match_p2 = c.result_extra
where (b.user_id = 1566 and c.user_id = 2058) or (b.user_id = 2058 and c.user_id = 1566)
Could be this
SELECT matches.*
FROM matches
LEFT JOIN results a
ON ((a.result_extra = matches.match_p1 AND
a.result_extra = matches.match_p2))
LEFT JOIN results b
ON ((b.result_extra = matches.match_p1 AND
b.result_extra = matches.match_p2))
WHERE a.user_id = 2058
AND b.user_id = 1566
If 1566 and 2059 is user_ids,maybe this help you
SELECT matches.*
FROM matches
LEFT JOIN results
ON ((results.result_extra = matches.match_p1) OR
(results.result_extra = matches.match_p2))
WHERE results.user_id in (1566,2058);
I've got a query I'm trying to write to get counts of active users and active contacts associated with each account. I have attempted to run the counts separately and in both cases they run at under 1 sec but when I put them together as seen below I don't get a result. Please let me know if there is anything I can do it enhance the query.
select count(c.c_no) as contacts_count, count(u_no) as user_count, a.*
from accounts a
LEFT JOIN users u on u.a_no = a.a_no and u_status = 1
LEFT JOIN IDP1.contacts c on c.a_no = a.a_no and c_status = 1
where a_status = 1
group by a_no
THANK YOU!
Do not use * and aggregate functions simultaneously
Try this
select count(c.c_no) as contacts_count, count(u_no) as user_count, a_no
from accounts a
LEFT JOIN users u on u.a_no = a.a_no and u_status = 1
LEFT JOIN IDP1.contacts c on c.a_no = a.a_no and c_status = 1
where a_status = 1
group by a_no
I have user related info split into multiple tables . I am trying to write a join to retrieve data for a single user , a lot of the info is optional , so the entry for many columns may be null , which is okay . I have written the following query , it is working except that it returns all users when I want the user with id '69'
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN (
SELECT *
FROM cur_website
WHERE Userid =69
) AS w ON cur_doctor_names.UserId = w.Userid
I want the following result :
First_Name | Last Name | Website
ABC XYZ Null
where ABC is the name for user with id 69 .
You are only filtering the websites because you are using a left join. You should apply your filter to the doctors table
SELECT
cur_doctor_names.First_Name,
cur_doctor_Names.Last_Name,
w.Website
FROM
cur_doctor_names
LEFT JOIN cur_website AS w
ON cur_doctor_names.UserId = w.Userid
WHERE
cur_doctor_names.UserId = 69
Try writing it like this - your where clause was in the wrong place (also your left join does not need to be written like that):
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website as w
ON cur_doctor_names.UserId = w.Userid
WHERE cur_doctor_names.userId = 69
I think you can just write
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website w ON cur_doctor_names.UserId = w.Userid
WHERE Userid =69