Hi guys I have a query like somethign below. I am using two inner join and I am select from two two but different coloums
the first inner join changes to see if any of the staffs shares a company vehicle with other staff.
The second inner join changes to see if the what staff level drivers what type of vechicle in the company.
select
van_col1, van_col2, admin_col3, admin_col4
from
user
INNER JOIN
admin
ON
user.van_col1=adim.admin_col3
INNER JOIN
user
ON
adim
adim.admin_col3=user.van_col2
If you join the same table twice then you have to use alias names to differ the joins of the table
select u1.van_col1, u2.van_col2, a.admin_col3, a.admin_col4
from user u1
INNER JOIN admin a ON u1.van_col1 = a.admin_col3
INNER JOIN user u2 ON a.admin_col3 = u2.van_col2
And if you have columns in that tables that are named equaly then you have to tell the DB from which table you like to take the column, otherwise it would be ambiguous.
Related
I have four tables where I am trying to left join the 2nd-4th to the one on the left in this picture. From left to right:
1st table (jobs) is a table of jobs
2nd table (applications_jobs) is a bridge table to link jobs and application IDs
3rd table (applications) is applications
4th table (candidates) is candidates based on those applications
I want to get some columns from 1st table (jobs) and 4th table (candidates). I want to get job name (name) and status (status) columns from jobs table. I want to get first name (first_name) and last name (last_name) from candidates table.
Here's what I've tried:
SELECT
name, status, first_name, last_name
FROM
jobs, candidates
left join
applications_jobs aj on jobs.job_id = id
left join
applications a on aj.job_id = a.id
left join
candidates c on a.candidate_id = c.id
but get a error:
ERROR: invalid reference to FROM-clause entry for table "applications_jobs"
HINT: There is an entry for table "applications_jobs", but it cannot be referenced
from this part of the query.
any ideas?
The items you are selecting should be identified by the table they are coming from when you are performing joins, though it is not always necessary if the tables don't share column names. By writing it out, it would help to prevent the confusion you're having with the FROM clause.
The FROM clause can only be from a single table. In this case, it would be your 'jobs' table. Also, to properly reference your columns in your query, the first join should be application_jobs aj ON aj.job_id = jobs.id, and your second join should be applications a ON aj.application_id = a.id.
SELECT
"jobs".name, "jobs".status, "c".first_name, "c".last_name
FROM
jobs
left join
applications_jobs aj on aj.job_id = jobs.id
left join
applications a on aj.application_id = a.id
left join
candidates c on a.candidate_id = c.id
If you are still getting NULLs for the first and last names, Then you don't have candidates that have applications for that specific job. If you want to omit results that would otherwise be NULL, you can do an INNER JOIN on candidates so that it only returns records that exist on both sides of the equation.
How do I join two tables with the SQL Query in the image attached? Currently is pulling no data.
Here is an example of the report I need
Example report
I think this would work, you can change the type of JOIN according to your requirement:
SELECT
o.DMN_ID,
o.ORG_ID,
o.ORG_DESC,
o.ORG_TYP_ID,
o.LST_UDR_USR,
o.LST_UPD_TSTMP,
og.ORG_GROUP_ID,
og.ORG_GROUP_DESC,
og.LST_UPD_USR,
og.LST_UPD_TSTMP
FROM PA_ORG o INNER JOIN
PA_ORG_GROUP og ON
o.ORG_ID = og.ORG_GROUP_ID
and o.ORG_DESC = og.ORG_GROUP_DESC
I'm having trouble doing the following query. The idea is that I have two tables Stores and Users. In Stores I have the columns store_owners and store_last_modified, both values are integer that are related to the id of dbo.Users. How I can display the name that is stored in users related to the two columns. Like that:
select stores.name , users.name as name_store_owner , users.name as name_store_last_modified
from stores
LEFT JOIN users ON stores.store_owners=users.id (related to name_store_owner)
LEFT JOIN users ON stores.store_last_modified=users.id (related to name_store_last_modified)
How do I do that?
Thank you in advance.
You need to give the tables aliases, so you can refer to the same table twice in the from clause. In addition, you need to refer to the right table (users not stores):
select s.name, uo.name as name_store_owner, um.name as name_store_last_modified
from stores s left join
users uo
on s.store_owners = uo.id left join
users um
on s.store_last_modified = um.id
It appears that the condition can be checked without referring the table twice.
select * from
stores s
left join
users u on u.id = s.store_owners
and u.id = s.store_last_modified
From your question it appears that user id (id) should match with both the columns in the stores table for a single row.
I have 2 tables in an MS Access 2013 database that I have pulled through an ODBC. One is called PUBLIC_PHONE_NUMBERS and the 2nd is PUBLIC_CUSTOMER. Now I need to join the two fields PHONE_ACCT and PHONE_NUMBER from the phone table to the customer table. The phone account number is the same as the customer account number, but I need each account to have its phone number listed on the customer's table as well.
Here is what I thought would work:
SELECT public_phone_numbers.phone_acct, public_phone_numbers.phone_number
FROM public_phone_numbers
INNER JOIN public_customer
ON public_phone_numbers.phone_acct = public_customer.c_acct;
not sure I understand what you need, but perhaps it's only a left outer join?
SELECT c.*, p.phone_number
FROM public_customer c
left outer JOIN public_phone_numbers p
ON c.c_acct = p.phone_acct
I am working in Oracle APEX.I am Making report from following four tables Patient History Junction and Disease but unable to make it.
I want to SELECT
Pat_Name,Pat_Age` from Patient Table
.
Treated_By,Sys_Date from History Table
and
Dis_Name from Disease Table
.There is a Junction Table between History and Disease. Below is the diagram of the above scenario.
You need to JOIN each of the tables, similar to this:
select p.pat_name,
p.pat_age,
h.treated_by,
h.sys_date,
d.dis_name
from patient p
inner join history h
on p.pat_id = h.pat_id
and p.app_id = h.app_id
left join junction j
on p.pat_id = j.pat_id
left join disease d
on j.dis_id = d.dis_id
If you need help learning join syntax, check this helpful visual explanation of joins.
Notice that I used an INNER JOIN between patient and history and joined the tables on both keys in patient. This type of join will return all matching records in both tables.
I used a LEFT JOIN on the other two tables which will return all of the patient/history data even if there is not a matching record in the other two tables. Depending on your need, you might be able to use an INNER JOIN on those tables.