mysql schedule query date table and user table -
i trying make schedule in 1 simple query
i not have slightest idea how this.
i have tried left join , count figured maybe there more efficient way doing this.
so came on here.
any appreciated, thank you.
one table has dates -
04-01-2017 04-02-2017 04-03-2017
then next table has names
john ben matt billy bob susan
what want try query out -
date - p1 p2 04-01-2017 john ben 04-01-2017 billy matt 04-01-2017 susan bob 04-02-2017 john matt 04-02-2017 billy bob 04-02-2017 susan matt 04-03-17 etc...
users can not repeat each other
create table `name` ( `id` int(11) not null auto_increment, `names` varchar(45) default null, primary key (`id`) ) engine=innodb auto_increment=7 default charset=utf8; insert `name` values (1,'john'),(2,'ben'),(3,'matt'),(4,'billy'), (5,'bob'),(6,'susan'); create table `schedule_dates` ( `id` int(11) not null auto_increment, `date` varchar(255) default null, primary key (`id`) ) engine=innodb auto_increment=26 default charset=utf8; insert `schedule_dates` values (1,'9/23/2017'),(2,'9/30/2017'),(3,'10/7/2017'),(4,'10/14/2017'),(5,'10/21/2017'),(6,'11/4/2017'),(7,'11/11/2017'),(8,'11/18/2017'),(9,'11/25/2017'),(10,'12/2/2017'),(11,'12/9/2017'),(12,'12/16/2017'),(13,'12/23/2017'),(14,'12/30/2017'),(15,'1/6/2018'),(16,'1/13/2018'),(17,'1/20/2018'),(18,'1/27/2018'),(19,'2/3/2018'),(20,'2/10/2018'),(21,'2/17/2018'),(22,'2/24/2018'),(23,'3/3/2018'),(24,'3/10/2018');
if have table of names id
values, can result set showing distinct pairs of names using self join (http://sqlfiddle.com/#!9/4a5487/1/0)
select a.name, b.name names join names b on a.id > b.id
the on a.id > b.id
clause builds list won't show manny | jack
if shows jack | manny
. if want both in list not jack | jack
use on a.id <> b.id
putting distinct date each row of result set little harder in mysql, because doesn't have sort of inbuilr row numbering scheme. have hack around using user variables. try (http://sqlfiddle.com/#!9/4a5487/7/0)
select '2017-04-01' + interval(@rownum := @rownum+1) day date, a.name, b.name names join names b on a.id > b.id join (select @rownum := -1) initialize
Comments
Post a Comment