Understanding MySql queries -
trying teach self bit of mysql , php , decided working on actual project oh "how mysql , php book".
first problem have understanding joint table queries.
here tables:
create table `clients` ( `id` smallint(6) not null auto_increment, `client` varchar(50) null default null, primary key (`id`), index `klientid` (`id`) ) collate='utf8_general_ci' engine=innodb auto_increment=8; create table `facilities` ( `id` smallint(6) not null auto_increment, `facility` varchar(45) not null, `fk_client` smallint(6) null default null, primary key (`id`), index `fk_idklijent_idx` (`fk_client`), constraint `fk_client_id` foreign key (`fk_client`) references `clients` (`id`) on update cascade on delete cascade ) collate='utf8_general_ci' engine=innodb auto_increment=35; create table `models` ( `id` smallint(6) not null auto_increment, `model` varchar(50) not null, `fk_manufacturer` smallint(6) not null, primary key (`id`), index `modelid` (`id`), index `fk_proizvodjacid_idx` (`fk_manufacturer`), constraint `fk_manuf_id` foreign key (`fk_manufacturer`) references `manufacturers` (`id`) on update cascade on delete cascade ) collate='utf8_general_ci' engine=innodb auto_increment=9; create table `machines` ( `id` smallint(6) not null auto_increment, `serial` varchar(50) not null, `fk_model` smallint(6) not null, `invbr` int(11) null default '0', `fk_facilities` smallint(6) null default '0', `sw` varchar(255) null default null, `adaptation` varchar(255) null default null, primary key (`id`), index `fk_uredjaji_modeli` (`fk_model`), index `fk_uredjaji_poslovnice` (`fk_facilities`), index `index 4` (`serial`), constraint `fk_facility_id` foreign key (`fk_facilities`) references `facilities` (`id`) on update cascade on delete cascade, constraint `fk_models_id` foreign key (`fk_model`) references `models` (`id`) on update cascade on delete cascade ) collate='utf8_general_ci' engine=innodb auto_increment=93; create table `technicians` ( `id` smallint(6) not null auto_increment, `name` varchar(50) null default null, primary key (`id`), index `serviserid` (`id`) ) collate='utf8_general_ci' engine=innodb auto_increment=5; create table `workorders` ( `id` smallint(6) not null auto_increment, `wo_nr` varchar(50) not null, `fk_machine_id` smallint(6) not null, `fk_technitian_id` smallint(6) not null, `counter` int(11) not null default '0', `service_date` date not null, `description` longtext not null, `work_hours` int(11) not null default '1', primary key (`id`), index `fk_rn_serviseri` (`fk_technitian_id`), index `fk_machines_id_idx` (`fk_machine_id`), constraint `fk_machines_id` foreign key (`fk_machine_id`) references `machines` (`id`) on update no action on delete no action, constraint `fk_technitian_id` foreign key (`fk_technitian_id`) references `technicians` (`id`) on update no action on delete no action ) collate='utf8_general_ci' engine=innodb auto_increment=1393; using query each row 8 times.
select date_format (w.service_date, '%d.%m.%y'), f.facility, m.model, mc.serial, w.description, t.name workorders w, facilities f, models m, machines mc, technicians t f.id = mc.fk_facilities , w.fk_machine_id = mc.id , w.fk_technitian_id = t.id , w.service_date > '2009-12-31' order w.service_date desc; can pls point me i'm doing wrong. i need rows workorders table. other tables here show real data not id's - bad, not true :(
thx
edit 1: need list of workorders. explain question little more here how result should like: [date][name of facility machine (fk_machine_id) belongs to][model of machine (fk_machine_id)][serial number of machine (fk_machine_id)][description workorder][technician name (fk_technician_id)]
edit2:
i think problem in fact model need check machines table first. same thing facility.
got it!
the problem in 1 missing and.
final select looks this:
select date_format (w.service_date, '%d.%m.%y') service_date, w.wo_nr, f.facility, m.model, mc.serial, format (w.counter, 0) counter, w.description, t.name technician workorders w, technicians t, machines mc, models m, facilities f mc.fk_facilities = f.id , w.fk_machine_id = mc.id , mc.fk_model = m.id , w.fk_technitian_id = t.id , w.service_date > '2009-12-31' order w.service_date desc; thx all. moving on next problem :)
Comments
Post a Comment