문제

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?

도움이 되었습니까?

해결책 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)

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top