Copy all the values from table1 to table2 only if condition met - sql

Let's say var_Environment = QA1,QA2.
I have two tables table 1 and table 2. Table 1 has some column name var_Environment =QA1; where as table 2 does have the same column name but no values
Now my task is, I need to copy all the values from table1 to table2 only if condition meets var_Environment =QA2 How will I perform this operation ? Can I use Union All with some condition? Can someone help?

You can use INSERT INTO SELECT like below.
The WHERE clause may need adjustment, so run the the SELECT query by itself to check if the correct rows are selected.
INSERT INTO table2
SELECT var_Environment
FROM Table1
WHERE var_Environment LIKE '%QA2%'

Related

Insert column from one table to another

I want to copy a column from one table to another.
The number of rows is equal in both tables. The values I want to copy from table2 to table1 are unique. I've tried a few thing, but none so far work. My code is:
insert into alleoppdragpunkter3
select Idtall
from IDtall
Msg 2809, Level 16, State 1, Line 2
The request for procedure 'IDtall' failed because 'IDtall' is a table object.
I would like my column from table2 to be in table1.
You can try below-
insert into alleoppdragpunkter3(col1,col2,col3,....)
select col1,col2,col3,.... from IDtall
You do not copy columns between tables. You can insert rows and update columns.
Perhaps you want:
update p
set p.<col> = i.<col>
from alleoppdragpunkter3 p join
idtall i
on p.? = i.?;
The ? is for the column that specify the join conditions between the tables. The set references the column you want to update and which value to take.

SQL Merge tables with two different columns

I have two tables that look like this:
table1:
table2:
and I'd like to merge them, to create a table with a header like this:
variablenname | mean | stddev | ms_form | dependent | fvalue | probf
The first nine rows should be the first table with empty values in the last 3 columns followed by the three rows from the second table with empty values in the first 4 columns. I'm sorry I can't post pictures or a third link, but my reputation is too low. I hope it's understandable.
I tried to create a new table and select everything from both with union but that didn't work. Can anybody help me?
Thanks!
You can use ALTER to add the last 3 columns to table 1. From there, INSERT table 2 onto table 1. So something like this.
ALTER table1 ADD dependent varchar(16)
ALTER table1 ADD favalue varchar(16)
ALTER table1 ADD probf varchar(16)
INSERT INTO table1(dependent, fvalue, probf)
SELECT * FROM table2
You can use a combination of joins and unions
LEFT JOIN the two tables in first query on fields that won't have a match
then UNION the second query with a RIGHT JOIN of the same tables.
The LEFT JOIN will only show results from the first table, then the RIGHT JOIN will only show results from the second
Something Like:
SELECT * FROM table1 as t1
LEFT JOIN table1 as t2 on t1.variablenname = t2.fValue
UNION
SELECT * FROM table1 as t1
RIGHT JOIN table1 as t2 on t1.variablenname = t2.fValue;
This is how I would like the final table to look like:

How do I copy rows from one table to another in SQL?

I have been trying to copy the data listed in one table to another that are both located in the same database. However, every time I have everything entered, it runs the query and says 0 rows updated.
I have tried several variations in an attempt to get this to work. One such attempt is as listed below. I found this while researching in an attempt to get this done.
UPDATE
t1
SET
t1.column = t2.column
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.id = t2.id;
Any help on this would be greatly appreciated.
UPDATE will only modify records that already exist, if you want to insert rows that exist on another table use INSERT.
You can combine INSERT and SELECT to copy a whole table1 into a table2.
INSERT INTO table2 SELECT * FROM table1;
You can use INSERT IGNORE to copy only records that don't exist yet (based on unique keys).
You can also specify fields to copy in case the tables are different:
INSERT INTO table2 (id, first_name, last_name) SELECT id, name, surname FROM table1;

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

Select values from one table depending on referenced value in another table

I have two tables in my SQLite Database (dummy names):
Table 1: FileID F_Property1 F_Property2 ...
Table 2: PointID ForeignKey(fileid) P_Property1 P_Property2 ...
The entries in Table2 all have a foreign key column that references an entry in Table1.
I now would like to select entries from Table2 where for example F_Property1 of the referenced file in Table1 has a specific value.
I tried something naive:
select * from Table2 where fileid=(select FileID from Table1 where F_Property1 > 1)
Now this actually works..kind of. It selects a correct file id from Table1 and returns entries from Table2 with this ID. But it only uses the first returned ID. What I need it to do is basically connect the returned IDs from the inner select by OR so it returns data for all the IDs.
How can I do this? I think it is some kind of cross-table-query like what is asked here What is the proper syntax for a cross-table SQL query? but these answers contain no explaination of what they are actually doing so I'm struggeling with any implementation.
They are using JOIN statements, but wouldn't this mix entries from Table1 and Table2 together while only checking matching IDs in both tables? At least that is how I understand this http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
As you may have noticed from the style, I'm very new to using databases in general, so please forgive me if not everything is clear about what I want. Please leave a comment and I will try to improve the question if neccessary.
The = operator compares a single value against another, so it is assumed that the subquery returns only a single row.
To check whether a (column) value is in a set of values, use IN:
SELECT *
FROM Table2
WHERE fileid IN (SELECT FileID
FROM Table1
WHERE F_Property1 > 1)
The way joins work is not by "mixing" the data, but sort of combining them based on the key.
In your case (I am assuming the key field in Table 1 is unique), if you join those two tables on the primary key field, you will end up with all the entries in table2 plus all corresponding fields from table1. If you were doing this:
select * from table1, table2 where table1.fieldID=table2.foreignkey;
then, providing your key fields are set up right, you will end up with the following:
PointID ForeignKey(fileid) P_Property1 P_Property2 FileID F_Property1 F_Property2
The field values from table1 would be from matching rows.
Now, if you do this:
select table1.* from table 1, table2 where
table1.fieldID=table2.foreignkey and F_Property1>1;
Would essentially get the same set of records, but will only show the columns from the second table, and only those that satisfy the where condition for the first one.
Hope this helps :)
If I understood your question correctly this will get the job done.
Select t2.*
from table1 t1
inner join table2 t2 on t2.id = t1.id
where t1.Prop = 'SomeValue'