puks1978
puks1978

Reputation: 3697

MySQL Select Query - Limit not record count, but type of returned result

I am wanting to produce a query which allows me to select multiple records depending on the previous available records. Let me explain.

I have a table which holds 100 records with 3 different clients and region dependent. i.e.

Record 1 belongs to client 1 and appears in regions: 1, 2, 3
Record 2 belongs to client 1 and appears in regions: 1, 2
Record 3 belongs to client 2 and appears in regions: 1, 2
Record 4 belongs to client 3 and appears in regions: 2, 3

Now, if I enter region 1 as my option to search, I would see record 1, 2, 3. If I enter region 2 as my option I would like to see records 1, 2, 3 but not record 4 because I already have 2 other clients.

Now, if I enter region 3 as my option I would like to have returned record 1, 4.

I basically want to do a limit on the result but not on the amount of records returned, but on the different regions. So show all records that exist for the first 2 available regions.

Hope this can be done.

Cheers

Upvotes: 0

Views: 310

Answers (1)

Amber
Amber

Reputation: 527193

It would probably be simpler to do this as two queries:

  1. Select unique client ids within the region you're looking at, and limit to the first N of them (where N is the most you want).
  2. Select all of the records with one of the selected client ids plus the desired region ID.

Upvotes: 2

Related Questions