简体   繁体   中英

How Do I Join Tables to Return a Specific Number of People Per Team Who Fit Certain Criteria?

This is a pretty specific one:

I have two tables ( t1 and t2 ) -- t1 is my all-person table, where everybody in my database and all their data is housed, and t2 is my much smaller table of people who are actually going to do the work of talking to the people in t1 .

As you can see in this sample SQL Fiddle , the people in t1 each have specific criteria assigned to them (age, rating, and team). My end-result will hopefully be: for every one worker in t2 , the query will return 2 specific people from t1 if their teams match (the idea behind this is that I'm matching the workers in t2 to the person they're going to talk to in t1 based on their team).

But what makes it trickier is that there are two more sets of criteria that I want the query to satisfy also:

  1. Only return people from t1 if their age is in between 30 and 60 (everybody outside of those age-ranges should be ignored) --
  2. And if there's more than two people that fit the above criteria, return the ones with the best rating first. (For example, if there are four people on a team and we only need two -- so the query should return the two with the best ratings, which would be whichever ratings closer to 100.)

The final thing that is difficult to wrap my head around is that there are multiple workers per team as well -- so if there's two t2 workers on 'Team A', the query should return four distinct t1 people on Team A: two attached to one worker and two attached to another (and like I said above, they should be the four best rated people [though it doesn't matter which two people goes to which worker).

My hopeful output will look something like the following for all teams:

ID (t1)   Person (t1)   Team (t1)   Worker (t2)
539184    Smith, Jane   Team A      Smith, Bob
539186    Smith, Jim    Team A      Smith, Bob
537141    Smith, Danny  Team A      Smith, Bill
537162    Smith, James  Team A      Smith, Bill
Etc.

In reality, I'm doing something similar to this with tens of thousands of records -- which is why this is the only way I can imagine doing it, but I barely even know where to start. Any help would be greatly appreciated, and I'll add any additional information that would be helpful!

The SQL fiddle did not work. But still going ahead with the query format :)

SET @rank:=1, @curr =  0;    

SELECT * FROM(
SELECT @rank := if(@curr = t1.id,  @rank+1, 1 ) as rank,@curr := t1.id as curr, <field_list>
FROM t1
INNER JOIN t2 on t1.id = t2.ref_id
WHERE t2.age BETWEEN 30 AND 60 < AND "whatever">
ORDER BY t1.id, t2.ranking desc
) t WHERE rank <= 2 ;

replace with fields you want to select like name, id, gender < AND "whatever"> replace with all conditions you have like "ranking > 10 " etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM