SQL Server - Merge 3 temp table columns adjacent to each - sql-server-2008

I have created 3 temporary tables, where each temporary table holds two lines of data. But when I used the UNION command I end up having 6 lines of data, where I was expecting to have only two lines.
JFI, 1st Temp table holds 8 Fields (columns), 2nd Temp table holds 7 & 3rd Temp table holds 6.
I am looking for a solution where the 2nd & 3rd table columns/values should endup after the 1st table columns,
expected outcome to have 2 rows across 21 columns
expected columns
T1_Col1,T1_Col2,T1_Col3,T1_Col4,T1_Col5,T1_Col6,T1_Col7,T1_Col8 ,T2_Col1,T2_Col2,T2_Col3,T2_Col4,T2_Col5,T2_Col6,T2_Col7 ,T3_Col1,T3_Col2,T3_Col3,T3_Col4,T3_Col5

We should be using the JOIN Function to add the Table 2 & Table 3 columns adjacent.
Note: you need have at least 1 primary key across ALL three tables to make the join
E.g.
Select * from #Table1 T1
INNER JOIN #Table2 T2 ON T1.PrimaryKey = T2.PrimaryKey
INNER JOIN #Table3 T3 ON T1.PrimaryKey = T3.PrimaryKey

Related

How to insert a date to new column in table 2 based on product_Id and match the (product_id) in table 1 and use a column (date) to add to table 2

Table 1
Id
place
expiry_date
10
xyz
2022-09-12
Table 2 - expiry_date is the new column created in table 2. Need to fetch expiry date from table 1 where T1_id (in table 2) matches id (in table 1)
Oid
userid
expiry_date
T1_id
2
123
10
How to fetch expiry date (table 1 and fill the new column in table 2) only if the T1_id and Id(table 1) matches
Trying
insert into (sql) statements
Joins used
Join table1.Id on table2.T1_id
Insert statements won't allow you to change values from an existing table, they only allow you to add brand new rows to a table. In your case you may want to use an UPDATE statement.
In order to get matches between the two tables, you can apply a JOIN operation within the UPDATE statement, using the condition you pointed in your post description.
UPDATE tab2
INNER JOIN tab1
ON tab1.Id = tab2.T1_id
SET tab2.expiry_date = tab1.expiry_date;
Check the demo here.

Use the result of a select as a field name in a view

It is possible in mysql create a view that use the column of a table plus the result of another for explain data?
I explain what I mean with an example.
Table 1
Table 2
Table 3
I want to select the record of table 2 and use in addition of record of table 1 and insert the data of table 3, like this creating a view that update if i adding row to any table.
the number on the column is num from table3 from id1 table1 and id2 from table2

MySQL select all rows in one tables matching unique values in another table

I have 2 tables. Both contain 1 column with same value. I need to select all rows of one table if they have same value in another table.
How can do it?
I think what you are looking to do is
SELECT *
FROM table_1 INNER JOIN table_2
WHERE column.table_1 = column.table_2
This will return all the values that are in both table 1 and table 2 where the value in the column in table 1 is also in table 2

MySQL - how to write stored procedure to insert data in to table from two other tables

I have three tables (having 4 columns each) of identical columns. First column is Unique ID which is common in all tables, other three columns in two tables have data (integer) of two months.
I want to insert difference of values present in each columns of two tables against each Unique ID in third table.
Please help me to create a procedure in MySQL.
I am not clear why you would need a stored procedure for this.
insert into table1 (id,col1,col2,col3)
select t2.id,
t2.col1-t3.col1,
t2.col2-t3.col2,
t2.col3-t3.col3
from table2 t2
join table3 t3 on t3.id = t2.id;

merge two tables to update the existing table with new data

I have a table old_data and a table new_data. I want to write a select statement that give me
Rows in old_data stay there
New rows in new_data get added to old_data
unique key is id so rows with id in new_data should update existing ones in old_data
I need to write a select statement(I believe it should have joins) that would give me old_data updated with new data and new data added to it.
Example:
Table a:
id count
1 2
2 19
3 4
Table b:
id count
2 22
5 7
I need a SELECT statement that gives me
id count
1 2
2 22
3 4
5 7
You can use a join to select the data, you can use to as insert statement into a new table, but you cannot use a join to execute both insert and update statements.
Join is for doing query, not for doing insert or update.
In MySql the simplest way to do your job is:
REPLACE INTO old_data SELECT * FROM new_data;