Routray
Routray

Reputation: 75

How to retrieve the data of second table (Table 2) by pointing data of another table (Table 1)

Can someone suggest/help on the below SQL query.

I have two table(Called Table 1 and Table 2), In Table 1 i have the value 11111 in ORG column, using this value i have to identify the correspondence value 1234(Which under MIDA).

MIDA have same value in both table, using this 1234(Which under MIDA, Table 1), i have to retrieve all data from Table 2 where MIDA=1234.

Query should be From Table 1 using ORG value identify the MIDA value and using MIDA value retrieve all data from Table 2 where there are same MIDA(bath Table 1 and Table 2) value.

Table:-1

PK    OFFIC     MIDA    DEPT    VER     ORG
____________________________________________

123   321       1234    AA1     1.0     11111

Table:-2

FLOW    FREE    MIDA    PRINT   TIME        ID
____________________________________________________

XYZ     A1      1234    test    2019-06-11  1.0

Upvotes: 0

Views: 85

Answers (5)

Aashish Karki
Aashish Karki

Reputation: 356

SELECT * from TABLE1 t1
INNER JOIN TABLE2 t2
  ON t1.MIDA=t2.MIDA
WHERE t1.ORG=1111

given ORG is an int value else it would be '1111'

Upvotes: 0

DarkRob
DarkRob

Reputation: 3833

You may use JOIN for your requirement. You may find this link for more info JOIN.
For your above query:-

  SELECT T2.FLOW, T2.FREE, T2.MIDA, T2.PRINT, T2.TIME, T2.ID 
  FROM Table2 AS T2 
  INNER JOIN TABLE1 AS T1 ON T1.MIDA=T2.MIDA
  WHERE T1.ORG = 1111       ------ this is optional for fetching only record of org '1111'

Upvotes: 0

foyss
foyss

Reputation: 983

An INNER JOIN answers your question;

SELECT Table2.* 
FROM Table2
INNER JOIN Table1
ON Table1.MIDA = Table2.MIDA
WHERE Table1.ORG = '11111'

Upvotes: 5

MichaelW
MichaelW

Reputation: 1

Didn't test:

SELECT * FROM Table2
WHERE MIDA IN (
SELECT MIDA FROM Table1
WHERE ORG = "YOUR ORG VALUE")

EDIT1 Format

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521419

It sounds like you just need a simple join query:

SELECT t2.*
FROM Table2 t2
INNER JOIN Table1 t2
    ON t2.MIDA = t2.MIDA
WHERE
    t1.ORG = '11111';

Upvotes: 0

Related Questions