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.
Related
Im sorry i cant explain the question
I have a problem that i need to join 4 tables together to find some information. Im using inner join which is bringing me the right data but duplicate row having a minor change data. Lets say im getting this data
See the Table here
If we look at the results. At the last 4 rows we can notice simillar results. What i want to achieve is these 4 rows converted into 2.
I only want rows whose account id = vehicle account id and 110. I want these two rows. The join query i write is as follows
select Distinct vh.VehicleNo,jd.AccountID, tr.ID,jd.Memo,je.Description, jd.Cr,jd.Dr ,jd.Detail, je.JEntryId, je.ExpenseID from tbl_JDetail jd
inner join tbl_JEntry je on je.JEntryId = jd.JEntryID
inner join tbl_Trip tr on tr.ID = je.RefID
inner join tbl_Vehciles vh on vh.VID = tr.VehicleID
I really need to achieve this.. Any help would be appreciated how can i achieve. What i want is let me write the query in simple langauge
The query should first find rows in jdetail with accountid = 110. Then get its jentryid . Then in Jentry it should find the tripid and in trip table i have the vehicle id. Vehicle table contains vehicle id.
Then the query should find in jdetail with same jentryid and vehicleaccountid
i need jdetail accountid = 110 and accountid = vehicle.accountid
The vehicle relation is Jdetail > Jentry > Trip > Vehicle
i hope i was clear enough
Please add supporting information (create table statements, sample data, current output and desired output) to your question. As you have not included supporting information, I am unable to test this. This is my "best guess" based on your description.
select Distinct vh.VehicleNo,jd.AccountID, tr.ID,jd.Memo,je.Description, jd.Cr,jd.Dr ,jd.Detail, je.JEntryId, je.ExpenseID
from tbl_JDetail jd
inner join tbl_JEntry je
on je.JEntryId = jd.JEntryID
inner join tbl_Trip tr
on tr.ID = je.RefID
inner join tbl_Vehciles vh
on vh.VID = tr.VehicleID
and vh.AccountID = jd.AccountID
where jd.AccountID = 110
Solved it by using 2 CTEs. CTE1 finds the jentry_id of all records using account ID = 110
and CTE2 find all the required data with inner join cte on cte.JEntryId = jd.JEntryID
It solved all my problems..
Thankyou. Hope it help someone else too
I am trying to create a MYSQL query that pulls in data from a range of tables. I have a master bookings table and an invoice table where I am recording invoice id's etc from Stripe.
I am storing two invoices per booking; one a deposit, the second the final balance.
In my admin backend I then display to the admin info on whether the invoice is paid etc so need to pull in data from SQL to show this.
I'm following some previous guidance here What's the best way to join on the same table twice?.
My query is returning data, however when the invoices table is included twice (to give me the deposit and balance invoices) however the column names are identical.
Could someone point me in the right direction? I think I need to somehow rename the columns on the second returned invoice??? Sorry new to anything but basic SQL queries.
This is my SQL
SELECT * FROM bookings
INNER JOIN voyages ON bookings.booking_voyageID = voyages.voyage_id
LEFT JOIN emailautomations ON bookings.booking_reference = emailautomations.automation_bookingRef AND emailautomations.automation_sent != 1
LEFT JOIN invoices ON bookings.booking_stripeDepositInvoice = invoices.invoice_id
LEFT JOIN invoices inv2 ON bookings.booking_stripeBalanceInvoice = inv2.invoice_id
Thanks to #Algef Almocera's answer I have amended my SQL (and stopped being lazy by using SELECT *, was able to trim loads of columns down to not many!)
SELECT
bookings.booking_status,
bookings.booking_reference,
bookings.booking_stripeCustomerReference,
bookings.booking_stripeDepositInvoice,
bookings.booking_stripeBalanceInvoice,
bookings.booking_totalPaid,
bookings.booking_voyageID,
bookings.booking_firstName,
bookings.booking_lastName,
bookings.booking_contractName,
bookings.booking_contractEmail,
voyages.voyage_id,
voyages.voyage_name,
voyages.voyage_startDate,
depositInvoice.invoice_id AS depositInvoice_id,
depositInvoice.invoice_status AS depositInvoice_status,
balanceInvoice.invoice_id AS balanceInvoice_id,
balanceInvoice.invoice_status AS balanceInvoice_status
FROM bookings
INNER JOIN voyages ON bookings.booking_voyageID = voyages.voyage_id
LEFT JOIN emailautomations ON bookings.booking_reference = emailautomations.automation_bookingRef AND emailautomations.automation_sent != 1
LEFT JOIN invoices depositInvoice ON bookings.booking_stripeDepositInvoice = depositInvoice.invoice_id
LEFT JOIN invoices balanceInvoice ON bookings.booking_stripeBalanceInvoice = balanceInvoice.invoice_id
This, sometimes, couldn't be avoided as keywords might actually often be the same but of different purpose per table. To help with that, you can use aliases. for example:
SELECT
invoices.column_name AS invoices_column_name,
transactions.column_name AS transactions_column_name
FROM invoices ...
LEFT JOIN transactions ...
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'
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;
I had most of this query worked about except two things, large things, one, as soon as I add the forth table [departments_tbl]into the query, I get about 8K rows returned when I should only have about 100.
See the attached schema, no the checkmarks, these are the fields I want returned.
This won't help, but here is just one of the queries that I almost had working, until the [department_tbl was added to the mix]
SELECT _n_cust_entity_storeid_15.entity_id,
_n_cust_entity_storeid_15.email,
customer_group.customer_group_code,
departments.`name`,
departments.manager,
_n_cust_rpt_copy.first_name,
_n_cust_rpt_copy.last_name,
_n_cust_rpt_copy.last_login_date,
_n_cust_rpt_copy.billing_address,
_n_cust_rpt_copy.billing_city,
_n_cust_rpt_copy.billing_state,
_n_cust_rpt_copy.billing_zip
FROM _n_cust_entity_storeid_15 INNER JOIN customer_group ON _n_cust_entity_storeid_15.group_id = customer_group.customer_group_id
INNER JOIN departments ON _n_cust_entity_storeid_15.store_id = departments.store_id,
_n_cust_rpt_copy
ORDER BY _n_cust_rpt_copy.last_name ASC
I've tried subqueries, joins, but just can't get it to work.
Any help would be greatly appreciated.
Schema Please note that entity_id and cust_id fields would the be links between the _ncust_rpt_copy table and the _n_cust_entity_storeid_15 tbl
You have a cross join to the last table, _n_cust_rpt_copy:
SELECT _n_cust_entity_storeid_15.entity_id,
_n_cust_entity_storeid_15.email,
customer_group.customer_group_code,
departments.`name`,
departments.manager,
_n_cust_rpt_copy.first_name,
_n_cust_rpt_copy.last_name,
_n_cust_rpt_copy.last_login_date,
_n_cust_rpt_copy.billing_address,
_n_cust_rpt_copy.billing_city,
_n_cust_rpt_copy.billing_state,
_n_cust_rpt_copy.billing_zip
FROM _n_cust_entity_storeid_15 INNER JOIN
customer_group
ON _n_cust_entity_storeid_15.group_id = customer_group.customer_group_id INNER JOIN
departments
ON _n_cust_entity_storeid_15.store_id = departments.store_id join
_n_cust_rpt_copy
ON ???
ORDER BY _n_cust_rpt_copy.last_name ASC;
It is not obvious to me what the right join conditions are, but there must be something.
I might guess they it at least includes the department:
_n_cust_rpt_copy
ON _n_cust_rpt_copy.department_name = departments.name and