Combining 2 SQL Tables - sql

I have the following 2 tables:
temp_table w/ columns MAIN_AUTHOR, COLLAB_NAME, COLLAB_ID, RESEARCH_ID
people_person w/ columns id, MAIN_AUTHOR, COLLAB_NAME, COLLAB_ID, RESEARCH_ID
temp_table contains data, people_person does not. I want to put all the rows from temp_table into the corresponding columns in people_person. Ive tried different joins but I dont know what kind of join to use/ how to do it. Thanks

I assume your id column in people_person is auto-increment. So
insert into people_person (MAIN_AUTHOR, COLLAB_NAME, COLLAB_ID, RESEARCH_ID)
select MAIN_AUTHOR, COLLAB_NAME, COLLAB_ID, RESEARCH_ID
from temp_table

Related

Suggestion to make a massive insert

I have the tables below:
tb_profile tb_mbx tb_profile_mbx tb_profile_cd
id id id_profile id_perfil
cod_mat mbx id_mbx id_cd (matches id_mbx)
concil bp
masc
I need to create a query that when validating that the id_cd 1,2,4,5
and 6 exists in tb_profile_cd, perform an insert in the
tb_profile_mbx table with the cod_matrix parameters of the tb_profile
table.
Remembering that each concil has its ID in the tb_mbx table and a
concil has many cod_mat.
Another point is that the concil id_mbx represents the id_cd of the
tb_profile_cd table.
One more point is that as I said above, that a concil has many
cod_mat. I have around 20 thousand records for each concil.
For my need, try to consult the query below, but Oracle returned an error:
insert into tb_profile_mbx values (seq_profile_mbx.nextval,
(select id from tb_profile where concil like '%NEXXERA%')
,(select id from tb_mbx where mbx like '%NEXXERA%')
,null
,null);
ORA-01427: single line subquery returns more than one line
Would there be another way to do this query?
Thanks in advance!
You can insert all matching combinations of matches using:
insert into tb_profile_mbx (id_profile, id_mbx)
select p.id, m.id
from tb_profile p join
tb_mbx m
on p.concil like '%NEXXERA%' and m.mbx like '%NEXXERA%';
I would recommend running the select to see if it returns the values you want.
You only show two columns for tb_profile_mbx, so I've only included two columns in the insert.

How to retrieve rows from two SQL tables

Let's say we have two tables as follows:
Table A: [ID, CarName] with one row (1, 'Fiat')
Table B: [ID, FirstName] with one row (1, 'Super Man')
My question is: what kind of query that I can run in order to return rows from the tables A and B while there is no connection between them?
The returned rows will be:
#1 (1,'Fiat')
#2 (1,'Super Man')
Thanks
Works if both of the tables have the same number of columns:
select
*
from table1
union all
select
*
from table2
;
If the number of columns in the table is different, you need to explicitly call missing columns in the table with fewer columns as NULL (select A, B, null from table 1;)
But it would probably be better if you get the tables to have a common key to join them on.

I want to combine/union two tables and have it create a field that identifies which table it came from

I want to combine/union two tables and have it create a field that identifies which table it came from. I saw an answer for SQL but I do not need max values. I just need to Union two tables. Here is my current SQL for a union query I made through access' query.
SELECT [TableA].[1As], [TableA].[2As]
UNION
SELECT [TableB].[1As], [TableA].[2As];
I want the tables to create whichtabl field and populate whatever word I tell it based on the the table it came from to look like this:
Fields: 1As 2As WhichTabl
data: 100 1 TableA
110 0 TableB
thanks in advanced! Please excuse me I am a newbie!
You can just add in the column as a constant:
SELECT "TableA" as which, [TableA].[1As], [TableA].[2As]
FROM TableA
UNION ALL
SELECT "TableB", [TableB].[1As], [TableB].[2As]
FROM TableB

SQL query that merges two tables together with a where clause

Hi I want to search through table 2 with a value (names CustID) from table 1, and if all the values are found in table 2 (CustID) that matches table 1 (CustID) the values must be shown together with all the values from table 1 that does not match table 2's values.
Is it possible to do it this way and if it is can you please show me how, I need this for a project.
Thanks in advance
It sounds like you want a "LEFT" JOIN, which will pull up all records in table1 plus the matching ones in table2.
SELECT A.,B. FROM table1 A LEFT JOIN table2 B ON A.CustID = B.CustID

Normalizing a table, from one to the other

I'm trying to normalize a mysql database....
I currently have a table that contains 11 columns for "categories". The first column is a user_id and the other 10 are category_id_1 - category_id_10. Some rows may only contain a category_id up to category_id_1 and the rest might be NULL.
I then have a table that has 2 columns, user_id and category_id...
What is the best way to transfer all of the data into separate rows in table 2 without adding a row for columns that are NULL in table 1?
thanks!
You can create a single query to do all the work, it just takes a bit of copy and pasting, and adjusting the column name:
INSERT INTO table2
SELECT * FROM (
SELECT user_id, category_id_1 AS category_id FROM table1
UNION ALL
SELECT user_id, category_id_2 FROM table1
UNION ALL
SELECT user_id, category_id_3 FROM table1
) AS T
WHERE category_id IS NOT NULL;
Since you only have to do this 10 times, and you can throw the code away when you are finished, I would think that this is the easiest way.
One table for users:
users(id, name, username, etc)
One for categories:
categories(id, category_name)
One to link the two, including any extra information you might want on that join.
categories_users(user_id, category_id)
-- or with extra information --
categories_users(user_id, category_id, date_created, notes)
To transfer the data across to the link table would be a case of writing a series of SQL INSERT statements. There's probably some awesome way to do it in one go, but since there's only 11 categories, just copy-and-paste IMO:
INSERT INTO categories_users
SELECT user_id, 1
FROM old_categories
WHERE category_1 IS NOT NULL