SQL insert into table with values selected from other tables where an external value matches - sql

I have the following tables:
Table1:
id
rarity
1
Common
2
Uncommon
3
Rare
Table2:
id
Type
1
Air
2
Earth
3
Fire
4
Water
The output table already exists and the schema is the following:
rarityID
weakness_typeID
resistance_typeID
and I should fill it with rows according to the Table2 and Table1.
For example if I'm given:
type is 'Water' and 'Air'
rarity is 'Common'
I'd like to add the IDs contained in Table1 and Table2 to this table to get the following updated output table:
rarityID
weakness_typeID
resistance_typeID
1
4
1
I've written the following query:
INSERT INTO table3 (rarityID, weakness_typeID, resistance_typeID)
SELECT rar.id, weak.id, res.id
FROM table1 rar, table2 weak, table2 res
WHERE rar.rarity = `Common`
AND weak.type = `Water`
AND res.type = `Air`;
But it doesn't work, can you help me?

My understanding of your problem is that you're trying to get ids for each of your information.
If this is correct, in order to do this you need to select their ids in three separate queries like it is done in the following query:
INSERT INTO table3 (rarityID, weakness_typeID, resistance_typeID)
SELECT (SELECT rar.id
FROM table1 rar
WHERE rar.rarity = 'Common') AS rarityID,
(SELECT weak.id
FROM table2 weak
WHERE weak.type = 'Water') AS weakness_typeID,
(SELECT weak.id
FROM table2 weak
WHERE weak.type = 'Air') AS resistance_typeID;
If you want to play with this code, check this SQL Fiddle.

Related

Delete a record from a table referring to 2 or more columns from another table

Consider having 2 tables as follows.
Table 1:
Unit
SKU number
Active
A
1
Y
B
2
Y
c
3
Y
Table 2:
Unit
SKU number
description
X
4
Apple
B
2
Mango
Y
5
Grapes
z
6
Banana
I wanted to delete record B,2,Y from table 1 by referring to table 2 where values in columns Unit and SKU number match.
I tried using the following query but it didn't seem to work
DELETE FROM table1
WHERE (Unit, SKU_Number) IN (SELECT Unit, SKU_Number FROM table2);
The error was
An expression of non-boolean type specified in a context where a condition is expected, near ','
Can someone please help me understand what I am doing wrong here or help me rewrite the SQL query to achieve the required objective?
You could use similar logic with exists:
DELETE
FROM table1 t1
WHERE EXISTS (SELECT 1 FROM table2 t2
WHERE t2.Unit = t1.Unit AND t2.SKU_Number = t1.SKU_Number);
You can try using this query, assuming Unit of Table 1 is unique:
DELETE FROM table1
WHERE table1.Unit IN (
SELECT table1.Unit
FROM table1
LEFT JOIN table2 ON table1.Unit = table2.Unit
AND table1.SKU_Number = table2.SKU_Number
)
If unit is not an unique field, simply replace it with whichever field is unique, or with primary key of Table 1.
You can use inner join for delete:
DELETE t1
FROM table1 t1
INNER JOIN table2 t2
ON t1.unit=t2.unit and t1.SKU_Number = t2.SKU.Number

SQL Update using data from other tables and foreign keys

I am trying to update a column from table1 based off of data from two other tables.
Table 1 has columns id, columnIWantToUpdate, table2FK, table3FK
Table 2 has columns table2FK, table2_unique_id
Table 3 has columns table3FK, table3_unique_id
So I get table2_unique_id and table3_unique_id as inputs and I want to use the columns table2FK and table3FK to update table 1 based off of the unique_ids I received as input
One method uses filtering in the where clause:
update table1
set columnIWantToUpdate = ?
where exists (select 1
from table2 t2
where t2.table2FK = table1.table2FK
) or
exists (select 1
from table3 t3
where t3.table3FK = table1.table3FK
);
It is not clear if you want and and or for the conditions.

SQL INSERT from 1 table to another , WHERE existing columns in both tables share the same content

I have 2 tables,
Table 1 :columns = id, Category
Table 2 : columns = Category, CategoryID
I want to insert the 'id' from the Table 1 into the 'CategoryID' column in Table 2, where the contents of 'Category' in both tables are the same.
What you need is to update Table2 with the id from Table1 and you can do it with a correlated subquery:
UPDATE Table2
SET CategoryID = (SELECT id FROM Table1 WHERE Table1.Category = Table2.Category)
Depending on the database that you use, this can be done also with a join, but the syntax differs for each case.

Update a field based on the content of a field in another table

These table descriptions are vague to protect the data I am working with. I apologize in advance but i must keep the details limited. I'm using SQL Server 2008.
I have 2 tables that look like this:
Table 1:
Facility, Person, ID, Group
Table 2:
ID, Type, Date
I want to update the Group in Table 1 based on the Type in Table 2, but only where it matches a certain Facility.
I tried this:
UPDATE Table 1
SET Group = 'Big'
FROM Table 1 T1
INNER JOIN Table 2 T2 on T1.ID = T2.ID
WHERE Type = 'B' AND Facility LIKE '%Game%'`
But the result is an update to all the fields with Facility like '%Game%'.
A SELECT statement returns all the correct results. I am unsure what is wrong.
Thanks for any help.
You are very close. You just want to use the alias in the update rather than the table name:
UPDATE T1
SET Group = 'Big'
from Table 1 T1 INNER JOIN
Table 2 T2
on T1.ID = T2.ID
WHERE Type = 'B' AND Facility like '%Game%'

Microsoft Access query - not returning all data

Suppose I have the following tables:
Table1: FLP, FNAME, LNAME
Table2: FLP, Job, Company, Location
Table3: FLP, Status, Salary, Position
All are linked together by the FLP.
When I am trying to query to get all fields in all tables suppose my query is the following
Select *
From Table1, Table2, Table3
Where Table1.FLP = Table2.FLP AND Table1.FLP = Table3.FLP
However, suppose that one of the tables does not contain information or the record thus doesn't have an FLP.
For example: If i inserted into table1 johndoe1, john, doe,
and into table2 johndoe1, developer, comp, usa,
but did not insert anything into table 3 since the information is optional
my query will not fetch the result since it will fail at Table1.FLP= Table3.FLP
Sometimes table2 does not contain any information
If i tried my query as follows
Select *
From Table1, Table2, Table3
Where Table1.FLP = Table2.FLP OR Table1.FLP = Table3.FLP
then it will fetch all the results but mixes them up for example johndoe1 will have information from table3 that isn't equal to johndoe1 thus creating multiple results with different values.
If needed more explanation or the real table design please let me know.
You should use LEFT JOIN:
Select * From Table1 LEFT JOIN Table2 ON Table1.FLP = Table2.FLP LEFT JOIN Table3 ON Table1.FLP = Table3.FLP;
This will give you a null entry whenever there is no matching element in a right hand table.
See: http://msdn.microsoft.com/en-us/library/bb208894%28v=office.12%29.aspx