简体   繁体   中英

Stuck on a select query

I was wondering if anyone could help me with a query that i've been struggling with.

I have 2 tables:

Prices & Visitors

Prices:

In this table the first column represents the age groups (0-4, 4-12, 12-80) with their corresponding prices

AgeGroup

0
4
12
80

Price

0
5
20
15

Visitors:

Name

Cindy
Bob

BirthDate

2001-01-28
1958-03-01

I'd like to have one select statement show me the names of the visitors and the prices that they'll have to pay.

So i'll probably need something akin to this to get their age:

year(curdate()) - year(visitors.BirthDate)

and then i'm guessing that i'll need something like this as well:

where max(Prices.AgeGroup) <= year(curdate()) - year(visitors.BirthDate)

Unfortunately that's where i'm stuck, i've already been at it for quite some time now but i still haven't been able to create the desired result yet.

Anyway if anyone could give me a helping hand, i'd very much appreciate it. :)

You can use this query.

SELECT 
visitors.name, visitors.birthdate, DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(birthdate, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(birthdate, '00-%m-%d'))   
AS age, (select price from prices where age_group < age order by age_group desc limit 1) as price
FROM 
visitors

Here's SQLFiddle for you.

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