create a sql join with more columns - sql

i want create a sql statemnt (in PL SQL Developer) with a join with comma seperated?
SELECT * FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.tab_id, second_id = t2.tab_id, second_id;
I always get a ORA-00920 Exception. If i change it to two Rows:
t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
Then i get rows.
Can some say me if i can use the first step with coma seperated columns?
Greetz

You need a valid condition:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
I think Oracle will also let you do:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON (t1.tab_id, t1.second_id) in ( (t2.tab_id, t2.second_id) );
Or even:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
USING (tab_id, second_id);
This works because the JOIN keys have the same names in the two tables.

Related

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

Oracle Query comparing two table

I have two tables T1 and T2.
T1 having records like A,B,C,D
T2 having records like A,B,D,E
Now out of the query should be C when we compare both tables as C is not available in T2
Please help here..
In Oracle, you can use the minus set operator:
select t1.*
from t1
minus
select t2.*
from t2;
You should be able to just use an inner join, this will return everything that both tables have in common, so:
SELECT T1.* FROM T1 INNER JOIN T2 ON T1.id = T2.id
Another way of achieving this would be:
SELECT
C1
FROM
T1
WHERE
C1 NOT IN (
SELECT
C1
FROM
T2
);

force table name before field in select

SELECT *
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
Both tables have a date_createField, so when I use select *, date_createField is returned twice. I could solve this by changing my select to:
SELECT T1.date_createField, T2.date_createField
FROM Table1 T1
LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK
But is it possible to not specify the specifik fields (keep select *), and force the table name in front of the property?
I'm having this problem because I'm joining 2 tables with a lot of columns and some columns have the same name. I would like to use select * and still have a distinction between columns present in both tables. Is this possible?
This is not possible you need to specify the column names or accept all columns. If this is a query you will execute often make a view which can be reused.
May this will help you
SELECT T1.*, T2.* FROM Table1 T1 LEFT OUTER JOIN Table2 T2 ON T1.CCONTACT_FK = T2 .CCONTACT_PK

How to make a proper cross join

I want to cross join the two table
Query
Select * from table1 cross join select * from table2
Above query is showing error.
What wrong in my query
Reference
The result set of the Cross Join is the number of rows in the first table multiplied by the number of rows in the second table
SELECT T1.Columns1, T1.Column2, T2.ColumnName FROM Table1 T1
CROSS JOIN Table2 t2
You must use select like this : SELECT T1.FIELD1, T2.FIELD2 FROM TABLE1 AS T1 CROSS JOIN TABLE2 AS T2 - FIELD[X] FOR EXAMPLE ONLY
Try this:
SELECT * FROM table1, table2
or this:
select * from table1 CROSS JOIN table2;

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