MS Access 2013: Joining tables multiple times - ms-access

I am totally new to MS Access, so please be kind if my requests might look nonsense.
I have a database with two tables: tblHerbs and tblSeasons.
Two fields in tblHerbs refer to tblSeasons and I don't know how to set the relation between these two tables, besides building a simple query that allows me to select everything from tblHerbs (and show it on a Report).
Any kind of help would be highly appreciated.

You need alias
select t.Season1, s1.name, t.Season2, s2.name
from ((tblHerbs t
inner join tblSeasons s1 on s1.name= t.Season1 )
inner join tblSeasons s2 on s2.name= t.Season2)

Related

Query For Getting Value From two tables

Hi i need your help on MS Access Database. I have two tables,tblExpense with three Columns(ExpenseID,ExpenseDate,Status) And tblExpenseDetails(ExpenseID,RefNo,Amount,Purpose).
Which Select Statement should i use if i want to get the Status from tblexpense using the RefNo from tblExpenseDetails?
{SELECT tblexpenseDetails.RefNo,tblexpense.Status,tblexpenses.ExpenseDate FROM tblExpense INNER JOIN tblexpenseDeatils ON tblexpensedetails.ExpenseID=tblexpenses.ExpenseID WHERE tblexpenseDetails.RefNo=?;
Used That and it worked any other ideas are still welcome.

How to use multiple inner joins

How can I create a single sql command to display the statements of accounts of one of the customer using inner join? help please, thanks.
I see that your custumers are identified by the telephone number, i don't think that is a good idea, since telephone number can change quite often in your custumer table, anyway this should be the query.
SELECT SA.* FROM STATAMENT_OF_ACCOUNT_TBL SA
JOIN OFFICIAL_RECEIP_TBL R ON SA.STATEMENT_ACC_NO=R.STATEMENT_ACC_NO
JOIN CUSTUMER_TBL C ON C.CUS_TEL_NO=R.CUS_TEL_NO
WHERE C.CUS_TEL_NO='422-9418'
Ah, and there should not be null on the keys you are trying to join or it could result in nothing as results.

Advice on my relational database query

Hi I have just started using relational databases at college and I am having trouble with queries when using multiple tables. I normalized some data to the best of my knowledge as you can see below, however I am unsure how to write a query which will show me all of the products a particular customer has bought. When I have tried it just shows me all products along with that 1 customer even if they have not bought anything. Many thanks in advance!
p.s I have looked for online documentation and vids on how to create multiple table queries but all I have found involve using one table. If anyone can point me in the direction of a good source to learn this I would appreciate it! Thanks again.
It's only a simple inner join with the related table
select d.product_id, d.product_name
from invoce as a
inner join order as b on a.order_no = b.order_no
inner join customer as c on a.cust_no = c.cust_no
inner join product as d on b.product_id = d.product_id

Need SQL aid for query with LEFT JOIN

I'm a beginner in sql and need some help using the left join (or alternate) function inthe following:
I have 2 tables:
1) Client
2) Server
Client has 2 columns (country and clientname) which is not present in Server. I want to join / copy these two columns into the Server table using the unique identifier column 'ClientID' present in both tables to match and join. How would I go about doing this and does anything recommend an easier way? I currently don't have physical access to a DB so I can't really test out any queries, so any help is appreciated!
Thank you
Are you looking for this?
SELECT s.*, c.country, c.clientname
FROM server s LEFT JOIN client c
ON s.clientid = c.clientid
Here is SQLFiddle demo.
For better understanding of JOINs see A Visual Explanation of SQL Joins

MySQL Query Join 2 tables with 2 relationship tables

I have a real mindbender of a MySQL problem which I am now thinking there is no answer to. Please help me, you are my only hope!
Stripping it down to the basics, I have two tables, "People" and "Activity". It is possible (long story and lots of data involved) for these two tables to be joined by two different relationship tables: people_activity and entity_activity
I need to do a query on the activity table which gets the people record/s linked to activity records based on both relationship tables.
This is what I have, but it is massively slow on lots of data:
select * from activity
left join peopleactivity on peopleactivity.activityid = activity.activityid
left join entityactivity on entityactivity.activityid = activity.activityid
left join people on (peopleactivity.peopleid = people.peopleid OR
entityactivity.entityid = people.peopleid)
Some more notes - I have also tried creating a view to combine the results of the two relationship tables and instead joining people and activity via this view. This also works, but is also still massively slow
Changing how the relationship/s work to consolodate to one table is a major headache
I have also tried a union -like this -
select * from activity
left join peopleactivity on peopleactivity.activityid = activity.activityid
left join people on (peopleactivity.peopleid = people.peopleid)
union
select * from activity
left join peopleactivity on peopleactivity.activityid = activity.activityid
left join people on (entityactivity.entityid= people.peopleid)
which also works, but for other reasons causes me problems. I really need to do this in one query without changing too much underlying.
Has anyone got any super amazing ideas that I have missed??!
You may try to replace OR with IN
left join people on people.peopleid IN (peopleactivity.peopleid, entityactivity.entityid)
1.) Try setting the id of the tables as the primary key on each table
2.) Use inner joins instead of left joins. Not sure why you are using left joins here as you will get all the results of the other tables left joined on the activity table and get basically all records whether or not they have a join value in another table. I think this might also help you. Can you post a describe of your tables.
I think you should keep the UNION query but making those INNER joins. Do you really need LEFT joins?
You could also change it into UNION ALL, which will have some performance gain:
SELECT activity.*, people.*, 'PA' AS joining_table
FROM activity
JOIN peopleactivity ON peopleactivity.activityid = activity.activityid
JOIN people ON peopleactivity.peopleid = people.peopleid
UNION ALL
SELECT activity.*, people.*, 'EA'
FROM activity
JOIN entityactivity ON entityactivity.activityid = activity.activityid
JOIN people ON entityactivity.entityid = people.peopleid
Thanks for the comments. I had tried various incarnations of the above. My answer was to set up a new table, copy all the existing links into that table, and then use triggers to add/remove links to that table whenever the links were added removed in the two separate link tables. This works well and also allows me to use indexes on this new table to keep things nice and snappy. Many thanks for those that took the time to post the ideas though!