Getting data from link tables
Just a quick question regarding SQL to get data from tables which are linked together. I'll try to do my best in describing the sitiuation to make it clear.
Right, I want to get data from 2 fields in a table called GPS (lon & lat). Each lon & lat value is assigned to one ship (can be null). The following tables are involved in this problem:
Ordersonship (ShipID, OrderID)
OrderID(OrderID, CustomerID, Date, ...)
ShipID(ShipID, Name, Docked...)
GPS(ShipID, lat, lon, date)
All tables are linked together. On my web form I have a drop down box which makes a OrderID selectable in the Ordersonship table (since not all orders have been assigned). I need to write a SQL command to get the lat & lon from the GPS table. First of all is this possible? And is the following SQL commands going the right way about it?
SELECT lat
FROM GPS g, ShipID s, Ordersonship o
Where o.OrderID = @SelectedOrderID
AND s.ShipID = g.ShipID
comm.Parameters.Add("@SelectedOrderID", System.Data.SqlDbType.Int);
comm.Parameters["@SelectedOrderID"].Value =
Convert.ToInt32(SelectedOrderDropDownBox.SelectedV alue);
SELECT lon
FROM GPS g, ShipID s, Ordersonship o
Where o.OrderID = @SelectedOrderID
AND s.ShipID = g.ShipID
comm.Parameters.Add("@SelectedOrderID", System.Data.SqlDbType.Int);
comm.Parameters["@SelectedOrderID"].Value =
Convert.ToInt32(SelectedOrderDropDownBox.SelectedV alue);
Would there be a better way of writing this, instead of the 2 commands? I am I even looking in the right direction?
Many thanks for any help, I hope I have explained it well enough :S
Regards,
Peter
|