Advice on my relational database query - mysql

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

Related

MS Access 2013: Joining tables multiple times

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)

Referencing one field for two different sets of related IDs in MySQL

I'm working on a school assignment for Databases and I'm running into a lot of trouble getting this one to work.
I'm trying to make a query for two columns, one of course prerequisites and one
for course IDs. The title in both of these columns must relate to the ID it is CONCATed with, which is only located under uni_course.
Here's what my output looks like right now from the following query (and what it should look like) :
SELECT CONCAT(uni_course.course_id,': ',title) as Course,
CONCAT(uni_prereq.prereq_id,': ',title) as Prerequisite
FROM uni_course
INNER JOIN uni_prereq ON uni_course.course_id = uni_prereq.course_id;
Any advice is very appreciated, thank you!
You need to add a join on the table again to get the description of the prerequisite course, hope that will help.
SELECT CONCAT(c.course_id,': ',c.title) AS Course,
CONCAT(p.prereq_id,': ',pc.title) AS Prerequisite
FROM uni_course AS c
INNER JOIN uni_prereq AS p ON c.course_id = p.course_id
INNER JOIN uni_course AS pc ON p.prereq_id = pc.course_id;

mysql - How to perform joining of of two junctional tables in case where one of them has foreign key of another?

I am new in the database design, so I am still learning, so sorry for maybe inappropriate terms using, so I will try to explain on common language what problem I have. I learned how to join two tables (getting result) over junction table which is in between, but I got into problem when I want to join one "regular" table and one junction table over another junction table.
I have a relational database which has tables and relations between them like this:
I know how to join hgs_transliterations, hgs_gardiners, hgs_meanings, hgs_word_types using hgs_translations, but what I don't know how to do is how to join those 4 tables and the hgs_references table.
This is my code for joining lower 4 tables:
SELECT hgs_transliterations.transliteration, hgs_gardiners.gardiners_code, hgs_meanings.meaning, hgs_word_types.word_type
FROM hgs_translations
JOIN hgs_transliterations ON hgs_translations.transliteration_id = hgs_transliterations.id
JOIN hgs_gardiners ON hgs_translations.gardiners_id = hgs_gardiners.id
JOIN hgs_meanings ON hgs_translations.meaning_id = hgs_meanings.id
JOIN hgs_word_types ON hgs_translations.word_type_id = hgs_word_types.id
I read some tutorials on this subject which mention AS, INNER JOIN, OUTER JOIN, but I didn't quite understand terminology and how I can use this to create what I need. Sorry for maybe basic questions, but as I say, I am just a beginner and I am trying to understand something deeply so I can use it appropriately. Thank you in advance.
P.S. If someone thinks that this is not good database design (design of relations between tables), I would like to hear that.
Just add two more joins:
SELECT hgs_transliterations.transliteration, hgs_gardiners.gardiners_code,
hgs_meanings.meaning, hgs_word_types.word_type,
hgs_references.reference
FROM hgs_translations
JOIN hgs_transliterations ON hgs_translations.transliteration_id = hgs_transliterations.id
JOIN hgs_gardiners ON hgs_translations.gardiners_id = hgs_gardiners.id
JOIN hgs_meanings ON hgs_translations.meaning_id = hgs_meanings.id
JOIN hgs_word_types ON hgs_translations.word_type_id = hgs_word_types.id
JOIN junc_translation_reference ON junc_translation_reference.translation_id = hgs_translations.id
JOIN hgs_references ON hgs_references.id = junc_translation_reference.reference_id

Getting a list from the first record from inner join

Let's say I have a table named costumer. Those customers have evolutions where I take notes from contacts that are made.
So the costumer table will have the basic information and the the evolutions will have what it needs and a field to point out the customer. Let's say costumer_id.
This will be a One to Many relation and I need to export several CSVs from mysql with a list of the clients when the first evolution was created (evolution.created_at) for a given month.
Been going around and I can't make the query work. Can someone help me? Thanks in advance
You could simply do a select that looks something like this:
SELECT
c.customer_id
min(e.created_at)
FROM
costumer c
inner join evolution e on
e.costumer_id = c.costumer_id
-- Not sure what you mean by given month
where e.month = 'April'
group by c.customer_id

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!