Domanda

I have two mysql tables: a. myTable1 and b. myTable2

First, I need to query the first table (myTable1) to get 'custid' and 'age' and then query the second table (myTable2) to get all the records that match any of the records in the intermediate result.

First query and the result look like as below:

select distinct custid,age from mtyTable1 where itemid in ( 2762 , 2763)

custid      age
"8897953"   53.1839
"1433515"   49.5507
"7638023"   59.4099
"2310899"   46.8374
"6736604"   47.3001

Now, How to write an efficient nested query to search 'myTable2' to find all the records?

È stato utile?

Soluzione 2

Try :

SELECT DISTINCT t2.*
FROM mtyTable1 t1 INNER JOIN Table2 t2 ON t1.custid=t2.custid AND t1.age=t2.age
WHERE t1.itemid IN ( 2762 , 2763)

Altri suggerimenti

JOIN the two tables:

SELECT t1.*
FROM yourTable2 t1
INNER JOIN
(
    select distinct custid,age 
    from mtyTable1 where itemid in ( 2762 , 2763)
) t2 ON t1.custid = t2.custid AND t1.age = t2.age

Or:

SELECT t1.*
FROM yourTable2 t1
INNER JOIN yourTable1 t2 ON t1.custid = t2.custid AND t1.age = t2.age
WHERE t2.itemid in ( 2762 , 2763)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top