简体   繁体   中英

Selecting multiple rows based on specific categories (mysql)

I don't think this is a duplicate posting because I've looked around and this seems a bit more specific than whats already been asked (but I could be wrong).

I have 4 tables and one of them is just a lookup table

SELECT exercises.id as exid, name, sets, reps, type, movement, categories.id
FROM exercises
INNER JOIN exercisecategory ON exercises.id = exerciseid
INNER JOIN categories ON categoryid = categories.id
INNER JOIN workoutcategory ON workoutid = workoutcategory.id
WHERE (workoutcategory.id = '$workouttypeid')
AND rand_id > UNIX_TIMESTAMP() 
ORDER BY rand_id ASC LIMIT 6;

exercises table contains a list of exercise names, sets, reps, and an id

categories table contains an id, musclegroup, and type of movement

workoutcategory table contains an id, and a more specific motion (ie: upper body push, or upper body pull)

exercisecategory table is the lookup table that contains (and matches the id's) for exerciseid, categoryid, and workoutid

I've also added a column to the exercises table that generates a random number upon entering the row in the database. This number is then updated only for the specified category when it is called, and then sorted and displays the ascending order of the top 6 listings. This generates a nice random entry for me. (Found that solution elsewhere here on SO).

This works fine for generating 6 random exercises from a specific top level category. But I'd like to drill down further. Here's an example...

select all rows inside categoryid 4

then still within the category 4 results, find all that have movementid 2, and then find one entry with a typeid 1, then another for typeid 2, etc

TLDR; Basically there's a few levels of categories and I'm looking to select a few from here and a few from there and they're all within this top level. I'm thinking this could all be executed within more than one query but im not sure how... in the end I'm looking to end with one array of the randomized entries.

Sorry for the long read, its the best explanation I've got.

Just realized I never came back to this posting...

I ended up using several mysql queries within a switch based on what is needed during the request. Worked out perfectly.

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