sql - MySQL get the nearest future date to given date, from the dates located in different table having Common ID -


i have 2 tables - client , banquet

client table ---------------------------- id     name 1      john 2      jigar 3      jiten  ---------------------------- banquet table ---------------------------- id     client_id   dated     1      1           2016.2.3 2      2           2016.2.5 3      2           2016.2.8 4      3           2016.2.6 5      1           2016.2.9 6      2           2016.2.5 7      2           2016.2.8 8      3           2016.2.6 9      1           2016.2.7  ---------------------------- :::::::::: **required result** ---------------------------- id     name          dated 2      jigar         2016.2.5 3      jiten         2016.2.6 1      john          2016.2.7 

the result generated such that

1. date future : closest or equal current date, further related respective client should filtered , ordered in format given in required result

curdate() current case 5.2.2016

failed: query logic 1

select c.id, c.name, b.dated client c, banquet b c.id = b.client_id , b.dated >= curdate() order (b.dated - curdate());  ------------------------------------------- output id     name          dated 2      jigar         2016.2.5 2      jigar         2016.2.5 3      jiten         2016.2.6 3      jiten         2016.2.6 1      john          2016.2.7 2      jigar         2016.2.8 2      jigar         2016.2.8 1      john          2016.2.9 

failed: query logic 2

select c.id, c.name, b.dated client c, banquet b    b.dated = (        select min(b.dated)        banquet b        b.client_id = c.id            , b.dated >= curdate()    ) order (b.dated - curdate());  ------------------------------------------- output id     name          dated 2      jigar         2016.2.5 2      jigar         2016.2.5 3      jiten         2016.2.6 3      jiten         2016.2.6 1      john          2016.2.7 

sqlfiddle

update : further result generated such that

2. clients without : dated should listed : may null

3. information other dated in banquet table need listed

updated required result

id     name          dated        meal 2      jigar         2016.2.5     lunch 3      jiten         2016.2.6     breakfast 1      john          2016.2.7     dinner 4      junior        -            - 5      master        -            supper 

for query, suggest applying where condition >= curdate() , select min(dated) group client_id:

select b.client_id, min(b.dated) banquet b b.dated >= curdate() group b.client_id; 

from this, can add necessary join client table client name:

select b.client_id, c.name, min(b.dated) banquet b inner join client c on c.id = b.client_id b.dated >= curdate() group b.client_id; 

sqlfiddle: http://sqlfiddle.com/#!9/aded8/18

edited reflect new parts of question:

based on new info added - asking how handle nulls , 'meal' column, i've made changes. updated query handles possible null values (by adjusting clause) in dated, , includes meal information.

select b.client_id, c.name,  min(b.dated) dated, ifnull(b.meal, '-') meal banquet b inner join client c on c.id = b.client_id b.dated >= curdate() or b.dated null group b.client_id; 

or can take of , combine gordon linoff's answer, sounds perform better overall.

new sqlfiddle: http://sqlfiddle.com/#!9/a4055/2


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -