mysql - INNER JOIN and GROUP BY to prevent duplicate results -


context:

i'm working on simple orm (for php) automatize of queries, based on static configuration.

thus, tables , entities definitions, library handles joins automatically , generates appropriate fields/table alias... no problem left joins inner may result in duplicated results in case of relation one-to-many. thought automatically add group clause (on auto-increment key) if necessary.

the question

is correct consider need add group clause if (and if) join's on , conditions doesn't match unique key of joined table ?

example

a simple example, want select events (at least) associated showing. if there other way without inner join, i'm interested know how :)

create table `event` (     `id` int unsigned not null auto_increment primary key,     `name` varchar(255) not null ); insert `event` (`name`) values ('my cool event');  create table `showing` (     `id` int unsigned not null auto_increment primary key,     `eventid` int unsigned not null,     `place` varchar(50) not null,      foreign key (`eventid`) references `event`(`id`),     unique (`eventid`, `place`) ); insert `showing` (`eventid`, `place`) values (1, 'school'); insert `showing` (`eventid`, `place`) values (1, 'park');    -- correct queries select t.* `event` t inner join `showing` t1 on t.id=t1.`eventid` t1.`placeid` = 'school'; select t.* `event` t inner join `showing` t1 on t.id=t1.`eventid` , t1.`placeid` = 'school';   -- query leading duplicate values select t.* `event` t inner join `showing` t1 on t.id=t1.`eventid`;   -- group query prevent duplicate values select t.* `event` t inner join `showing` t1 on t.id=t1.`eventid` group t.`id`; 

thanks !

(this should comment bit long)

no problem left joins inner may result in duplicated results in case of relation one-to-many

it's clear sentence @ least 1 of confused how relational database works, , how object-relation mapping should work.

query leading duplicate values

the rows produced not duplicates - you've written query doesn't show why different:

select t1.place, t.*  event  inner join showing  on event.id=showing.eventid; 

if you're not interested in data 'showing' why in query? if have events without related showing records should using 'exists' - not join (consider have single event 3 million showings)

select t1.place, t.*  `event` t  exists (select 1   showing   event.id=showing.eventid); 

if strictly implementing orm, shouldn't writing queries joins @ - imho, scenario better served using factories.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -