Can a table get other table column data?

I need to create a select statement in which the statement should retrieve data from other data in a table column.

eg.

   Table1              Table2 
   id                  id2
   age                 age2

Select id, age from table 1, where id = id2

It is possible.

+4
source share
3 answers

you can use INNER JOIN

SELECT 
T1.id,
T1.age
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.id = T2.id2

DEMO using INNER JOIN

you can use EXISTS

SELECT 
T1.id,
T1.age
FROM Table1 AS T1
WHERE EXISTS(
  SELECT 1 
  FROM Table2 AS T2
  WHERE T2.id2 = T1.id
);

you can use IN

SELECT 
T1.id,
T1.age
FROM Table1 AS T1
WHERE T1.id IN (SELECT T2.id2 FROM Table2 AS T2)

Note:

In a working demo, the output consists of two lines. There tabel1are two entries in and three entries in table2. But between these two tables, only two matching records were found. Therefore, the output consists of only two lines.

+4
source

, . JOIN, JOIN. SQL JOINs.

0
SELECT id ,age 
 FROM TABLE 1
WHERE id IN (SELECT id2 FROM TABLE2);

OR

SELECT id ,age 
 FROM TABLE1 , TABLE2
WHERE id = id2 ; 

OR

SELECT id ,age 
 FROM TABLE 1 , (SELECT id2 FROM TABLE2) TBL2
WHERE id = TBL2.id2 ;
0
source

Source: https://habr.com/ru/post/1649115/


All Articles