Skip to content
Advertisement

MySQL: How to sum on joined queries (subqueries) with multiple conditions?

I have two tables:

Table Y: indicates over what time period money is valid

Table Z

What I want:

I want the to sum up rows in Table Z based on time segments constraints (bounded) indicated in Table Y. That is, the rows that are to be summed in Table Z must have time segments within those found in Table Y (there are also additional filters on Table Z for other queries; but I don’t think that’s relevant here)

I’m very new to MySQL. I’ve tried subqueries, but think I’m missing something key; I’m not sure if this can be done without some complicated loop structure which mysql doesn’t seem well-equipped for.

Advertisement

Answer

It looks like you want to filter table z on rows whose range overlap ranges at least one range in table y.

If so, one approach is:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement