Get values from join query - mysql

I have two tables: user and details table. I want to display the details table in a table format.
user table Structure is:
details Table Structure is:
Result table Structure is:
I have tried this query:
SELECT A.*,B.name,C.name as Interviewer
FROM exitdetail_hrms A
LEFT JOIN hh_tbl_user B ON A.emp_id=B.sno
JOIN hh_tbl_user C
WHERE A.emp_id='12' AND C.sno='13'
But I didn't get exact answer..kindly help me on this..
Thanking You..

I am not sure, because you table description does not match the SQL you used, but i think the following SQL should solve your problem:
SELECT A.*,B.name,C.name as Interviewer
FROM exitdetail_hrms A
LEFT JOIN hh_tbl_user B ON A.emp_id=B.s_no
LEFT JOIN hh_tbl_user C ON A.interviewed = C.s_no
WHERE A.emp_id='12' AND C.sno='13'

Related

Left Join on Three tables not giving expected output

I have three tables, purchase, purchase_item, and customer. Iam trying to use left join on these tables. But, iam not getting the expected result in MS Access.
the query is:
SELECT *
FROM (CUSTOMER
left JOIN purchase ON CUSTOMER.CustomerID = PURCHASE.CustomerID) left join purchase_item on purchase_item.InvoiceNumber = purchase.InvoiceNumber;
Getting output as :
https://i.stack.imgur.com/VMJfs.png
I have double-checked all the tables. All the tables have matching data, but still purchase_item data is not getting populated when trying to view all the data. Please help me to find where Iam going wrong.
PURCHASE table:
https://i.stack.imgur.com/qnPT7.png
PURCHASE_ITEM table:
https://i.stack.imgur.com/BbM2d.png
CUSTOMER table:
https://i.stack.imgur.com/oqZ9q.png
Thanks in advance.
First, try correcting the ON order:
...LEFT JOIN purchase_item ON purchase.InvoiceNumber = purchase_item.InvoiceNumber
It is good practice to use the form: A JOIN B ON A.ID = B.ID
If this doesn't work, answer back, and we can look at other things to try.

Querying mysql with a bridge table

I have a database scheme like this:
All I want is to query data from these tables where the one in the middle is a bridgetable.
I want to write a query to get Details from WrapupCodes table for all the WrapupIds that exists in Bridgetable for the same contactId as in CallDetails table.
e.g:
Similarly this is the table I want to get details from
And here is the main table.
Thanks in advance.
It is very basic SQL:
SELECT *
FROM WrapupCodes w
INNER JOIN Contact_Wrapup c ON c.WrapupID = w.WrapupID
INNER JOIN CallDetail d on d.ContactID = c.ContactID

SQL: selecting all values that meet one criteria from 2 tables

I have looked up some questions relating to this topic, but can't seem to find what I need:
I have 2 tables, and I would like to append information from the main table to the other matching one criteria from both. so in one table I have zip code information, I want to append all the users from the main table which match the zip codes in the other table. so i want something like:
any kind of pointers would be greatly appreciated. Thank you!
This should be a simple JOIN of the two tables on the zip_code field. Something like:
SELECT z.zip_code, u.user_id
FROM MyZipCodeTable z
LEFT JOIN MyUserTable u ON z.zip_code = u.zip_code
This will return all zip codes and any matching users.
I think you are looking for all the users from the main table with zip codes in the other table. I assume you can join both tables on user_id.
To get that you want something like this:
SELECT main_table.*, other_table.zipcode
FROM main_table mt
LEFT JOIN other_table ot ON mt.user_id=ot.user_id

MySQL Inner Join Against an Inner Join?

I have 3 tables "Employees", "EmployeeLeaveDays" and "EmployeeLeaves".
I'm looking to create a view that displays the date of the leave and the employee name. So in order for my calendar to work I have split everyones leave into individual days(EmployeeLeaveDays) which has an FK that links each day back to (EmployeeLeaves) which has other details around the leave, in EmployeeLeaves I have a column "employee" which is an FK back to employees which contains the name.
So In my view I want to return the name as you can see is 2 tables away, I've wrote this MySQL query but it doesn't work (returns no data), I'm wondering if there is anyway to do what I need to do?
SELECT
EmployeeLeaveDays.id,
EmployeeLeaveDays.employee_leave,
EmployeeLeaveDays.leave_date,
EmployeeLeaveDays.leave_type
FROM EmployeeLeaveDays
INNER JOIN EmployeeLeaves
ON EmployeeLeaveDays.employee_leave=EmployeeLeaves.employee
INNER JOIN Employees
ON EmployeeLeaves.employee=Employees.employee_id;
Hopefully from that you're able to see what I'm trying to achieve, how ever I've attached some screenshots of the table structure.
Thanks
After some thinking I got there in the end. Here's the final query.
SELECT
EmployeeLeaveDays.id,
EmployeeLeaveDays.employee_leave,
EmployeeLeaveDays.leave_date,
EmployeeLeaveDays.leave_type,
EmployeeLeaves.employee,
Employees.employee_id,
Employees.first_name,
Employees.last_name
FROM EmployeeLeaveDays
LEFT JOIN EmployeeLeaves ON EmployeeLeaveDays.employee_leave = EmployeeLeaves.id
LEFT JOIN Employees ON EmployeeLeaves.employee = Employees.id;

select from a table where the dates are different to tha dates in other table

I have some dates in a table and i need a query that the dates in the table are different to the second table, i am intenting with this:
http://sqlfiddle.com/#!2/a1308/2
But it now works
I need for example in the first table:
3202024834
3108984111
3118545645
4608389
2631993
9876534
3114568970
and in other
3202024834
3108984111
3118545645
Then the output of the query is:
4608389
2631993
9876534
3114568970
I am interpreting your question as "Get values in a that are not in b". If so, a good way to approach this is with a left outer join along with a where clause:
select a.msisdn
from msisdn a left outer join
insertados b
on a.msisdn = b.numero
where b.numero is null;
The data is the SQLFiddle doesn't exactly match the data in the question, so the results are a bit different from what is in the question.