Join Orders, Staff's first and last name, item and location into one so I can export the content into an Excel spreadsheet.
SELECT orders.order_id, staff.staff_id, staff.first_name, staff.last_name, items.name, locations.address1, locations.address2, locations.state, locations.zip_code, orders.created_at
FROM orders
INNER JOIN staff
ON orders.staff_id = staff.staff_id
INNER JOIN items
ON orders.item_id = items.item_id
INNER JOIN location_staff
ON location_staff.staff_id = staff.staff_id
INNER JOIN locations
ON location_staff.loc_id = location.loc_id
I am trying to gather this information to put into an excel document but my query is not returning any results. Any help would be appreciated.
Here is An ERD diagram for further understanding
https://www.dropbox.com/s/7inma4s42xq5t4a/ERD.jpg
(Location_staff_link was shortened when created to location_staff)
jsut remove the extra tables in your FROM clause,
SELECT orders.order_id,
staff.staff_id,
staff.first_name,
staff.last_name,
items.name,
orders.created_at
FROM orders
INNER JOIN staff
ON orders.staff_id = staff.staff_id
INNER JOIN items
ON orders.item_id = items.item_id
INNER JOIN locations
ON location.staff_id = staff.staff_id
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Nothing in your query looks bad, so here is what I suggest to find the answer in this sort of case.
First reduce to the bare minimum of the first join and change the fields to select * (temporarily, you can go back to the real fields as soon as you find the issue) so you can se all the data.
Do you get records, then add in any where clauses on the tables in that join. Do you still get records?
Then add in each join and each where clause one at a time until you find the one where the records disappear. This will tell you what you need to do to fix it.
Often in a case like this where no records are returned, one of the joins needs to be a left join or there is no data that meets the terms of the where clauses or one of the joins is improperly defined. And sometimes, there is a problem in that your database does not have the data you were expecting it to have. But first you have to use the process above to diagnose where the problem is.
Related
Firstly, the relationships between the tables in question:
I'm attempting to write a query which retrieves: people_has_trips.people_personID, people_has_trips_tripID, seminarname, agent, and "Amount," which is intended to be the total of all entries in the Payments table for a given entry in the Trip table. This is relevant because this query is meant to be used in a subform on the Access frontend I'm building. In simple terms, the concept for this query is: Which trip(s) has this person been on, and how much money was spent, total, on each trip?
Here's my code so far:
SELECT
people_has_trips.people_PersonID,
people_has_trips.trips_tripID,
people_has_trips.seminar_seminarID,
seminar.seminarname,
agentref.agent,
payments.amount
FROM
(seminar
INNER JOIN (((trips
INNER JOIN people_has_trips ON trips.tripID = people_has_trips.Trips_tripID)
INNER JOIN payments ON trips.tripID = payments.trips_tripID)
INNER JOIN agentnotes ON trips.tripID = agentnotes.Trips_tripID) ON seminar.seminarid = people_has_trips.seminar_seminarid)
INNER JOIN
agentref ON trips.agentref_agentid = agentref.agentid;
Which returns the following:
So we're most of the way there, obviously the final version will be cleaned up a bit (seminarID is unneccesary, for example), so my only question is: what do I need to do so that instead of "amount" showing every individual payment, it's a total of all payments for a trip?
Bonus: The keen-eyed will notice that "seminar" is actually a optional foreign key, and therefore this query, being made of inner joins, excludes any combination of people and trips which does not include a seminar. This is not intended, and another problem I'll need to solve.
Sorry for rearranging the SQL, but I had a hard time following what was going on until I reorganised it.
If you GROUP BY the columns other than the payment value you can use aggregate functions like SUM to get the total of the payments.
SELECT
people_has_trips.people_PersonID,
people_has_trips.trips_tripID,
people_has_trips.seminar_seminarID,
seminar.seminarname,
agentref.agent,
SUM(payments.amount)
FROM seminar
INNER JOIN people_has_trips
ON
people_has_trips.seminar_seminarid = seminar.seminarid
INNER JOIN trips
ON
people_has_trips.Trips_tripID = trips.tripID
INNER JOIN payments
ON
trips.tripID = payments.trips_tripID
INNER JOIN agentnotes
ON
agentnotes.Trips_tripID = trips.tripID
INNER JOIN agentref
ON
agentref.agentid = trips.agentref_agentid
GROUP BY
people_has_trips.trips_tripID,
people_has_trips.people_PersonID,
people_has_trips.seminar_seminarID,
seminar.seminarname,
agentref.agent
;
I am newish to SQL and Join statements and I am way out of my league at the moment
I currently have 6 Database Tables that are all linked to the main 7th table based on the main tables ID, however all the information in the other 6 tables are looped and so have several displayed results to the one main tables ID.
Is it possible to join them all into one Join Statement so I can have a results so that everyones information from the main table also shows their information from the 6 other linked tables
So basically when they all have the informationed joined I want to be able to Display all information on a webpage
so I was wondering do I need to do multiple JOIN statements or just one Longer one?
I have Included some Images below that explain it visually. See examples 1 and 2
The columns that are highlighted in yellow are looped to have many results:
2. This is the example of how the information is looped into the
database where there are many Race_id sharing to the same inf_id:
Im not so sure how it will look once it has been joined since some of the information is looped into many Id's and not sure if that means it need to duplicate the column or the rows?? any help would be greatly appreciated.
You could use left join eg for the first tables influencer, social, activities
select i.*, s.follower, s.Social_Medial_URL, a.activity, a.result
from influencer i
left join social s on s.inf_id = i.id
left join activities a on a.inf_id = i.id
you can procede yourself adding the left join for the others tables using the same rules
select i.*
, s.follower_count
, s.social_media_url
, a.compete_activity
, a.compete_results
from influencers i
left join inf_other_social s on s.inf_id = i.id
left join inf_compete_activity a on a.inf_id = i.id
LIMIT 0, 25
I think you are confused about primary key and foreign key. the picture you have given is clearly elaborate everything. as per your db diagram the query might be like this...
select * from
Influencer i
Left Join Social s on i.inf_id = s.inf_id
Left Join Owned_Equip o on i.inf_id=o.inf_id
Left Join Ages_Interacted a on i.inf_id = a.inf_id
Left Join Activities ac on i.inf_id = ac.inf_id
Left Join Awards aw on i.inf_id = aw.inf_id
Left Join History h on i.inf_id = h.inf_id
By using this above query you can get all the information of Influencer and related to him (Social,Owned_Equip,Activities,Award etc) whether they exists or not. If you using only "Join" not "Left Join" then you can only find those records which is common for a single influencer to it's related entities/tables which might you say. as an example: say Influencer (id = 1 , suppose name is dan) after inner join we can get only records related to dan ( his social,owned equipments,activites,awards and so on if those tables contains record related to dan record)
I have a query which uses LEFT JOIN with multiple criteria to fetch the data. However, the query does not return any data when I use more than one AND clause in the conditions.
Eg:
LEFT JOIN tblName
on x.ABC=y.ABC
AND x.MNO="AnyValue"
AND x.UserId=1
does not return any data.
However when I remove the last AND clause (i.e LEFT JOIN tblName on x.ABC=y.ABC AND x.MNO="AnyValue"), it gives me wrong result. But yes it atleast return some data.
Let me know how to add multiple conditions to get the query working.
I want to get results using the above first condition, i.e LEFT JOIN tblName on x.ABC=y.ABC AND x.MNO="AnyValue" AND x.UserId=1
Your AND ... criteria needs to be in the WHERE part of the query.
First, lets make sure you understand what a LEFT JOIN is. Try to think of it this way. The left side is how I think in the sequential order of the FROM clause. You read left-to-right, so the first table is my LEFT side which is what I ALWAYS want. The right side (next table in the query) is optional, but if it exists, great, I have more detail.
So take a scenario of people and orders. I want a list of all people. But if they have an order, thats even better.
select
P.LastName,
O.OrderID,
O.OrderDate
from
People P
LEFT JOIN Order O
on P.PersonID = O.PersonID
This will do just that. Show all people and IF they have an order, that comes along for the ride. If they have multiple orders, it will show a row for ALL orders they had.
Now, expand on criteria to the next table. Lets say I only wanted to show orders since 2018. That would become part of the LEFT JOIN "ON" clause because I only care about orders portion of the requirement.
LEFT JOIN Order O
on P.PersonID = O.PersonID
AND O.OrderDate >= '2018-01-01'
I would still get ALL people, but only order info will show for those with an order date on/after Jan 1, 2018.
Now, if you only cared about ONLY PEOPLE who HAD ORDERS AND the orders were on after 2018, that would basically eliminate the need for a LEFT JOIN and become a standard (INNER) JOIN
JOIN Order O
on P.PersonID = O.PersonID
AND O.OrderDate >= '2018-01-01'
JOIN Implies the record in the ORDER side table MUST be found
So in your scenario, where is your "x" and "y" alias name coming from. Completely unclear given the context, but hopefully the above clarification will help you resolve your query. If not, update it with more detail and clarification what you are trying to get out of it.
Ok, I am semi efficient with SQL and I tried using an OR statement in a left join and found it didn't work.
The reason for me doing this is because I have two columns from two tables that I am trying to use to join if one returns NULL. I have a two tables - events and seasons. Each table has a sponsor id column which I use to join the sponsors table. The reason behind this is because sometimes a single event is sponsored rather than the whole season or the other way around.
My question is whats the best way I can do this. My original query isn't the same as this but its the same concept (except it doesn't grab every column from every table).
SELECT sp.*, re.*, ev.*, se.*
FROM events ev
INNER JOIN seasons se ON se.season_id=ev.event_sid
INNER JOIN results re ON ev.event_id=re.results_eid
LEFT JOIN sponsors sp ON ev.event_sponsor=sp.sponsor_id
and at the end I tried to tack on OR se.season_sponsor=sp.sponsor_id
Ultimately I am trying to join the sponsors table with either ev.event_sponsor or se.season_sponsor. Any help is much appreciated. I did a little researching and couldn't find an answer to what I was trying to accomplish.
Try:
SELECT sp.*, re.*, ev.*, se.*
FROM events ev
INNER JOIN seasons se ON se.season_id=ev.event_sid
INNER JOIN results re ON ev.event_id=re.results_eid
LEFT JOIN sponsors sp ON COALESCE(ev.event_sponsor,se.season_sponsor)=sp.sponsor_id
Ok, I'm grouping a query by month so I get a record for each month of the year, but I need to figure out how to do this: basically grouping by month on two different JOIN statements. I'm trying to get the count, or sum, of quantities in each child table if possible in one query. I could just loop through in php, but I'm trying to find a way to do in mysql.
Here's a sample
SELECT SUM(purchase_quantity),SUM(sales_quantity)
FROM products p
INNER JOIN purchase_order_lines sl ON pl.product_id = p.id
INNER JOIN purchase_orders po ON pl.purchase_order_id = po.id
INNER JOIN sales_order_lines sl ON sl.product_id = p.id
INNER JOIN sales_orders so ON sl.sales_order_id = so.id
GROUP BY YEAR(so.posted & po.posted), MONTH(so.posted & po.posted)
I know there's no way this would work, and the logic is ridiculous, and this isn't exactly the structure of my database, but just an idea of how things are linked and wwhat I'm trying to do. I'm thinking it would have to be done with subqueries in the SELECT statement, but I haven't come up with anything yet. I'll keep thinking about it, but if anyone has any ideas, that would be awesome.
because your grouping your result, you will get a count of one. why don't you try running a separate statement that will count the result and a separate statement to group it.