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.
Related
I am working with somewhat weird data. I want to identify unique records with columns having many to many relationship like in example below. Column B can be null, column A is non-null:
Output should be 2 unique values. The combinations of A and B (1,A), (1,B), (1,null), (2,B) are all only a single unique entity and rest are the second unique entity.
EDIT:
My requirement is not just finding distinct values. Imagine it's like user sessions - user A can have multiple sessions, but they can log in with different log ins ( such as B) and have same session id (1) and vice versa. I want to identify unique users based on session and log in where sessions and log ins can be n:n
You could do this using SQLITE3:
SELECT DISTINCT(ColumnA || Column B)
FROM Table;
For MYSQL I think it is:
SELECT CONCAT(ColumnA, ColumnB)
FROM Table;
I'm not sure how you want to output null values. You have 2 options:
If A has Null value and B has a value, output B
If A has null value and B has a value, output nullB
For Option 1 use the code above. For Option 2, you'll have to update the columnA database to replace all null values with the text 'null'.
Is this what you want?
select distinct a, b
from t
where not exists (select 1
from t t2
where t2.id = t.id and
t2.b not in ('A', 'B')
);
This select ids that have only 'A', 'B', or NULL values in the second column.
I have a two table where I have some values in a column UniqueKeys such as:
Table 1
2016_2016-2 S2_001840_30_01
2017_2017-2 D4_002213_3_01
The problem is that I am trying to match these with table 2 Unique values where the values are written in a different order such as :
Table 2:
001840_2016-2_S2_30_D_179_364128_400985
002213_2017-2_D4_3_E_752_376901_422828
Table 1 is from a different source system and table 2 is from different one. What I am trying to achieve is create a new table TABLE 3 where when the unique values match between table 1 and table 2 then insert the data from certain columns of table 1 and 2 into table 3 or else ignore the rest.
The way the Unique values should be is the following:
Year and Period: 2016-2
Cycle : S2
Unit: 001840
Group: 30
Giving the end result in Table 3 as:
001840_2016-2_S2_30
002213_2017-2_D4_3
You need to split both input values by "_" and then recombine the parts in the way they lead to the same format. Then you can join the tables.
Use two functions, the first one for values from type table 1, the second for values from table 2.
Effekt:
SELECT ...
FROM table1
JOIN table2 ON splitfunction1(table1.Key1) = splitfunction2(table2.Key2);
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
I was surprised by the outcome of these two queries. I was expecting same from both. I have two tables that share a common field but there is not a relationship set up. The table (A) has a field EventID varchar(10) and table (B) has a field XXNumber varchar(15).
Values from table B column XXNumber are referenced in table A column EventID. Even though XXNumber can hold 15 chars, none of the 179K rows of data is longer than 10 chars.
So the requirement was:
"To avoid Duplicate table B and table A entries, if the XXNumber is contained in a table A >“Event ID” number, then it should not be counted."
To see how many common records I have I ran this query first - call it query alpha"
SELECT dbo.TableB.XXNumber FROM dbo.TableB WHERE dbo.TableB.XXNumber in
( select distinct dbo.TableA.EventId FROM dbo.TableA )
The result was 5322 rows.
The following query - call it query delta which looks like this:
SELECT DISTINCT dbo.TableB.XXNumber, dbo.TableB.EventId
FROM dbo.TableB INNER JOIN dbo.TableA ON dbo.TableB.XXNumber= dbo.TableB.EventId
haas returned 4308 rows.
Shouldn't the resulting number of rows be the same?
The WHERE ID IN () version will select all rows that match each distinct value in the list (regardless of whether you code DISTINCT indide the inner select or not - that's irrelevant). If a given value appears in the parent table more than once, you'll get multipke rows selected from the parent table for that single value found in the child table.
The INNER JOIN version will select each row from the parent table once for every successful join, so if there are 3 rows in the child table with the value, and 2 in the parent, then there will be 6 rows rows in the result for that value.
To make them "the same", add 'DISTINCT' to your main select.
To explain what you're seeing, we'd need to know more about your actual data.
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