I have 2 tables in MySQL: city & user
here is the user table
city | date | user |
---|---|---|
jkt | 2021-10-15 | 10 |
jkt | 2021-10-16 | 3 |
jkt | 2021-10-17 | 0 |
jkt | 2021-10-18 | 9 |
jkt | 2021-10-19 | 15 |
jkt | 2021-10-20 | 15 |
jkt | 2021-10-21 | 15 |
jbi | 2021-10-15 | 12 |
jbi | 2021-10-16 | 4 |
jbi | 2021-10-17 | 5 |
jbi | 2021-10-18 | 2 |
jbi | 2021-10-19 | 8 |
jbi | 2021-10-20 | 13 |
jbi | 2021-10-21 | 10 |
and the city table city | start_date | -----| ---------------| jkt | 2021-10-17 | jbi | 2021-10-19 |
I would like to select user & city with start date base on start_date at city table. this is an example result.
city | date | user |
---|---|---|
jkt | 2021-10-17 | 0 |
jkt | 2021-10-18 | 9 |
jkt | 2021-10-19 | 15 |
jkt | 2021-10-20 | 15 |
jkt | 2021-10-21 | 15 |
jbi | 2021-10-19 | 8 |
jbi | 2021-10-20 | 13 |
jbi | 2021-10-21 | 10 |
I already query select city.city, date, user from city inner join user on city.city=user.city
but stils I have no idea how to create condition with start date due to each city have different start date.
CodePudding user response:
You need to use the start date of the city dynamically:
select city.city, `date`, `user`
from city
join `user`
on city.city = `user`.city and
`user`.`date` >= city.start_date