Combining query results of two different tables - sql

First select query
Table 1:
ID Value
131 ABC
120 DEF
Second select query
Table 2:
ID
120
131
I want to write a single query which will fetch me combining two tables (the required output)
ID Value
120 DEF
131 ABC
Note: if there is no entry in Table2, return the data from Table1 else combine and return the result.
Any thoughts? Thanks.

SELECT *
FROM table1 LEFT JOIN table2
ON table1.ID = table2.ID
if it can't find the matching record in table2 it will fill table2 columns in the result set with null

SELECT * FROM Table1,Table2 WHERE Table1.ID = Table2.ID

select t1.id, value
from table1 t1, table2 t2
where t1.id = t2.id

something like that:
SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.ID

Related

Trying to get value from other table using any in SQL

My query is:
SELECT *
FROM table1
RIGHT JOIN table2 on table1.ID = any(table2.parents)
WHERE table1.ID = 1
My first table is:
ID | desc
123 | Data123
231 | Data231
My second table is:
ID | parents
1 | {123,231}
2 | {123}
But this query isn't working. I am trying to get the data from both 123 and 231, how do I fix this?
any(table1.parents)
parents doesn't exists in table1.
Try to change places
SELECT *
FROM table1
RIGHT JOIN table2 on table1.ID = any(table2.parents)
WHERE table2.ID = 1
You didn't specify what exactly you want, but if you just want to find rows in table1 where the IDs are present in table2.parents, the following should do it:
select t1.*
from table1 t1
where exists (select *
from table2 t2
where t1.id = any(t2.parents))
order by t1.id;
If you want information from table2 as well you need a join (and I don't think you want an outer join):
select *
from table1 t1
join table2 t2 on t1.id = any(t2.parents)
order by t1.id
Due to the nature of a join that would return the row (123,Data123) twice - don't know if you want that or not.
Example: http://rextester.com/PUOL76353

SQL Server - Return all records from LEFT table and only non matching records from right table

I have 2 tables with the same structure (field names). Table1 and Table2.
I need to return all records from Table1 and only records from Table2 that do not match/join to a record in Table1.
Table2 has more records than Table1.
I am joining the 2 tables on 3 fields.
So basically I want all records from table1 returned and only records that don't have a match (joining on the 3 fields) to table1 from table2 returned.
Put another way, Table1 records take precedence over table2 records in my final result output when the records exist in both tables (same value for the 3 fields)
I started writing something like the below but I don't think it will work. Should I use a left outer join instead?
Select * from table1 t1
left join table2 t2 on t1.id = t2.id and t1.date = t2.date and t1.custid= t2.custid
where t2.id is null or t2.date is null or t2.custid is null
So, you need every row from table1 plus the rows from table2 that don't match with table1?:
SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2 t2
WHERE NOT EXISTS(SELECT * FROM table1
WHERE id = t2.id
AND date = t2.date
AND custid = t2.custid);
Select * from table1 t1
Union
Select * from table2 t2
Where Not exists
(Select * from table1
Where id = t1.id
and date = t1.date
and custid= t1.custid)

query in sql server 2005

there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...
SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2

mysql join two tables

I have a problem joining two tables:
table1
id name
1 aaa
2 bbb
3 ccc
table2
id table1_id name
1 1 x1
2 1 x2
3 2 s1
table1 is the main table, table2 contains attributes.
I need to join and search both tables, but display distinct results from first table.
When using JOIN I get multiple results from table2.
The scenario is I need to search main table TABLE1 and ALL ATTRIBUTES in TABLE2 and return if found
select distinct(name) from table1 inner join table2 on table1.id = table2.table1_id where table2.name = x2;
Should do the trick.
If you need entries which exists in both tables:
SELECT * from Table1 t1
WHERE YourConditionsHere
AND EXISTS (SELECT 1 from Table2 t2
WHERE t1.Id = t2.Table1_id
AND YourConditionsHere)
if you need entries from Table1 for which does not exists enteries in Table2
SELECT * from Table1 t1
LEFT JOIN
(SELECT * from Table2
WHERE YourConditionsHere
) t2
ON (t1.Id = t2.Table1_id)
WHERE YourConditionsHereForTable1
another option
select * from table1 t1 where t1.id in (select table1_id from table2 t2 where t2.name = "x1");
it's probably best to check query plains (i.e. EXPLAIN) for all suggested queries and check the one that performs best for your exact scenario.

SQL Query: select all the records from table1 which exist in table2 + all the records from table2 which don't exist in table1

consider the following example.
I have to select all the records from table1 which exist in table2 also + all the records from table2 which don't exist in table1 but exist in table2 and have IsActive=1 and status not null.
I initially tried it with a join but how to do the later part where I have to select the records which don't exist in table 1 ? I have to do it inside a single query presumably with a SQL view.
Edit
I need to combine the results like a UNION of 2 tables, so incase of rows absent in table1 but present in table2, the columns of belonging to table1 would be blank.
Here's an example query:
select *
from Table2 t2
left join
Table1 t1
on t1.id = t2.id
where t1.id is not null
or (isActive = 1 and status is not null)
The first line of the where clause takes care of "all the records from table1 which exist in table2". The second line is for "don't exist in table1 but exist in table2 and have IsActive=1 and status not null".
You will need an outer join here.
http://msdn.microsoft.com/en-us/library/ms187518.aspx
Is this it? Not sure if I got right what you want to do.
SELECT
*
FROM
Table1 t1
JOIN
Table2 t2 ON (t1.ID = t2.ID OR (t1.ID IS NULL AND t2.isActive = 1 AND t2.Status IS NOT NULL))