Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 complex relational query

Author  Topic 

false74
Starting Member

2 Posts

Posted - 2013-07-02 : 02:06:28
Hi, I need some help with querying some data in my database, it is a sqlite3 database but anything in sql should be pretty similar. I am working on a project that stores data about pieces of equipment (table: Equipment); each piece of equipment may have multiple usermanuals linked to it which is stored in another table (table: Manual); each piece of equipment may be assigned to one or multiple locations.

Below is an example of how I have my tables set up:

Table: Equipment
EquipmentID EquipmentName
1 Hammer
2 Wrench
3 Tape
4 Screwdriver


Table: Manual
ManualID EquipmentID ManualFilename
1 1 hammerRevision1.pdf
2 2 wrenchmanual.pdf
3 1 hammerRevision2.pdf
4 3 tapemanual.pdf


Table: Location
LocationID EquipmentID LocationName
1 1 Car
2 1 Garage
3 2 Attic

So for example, say I had two hammers and one was in my garage and the other was in my car, I would have a hammer entry in the equipment table and two entries in the location table for the car and garage. that type of hammer has a usermanual stored in the manual table as well.

I can't figure out how to get a table that will list each item in the manuals table and its location (if it has one).
What I would like to get as a result of the query would be something like this:

ManualID EquipmentName LocationName ManualFile
1 Hammer Car hammerRevision1.pdf
1 Hammer Car hammerRevision2.pdf
2 Wrench Attic wrenchmanual.pdf
3 Tape tapemanual.pdf


Can someone point me in the right direction? Right now I have something similar to this, but this will only list the equipment with a location and will ignore those without a location (the tape).

SELECT
Manual.ManualID as ManualID,
Equipment.EquipmentName as EquipmentName,
Location.LocationName as LocationName,
Manual.ManualFileName as ManualFile
From
Manual
INNER JOIN Equipment ON (Manual.EquipmentID = Equipment.EquipmentID)
INNER JOIN Location ON (Manual.EquipmentID = Location.EquipmentID)
ORDER BY
Manual.ManualID DESC

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-02 : 02:10:29
just make it left join

SELECT
Manual.ManualID as ManualID,
Equipment.EquipmentName as EquipmentName,
Location.LocationName as LocationName,
Manual.ManualFileName as ManualFile
From
Manual
INNER JOIN Equipment ON (Manual.EquipmentID = Equipment.EquipmentID)
LEFT JOIN Location ON (Manual.EquipmentID = Location.EquipmentID)
ORDER BY
Manual.ManualID DESC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-02 : 02:11:19
here's a link to understand how various joins work

http://www.w3schools.com/sql/sql_join.asp

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

false74
Starting Member

2 Posts

Posted - 2013-07-02 : 02:14:11
quote:
Originally posted by visakh16

just make it left join

SELECT
Manual.ManualID as ManualID,
Equipment.EquipmentName as EquipmentName,
Location.LocationName as LocationName,
Manual.ManualFileName as ManualFile
From
Manual
INNER JOIN Equipment ON (Manual.EquipmentID = Equipment.EquipmentID)
LEFT JOIN Location ON (Manual.EquipmentID = Location.EquipmentID)
ORDER BY
Manual.ManualID DESC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




AH, yes! I just realized! Duh, Thank you!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-02 : 02:24:37
Welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -