How do I get data from another table? - sql

I have a database with two tables, table 'product' and table 'factory'.
The table product has a column called factory_id and the table factory has both factory_id and 'factory_name'.
I have a bootstrap table which outputs all the fields the product table has, but how do I get the factory_name in there? I know I have to join tables but I don't really know how.

SELECT product.*, factory.factory_name
FROM product
LEFT JOIN factory ON product.factory_id = factory.factory_id;
This should work, but next time try including some code you tried yourself when you ask a question.

Related

Refer to different id's within the same query in SQL

I need to brind a lot of columns from several tables using LEFT JOIN. My starting point is the orders table and I bring the vendor name from the "Address_table". Then I add another table with order details and then the shipping information of each order detail.
My problem is that I need to bring a different record from "Address_table" to refer onether id's detailed in shipment table as of "origin_id" and "destination_id".
In other words, "address_id", "origin_id" and "destination_id" are all records from "Address_table". I brought the first one related to the vendor, how can I retrieve the other two?
Example
Thanks in advance
Your question is not exactly clear in terms of the tables and their relationships. It is, however, clear what the problem is. You need to join against the same table twice using different columns.
In order to do that you need to use table aliases. For example, you can do:
select *
from shipment s
left join address_table a on a.address_id = s.origin_id
left join address_table b on b.address_id = s.destination_id
In this example the table address_table is joined twice against the table shipment; the first time we use a as an alias, the second time b. This way you can differentiate how to pick the right columns and make the joins work as you need them to.

Update a field from one table to another, involving a 3 table join

I have a table I need to update the price field in. I need to update this field from a different price field from a different table. The only way I can get to the required table for the update is by joining another table into this query.
So in all I need to join 3 tables in the update.
Table A has the price field that needs to be updated. In this table I have the foreign key of the product.
Table A structure
-PK_TABLE_A,
-FK_TABLE_B,
-ITEM_COST,
-ITEM_PRICE (needs to be updated from Table C)
Table B is the product table which has the PK of the product. This table is used to access Table C.
I also need to filter Table B to only update a certain stock type.
Table B structure
-PK_TABLE_B,
-FK_TABLE_C,
-DESCRIPTION,
-QUANTITY,
-ITEM_TYPE (a string that needs to be added in where clause to only update records with certain type).
Table C has a foreign key back to Table B. It also contains the price field that I need to use to update the price field in table A
Table C structure
-PK_TABLE_C,
-FK_TABLE_B,
-PRICE (this is the field that I need to use to update the price field in table A)
-USED_DATE,
-ID
The DBMS I am using is Firebird.
I have tried to use sub queries to no avail. I regularly use a sub-select when using two tables to update, so something like
UPDATE table1 AS t1
SET t1.FK = (select table2.PK
FROM table2 INNER JOIN
table1
ON table2.FK = table2.PK
WHERE table2.name = t1.name)
I'm just struggling to use the same technique with a 3rd table incorporated.
I am not even sure if this is the correct way to go about this situation. I have looked on google, but most examples I have come across don't utilise the 3rd table.
Any help would be appreciated.
**edited to included more detail on table structure.
are you able to show us the table structures in more detail?
if both tableA and tableC have a foreign key that points back to tableB I don't think you need to include a three table join. you just need to
update tableA set ITEM_PRICE = SELECT(PRICE FROM TableC WHERE
TableA.FK_TABLE_B = TableC.FK_TABLE_B;
unless I'm missing something?
edited to reflect a better understanding of the problem
alright, I think I've got it this time:
update tableA set price =
(select price from tableC where tableA.fk_tableB = tableC.fk_tableB) where
(Select item_type from tableB where tableB.pk_tableB = tableA.fk_tableB) =
'$itemTypeVariable';
edited again with a better understanding of the problem

SQL multiple joins error

I am trying to create a temporary table with information from the orders table in. I then want to join stock.name, and most of the supplier information using the supplier ID which is the foreign key in the orders table. This is my code below, but I seem to get an error in access.
This query will be run in VB to create a temporary table to pass over data to excel. The table should only store one record but should be deleted soon after excel takes the data from it. Thanks in advance to anyone that can help me.
SELECT orders.purchase_order_number, orders.part_number, stock.name, orders.quantity, orders.order_date, suppliers.last_name, suppliers.first_name, suppliers.address_1, suppliers.address_2, suppliers.city, suppliers.postcode
INTO order_temp FROM orders
INNER JOIN suppliers ON orders.supplier_ID = suppliers.supplier_ID
INNER JOIN stock ON orders.part_number = stock.part_number
WHERE orders.purchase_order_number = 'PO10367' and suppliers.supplier_ID = 20
If you are querying from VB (such as VB.net), then when you query to your LOCAL VB app from the connection, you do not need to select into table... The error you are probably getting is that the table already exists and can't recreate it, it must be dropped... But if you just query to VB app, and no table created, you should be good to go.

Need help joining two tables by guid then adding values across columns by row

I have two tables.
One table stores items and their cost and are stored uniquely with their own guid.
ItemCost Table
Item_ID ItemCategory Item Cost
x-xx-x Computer Laptop 400.00
The other table stores a work space that is a collection of these items, best illustrated.
Work Space Table
WorkSpace_ID Workspace_Name Item_Category_1 Item_Category_2 and so on....
xx-xx-xx Workspace A xx-xx-xx (the guid from the first table)
Now I need some way to join the cost from the ItemCost table to the guid's they represent in the Work Space table, then, add them up across the columns to give a total cost of the work space. Some Item_Category columns in the Work Space table will be null.
Fairly new at SQL, kind of learning by fire here. If there's a better way to structure this, I'm open for suggestion.
Thanks.
Well part of your problem is the structure of the Workspace table. This table is not normalized.
In you current design you will need to unpivot or join on the workspace table multiple times to get the result.
Current Structure Query will be:
select *
from itemcost i
left join
(
select workspace_id,
worksapce_name,
item_category_1 ItemCategory
from workspace
union all
select workspace_id,
worksapce_name,
item_category_2
from workspace
) w
on i.Item_ID = w.ItemCategory
Typically you would have a Workspace table and a separate table to link the ItemCost to the Workspace called workspace_items:
create table workspace
(
workspace_id,
Workspace_Name
);
create table workspace_items
(
workspace_id,
Item_ID
);
This will make the joining on the table much easier. This structure allows you to have multiple item categories assigned to each workspace. If you alter your table structure, then your query will be:
select *
from itemcost i
left join workspace_items wo
on i.item_id = wi.item_id
left join workspace w
on wi.workspace_id = w.workspace_id
You need to create another many to many table between WorkSpaces and Items (and remove the Item columns from the Workspace table):
WorkSpaceItems Table:
WorkSpace_ID -- ItemID
Then it's easy to query all the WorkSpaceItems, join that to the Items table and sum that up.
Here's a link to schema and sample query: http://www.sqlfiddle.com/#!3/35336/7

MS Access Selecting Related Rows

I have 2 tables with a many-to-any relationship. For the example we will call the tables "Guys" and Girls" There is a junction table that contains the related primary keys...who has dated who.
If I want to find all the girls that Guy 1 has dated, I do a select on the junction table selecting all girls with guys.ID. This give me a RecordSet. Now to find the names of the girls, I need to select from the girls table a row using the key from each RecordSet row.
Isn't there an easier way? Since I've defined the relationships in Access I would think that there must be a way to build a single query. How do I do that?
SELECT girls.name
FROM (guys
INNER JOIN junct ON guys.guyID = junct.guyID)
INNER JOIN girls ON junct.girlID = girls.girlID
WHERE guys.guyID = [whatever id you're looking for]