Question about query - can this be done?
Question about query - can this be done?
am 02.06.2009 17:52:17 von ray
Hello,
I've tried the manual and google, but I am not even sure what to call what I
want to do.
simplified data example:
I have a table of start and end times for an event, and an id for that event
in a table. each event may occur multiple times, but never more than 5 times
and rarely more than 3.
I want a query that will provide one record per event with all times included.
feel free to answer RTFM or STFW as long as you provide the manual section or
key words. ;)
Thanks,
Ray
chart form follows:
id | event_id | start | end
---------------------------------------
1 | 4 | t1 | t2
2 | 4 | t3 | t4
3 | 4 | t5 | t6
4 | 5 | t1 | t2
5 | 5 | t3 | t4
becomes
id | event_id | start | end | start | end | start | end
------------------------------------------------------------ ---------------
? | 4 | t1 | t2 | t3 | t4 | t5 | t6
? | 5 | t1 | t2 | t3 | t4
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org
Re: Question about query - can this be done?
am 02.06.2009 18:32:39 von Brent Baisley
On Tue, Jun 2, 2009 at 11:52 AM, Ray wrote:
> Hello,
>
> I've tried the manual and google, but I am not even sure what to call wha=
t I
> want to do.
>
> simplified data example:
> I have a table of start and end times for an event, and an id for that ev=
ent
> in a table. each event may occur multiple times, but never more than 5 ti=
mes
> and rarely more than 3.
> I want a query that will provide one record per event with all times incl=
uded.
> feel free to answer RTFM or STFW as long as you provide the manual sectio=
n or
> key words. =A0;)
> Thanks,
> Ray
>
>
> chart form follows:
>
> id | event_id | start | end
> ---------------------------------------
> 1 =A0| =A0 =A04 =A0 =A0 =A0 =A0 | t1 =A0 =A0| t2
> 2 =A0| =A0 =A04 =A0 =A0 =A0 =A0 | t3 =A0 =A0| t4
> 3 =A0| =A0 =A04 =A0 =A0 =A0 =A0 | t5 =A0 =A0| t6
> 4 =A0| =A0 =A05 =A0 =A0 =A0 =A0 | t1 =A0 =A0| t2
> 5 =A0| =A0 =A05 =A0 =A0 =A0 =A0 | t3 =A0 =A0| t4
>
> becomes
>
> id | event_id | start | end | start | end | start | end
> ------------------------------------------------------------ -------------=
--
> ? =A0| =A0 =A0 4 =A0 =A0 =A0 =A0| t1 =A0 =A0| t2 =A0 =A0 | t3 =A0 =A0| t4=
=A0 =A0| =A0t5 =A0 =A0| t6
> ? =A0| =A0 =A0 5 =A0 =A0 =A0 =A0| t1 =A0 =A0| t2 =A0 =A0 | t3 =A0 =A0| t4
>
I think what you are looking for is GROUP_CONCAT. You can just GROUP
BY event id, and then process the resulting delimited string on the
front end.
SELECT event_id, GROUP_CONCAT(start) start_dates, GROUP_CONCAT(end) end_dat=
es
FROM events GROUP BY event_id
Or even combined start and end dates into a single string and group them.
SELECT event_id, GROUP_CONCAT( CONCAT(start, '-', end) ) start_end
FROM events GROUP BY event_id
But, if you really want to get it in the column format you indicate,
you can make a much more complicated query. Use SUBSTRING_INDEX to
split out the parts of the group you need.
SELECT event_id,
SUBSTRING_INDEX(GROUP_CONCAT(start), ',', 1 ) start1,
SUBSTRING_INDEX(GROUP_CONCAT(end), ',', 1 ) end1,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(start), ',', 2 ), ',',
-1 ) start2,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(end), ',', 2 ), ',', -1 ) end=
2,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(start), ',', 3 ), ',',
-1 ) start3,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(end), ',', 3 ), ',', -1 ) end=
3,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(start), ',', 4 ), ',',
-1 ) start4,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(end), ',', 4 ), ',', -1 ) end=
4,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(start), ',', 5 ), ',',
-1 ) start5,
SUBSTRING_INDEX( SUBSTRING_INDEX(GROUP_CONCAT(end), ',', 5 ), ',', -1 ) end=
5
FROM events GROUP BY event_id;
I think that will give the format you specified, but I am not
recommending you do it this way.
Hope that helps.
Brent Baisley
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dgcdmg-mysql-2@m.gmane.o rg
Re: Question about query - can this be done?
am 02.06.2009 18:44:48 von Peter Brawley
--------------060702090005080507050304
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Ray,
>I want a query that will provide one record per event with all times included.
>feel free to answer RTFM or STFW as long as you provide the manual section or
>key words. ;)
Can be done with a pivot table. Examples under "Pivot tables" at
http://www.artfulsoftware.com/infotree/queries.php. If you get stuck,
pipe up.
PB
-----
Ray wrote:
> Hello,
>
> I've tried the manual and google, but I am not even sure what to call what I
> want to do.
>
> simplified data example:
> I have a table of start and end times for an event, and an id for that event
> in a table. each event may occur multiple times, but never more than 5 times
> and rarely more than 3.
> I want a query that will provide one record per event with all times included.
> feel free to answer RTFM or STFW as long as you provide the manual section or
> key words. ;)
> Thanks,
> Ray
>
>
> chart form follows:
>
> id | event_id | start | end
> ---------------------------------------
> 1 | 4 | t1 | t2
> 2 | 4 | t3 | t4
> 3 | 4 | t5 | t6
> 4 | 5 | t1 | t2
> 5 | 5 | t3 | t4
>
> becomes
>
> id | event_id | start | end | start | end | start | end
> ------------------------------------------------------------ ---------------
> ? | 4 | t1 | t2 | t3 | t4 | t5 | t6
> ? | 5 | t1 | t2 | t3 | t4
>
>
> ------------------------------------------------------------ ------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.339 / Virus Database: 270.12.50/2150 - Release Date: 06/02/09 06:47:00
>
>
--------------060702090005080507050304--
Mysql 4.1 vs 5.0
am 02.06.2009 20:58:40 von Jaime Fuentes
QmVuY2hpbmcNClNvbWVib2R5IGtub3dzIHdoeSA0LjEgaXMgZmFzdGVyIHRo YW4gNS4wIG15c3Fs
IHZlcnNpb25zDQoNCkluZy4gSmFpbWUgRnVlbnRlcyBSLg0KOTk3NTAwNDU5 D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zdHMubXlzcWwuY29tL215c3FsP3Vuc3ViPWpm dWVudGVzQHNlZ3Vy
c2F0LmNvbQ0KDQo=
Re: Question about query - can this be done?
am 02.06.2009 23:14:36 von ray
On June 2, 2009 10:44:48 am Peter Brawley wrote:
> Ray,
>
> >I want a query that will provide one record per event with all times
> > included. feel free to answer RTFM or STFW as long as you provide the
> > manual section or key words. ;)
>
> Can be done with a pivot table. Examples under "Pivot tables" at
> http://www.artfulsoftware.com/infotree/queries.php. If you get stuck,
> pipe up.
>
> PB
>
Thanks Peter and Brent.
GROUP_CONCAT does exactly what I want.
Brent, you're right, I don't really want to break up the times into separate
fields that bad, the results are going into PHP so I can parse the combined
fields there without much difficulty.
The next problem is how do I use the results in a join. My first thought (that
doesn't work) was:
SELECT event_id, GROUP_CONCAT( CONCAT(start, '-', end) ) start_end FROM events
GROUP BY event_id JOIN event_details WHERE
events.event_id=event_details.event_id
I have tried brackets, and a few other things, but I haven't got it yet.
Thanks,
Ray
> -----
>
> Ray wrote:
> > Hello,
> >
> > I've tried the manual and google, but I am not even sure what to call
> > what I want to do.
> >
> > simplified data example:
> > I have a table of start and end times for an event, and an id for that
> > event in a table. each event may occur multiple times, but never more
> > than 5 times and rarely more than 3.
> > I want a query that will provide one record per event with all times
> > included. feel free to answer RTFM or STFW as long as you provide the
> > manual section or key words. ;)
> > Thanks,
> > Ray
> >
> >
> > chart form follows:
> >
> > id | event_id | start | end
> > ---------------------------------------
> > 1 | 4 | t1 | t2
> > 2 | 4 | t3 | t4
> > 3 | 4 | t5 | t6
> > 4 | 5 | t1 | t2
> > 5 | 5 | t3 | t4
> >
> > becomes
> >
> > id | event_id | start | end | start | end | start | end
> > ------------------------------------------------------------ -------------
> >-- ? | 4 | t1 | t2 | t3 | t4 | t5 | t6 ? |
> > 5 | t1 | t2 | t3 | t4
> >
> >
> > ------------------------------------------------------------ ------------
> >
> >
> > No virus found in this incoming message.
> > Checked by AVG - www.avg.com
> > Version: 8.5.339 / Virus Database: 270.12.50/2150 - Release Date:
> > 06/02/09 06:47:00
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org
Re: Question about query - can this be done?
am 02.06.2009 23:58:14 von ray
On June 2, 2009 03:14:36 pm Ray wrote:
> On June 2, 2009 10:44:48 am Peter Brawley wrote:
> > Ray,
> >
> > >I want a query that will provide one record per event with all times
> > > included. feel free to answer RTFM or STFW as long as you provide the
> > > manual section or key words. ;)
> >
> > Can be done with a pivot table. Examples under "Pivot tables" at
> > http://www.artfulsoftware.com/infotree/queries.php. If you get stuck,
> > pipe up.
> >
> > PB
>
> Thanks Peter and Brent.
> GROUP_CONCAT does exactly what I want.
> Brent, you're right, I don't really want to break up the times into
> separate fields that bad, the results are going into PHP so I can parse the
> combined fields there without much difficulty.
>
> The next problem is how do I use the results in a join. My first thought
> (that doesn't work) was:
>
> SELECT event_id, GROUP_CONCAT( CONCAT(start, '-', end) ) start_end FROM
> events GROUP BY event_id JOIN event_details WHERE
not sure where this typo came from ^^^^
I meant ON
> events.event_id=event_details.event_id
>
> I have tried brackets, and a few other things, but I haven't got it yet.
> Thanks,
> Ray
>
I found a solution, but not sure if it's a good idea.
CREATE OR REPLACE VIEW v AS SELECT event_id, GROUP_CONCAT( CONCAT(start, '-',
end) ) start_end FROM events GROUP BY event_id;
SELECT * FROM event_details JOIN v ON events.event_id=event_details.event_id
Thanks,
Ray
> > -----
> >
> > Ray wrote:
> > > Hello,
> > >
> > > I've tried the manual and google, but I am not even sure what to call
> > > what I want to do.
> > >
> > > simplified data example:
> > > I have a table of start and end times for an event, and an id for that
> > > event in a table. each event may occur multiple times, but never more
> > > than 5 times and rarely more than 3.
> > > I want a query that will provide one record per event with all times
> > > included. feel free to answer RTFM or STFW as long as you provide the
> > > manual section or key words. ;)
> > > Thanks,
> > > Ray
> > >
> > >
> > > chart form follows:
> > >
> > > id | event_id | start | end
> > > ---------------------------------------
> > > 1 | 4 | t1 | t2
> > > 2 | 4 | t3 | t4
> > > 3 | 4 | t5 | t6
> > > 4 | 5 | t1 | t2
> > > 5 | 5 | t3 | t4
> > >
> > > becomes
> > >
> > > id | event_id | start | end | start | end | start | end
> > > ------------------------------------------------------------ -----------
> > >-- -- ? | 4 | t1 | t2 | t3 | t4 | t5 | t6 ?
> > > | 5 | t1 | t2 | t3 | t4
> > >
> > >
> > > ------------------------------------------------------------ -----------
> > >-
> > >
> > >
> > > No virus found in this incoming message.
> > > Checked by AVG - www.avg.com
> > > Version: 8.5.339 / Virus Database: 270.12.50/2150 - Release Date:
> > > 06/02/09 06:47:00
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org
RE: Question about query - can this be done?
am 03.06.2009 00:13:31 von Nathan Sullivan
Ray,
You can use the results of a query in a join with something like:
select tmp.id, t1.id
from (some_query_selecting_id) as tmp
join t1 on t1.id=3Dtmp.id
Hope that helps.
Regards,
Nathan Sullivan
-----Original Message-----
From: Ray [mailto:ray@stilltech.net]=20
Sent: Tuesday, June 02, 2009 4:58 PM
To: mysql@lists.mysql.com
Subject: Re: Question about query - can this be done?
On June 2, 2009 03:14:36 pm Ray wrote:
> On June 2, 2009 10:44:48 am Peter Brawley wrote:
> > Ray,
> >
> > >I want a query that will provide one record per event with all times
> > > included. feel free to answer RTFM or STFW as long as you provide the
> > > manual section or key words. ;)
> >
> > Can be done with a pivot table. Examples under "Pivot tables" at
> > http://www.artfulsoftware.com/infotree/queries.php. If you get stuck,
> > pipe up.
> >
> > PB
>
> Thanks Peter and Brent.
> GROUP_CONCAT does exactly what I want.
> Brent, you're right, I don't really want to break up the times into
> separate fields that bad, the results are going into PHP so I can parse t=
he
> combined fields there without much difficulty.
>
> The next problem is how do I use the results in a join. My first thought
> (that doesn't work) was:
>
> SELECT event_id, GROUP_CONCAT( CONCAT(start, '-', end) ) start_end FROM
> events GROUP BY event_id JOIN event_details WHERE
not sure where this typo came from ^^^^
I meant ON=20
> events.event_id=3Devent_details.event_id
>
> I have tried brackets, and a few other things, but I haven't got it yet.
> Thanks,
> Ray
>
I found a solution, but not sure if it's a good idea.
CREATE OR REPLACE VIEW v AS SELECT event_id, GROUP_CONCAT( CONCAT(start, '-=
',=20
end) ) start_end FROM events GROUP BY event_id;=20
SELECT * FROM event_details JOIN v ON events.event_id=3Devent_details.event=
_id=20
Thanks,=20
Ray
> > -----
> >
> > Ray wrote:
> > > Hello,
> > >
> > > I've tried the manual and google, but I am not even sure what to call
> > > what I want to do.
> > >
> > > simplified data example:
> > > I have a table of start and end times for an event, and an id for tha=
t
> > > event in a table. each event may occur multiple times, but never more
> > > than 5 times and rarely more than 3.
> > > I want a query that will provide one record per event with all times
> > > included. feel free to answer RTFM or STFW as long as you provide the
> > > manual section or key words. ;)
> > > Thanks,
> > > Ray
> > >
> > >
> > > chart form follows:
> > >
> > > id | event_id | start | end
> > > ---------------------------------------
> > > 1 | 4 | t1 | t2
> > > 2 | 4 | t3 | t4
> > > 3 | 4 | t5 | t6
> > > 4 | 5 | t1 | t2
> > > 5 | 5 | t3 | t4
> > >
> > > becomes
> > >
> > > id | event_id | start | end | start | end | start | end
> > > ------------------------------------------------------------ ---------=
--
> > >-- -- ? | 4 | t1 | t2 | t3 | t4 | t5 | t6=
?
> > > | 5 | t1 | t2 | t3 | t4
> > >
> > >
> > > ------------------------------------------------------------ ---------=
--
> > >-
> > >
> > >
> > > No virus found in this incoming message.
> > > Checked by AVG - www.avg.com
> > > Version: 8.5.339 / Virus Database: 270.12.50/2150 - Release Date:
> > > 06/02/09 06:47:00
--=20
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dnsullivan@cappex.co=
m
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dgcdmg-mysql-2@m.gmane.o rg
[solved]Re: Question about query - can this be done?
am 03.06.2009 00:37:12 von ray
On June 2, 2009 04:13:31 pm Nathan Sullivan wrote:
> Ray,
>
> You can use the results of a query in a join with something like:
>
> select tmp.id, t1.id
> from (some_query_selecting_id) as tmp
> join t1 on t1.id=tmp.id
>
>
> Hope that helps.
>
>
> Regards,
> Nathan Sullivan
Thanks Nathan,
I think that completes the picture.
Just what I was looking for.
Ray
>
> -----Original Message-----
> From: Ray [mailto:ray@stilltech.net]
> Sent: Tuesday, June 02, 2009 4:58 PM
> To: mysql@lists.mysql.com
> Subject: Re: Question about query - can this be done?
>
> On June 2, 2009 03:14:36 pm Ray wrote:
> > On June 2, 2009 10:44:48 am Peter Brawley wrote:
> > > Ray,
> > >
> > > >I want a query that will provide one record per event with all times
> > > > included. feel free to answer RTFM or STFW as long as you provide the
> > > > manual section or key words. ;)
> > >
> > > Can be done with a pivot table. Examples under "Pivot tables" at
> > > http://www.artfulsoftware.com/infotree/queries.php. If you get stuck,
> > > pipe up.
> > >
> > > PB
> >
> > Thanks Peter and Brent.
> > GROUP_CONCAT does exactly what I want.
> > Brent, you're right, I don't really want to break up the times into
> > separate fields that bad, the results are going into PHP so I can parse
> > the combined fields there without much difficulty.
> >
> > The next problem is how do I use the results in a join. My first thought
> > (that doesn't work) was:
> >
> > SELECT event_id, GROUP_CONCAT( CONCAT(start, '-', end) ) start_end FROM
> > events GROUP BY event_id JOIN event_details WHERE
>
> not sure where this typo came from ^^^^
> I meant ON
>
> > events.event_id=event_details.event_id
> >
> > I have tried brackets, and a few other things, but I haven't got it yet.
> > Thanks,
> > Ray
>
> I found a solution, but not sure if it's a good idea.
>
> CREATE OR REPLACE VIEW v AS SELECT event_id, GROUP_CONCAT( CONCAT(start,
> '-', end) ) start_end FROM events GROUP BY event_id;
> SELECT * FROM event_details JOIN v ON
> events.event_id=event_details.event_id
>
> Thanks,
> Ray
>
> > > -----
> > >
> > > Ray wrote:
> > > > Hello,
> > > >
> > > > I've tried the manual and google, but I am not even sure what to call
> > > > what I want to do.
> > > >
> > > > simplified data example:
> > > > I have a table of start and end times for an event, and an id for
> > > > that event in a table. each event may occur multiple times, but never
> > > > more than 5 times and rarely more than 3.
> > > > I want a query that will provide one record per event with all times
> > > > included. feel free to answer RTFM or STFW as long as you provide the
> > > > manual section or key words. ;)
> > > > Thanks,
> > > > Ray
> > > >
> > > >
> > > > chart form follows:
> > > >
> > > > id | event_id | start | end
> > > > ---------------------------------------
> > > > 1 | 4 | t1 | t2
> > > > 2 | 4 | t3 | t4
> > > > 3 | 4 | t5 | t6
> > > > 4 | 5 | t1 | t2
> > > > 5 | 5 | t3 | t4
> > > >
> > > > becomes
> > > >
> > > > id | event_id | start | end | start | end | start | end
> > > > ------------------------------------------------------------ ---------
> > > >-- -- -- ? | 4 | t1 | t2 | t3 | t4 | t5 |
> > > > t6 ?
> > > >
> > > > | 5 | t1 | t2 | t3 | t4
> > > >
> > > > ------------------------------------------------------------ ---------
> > > >-- -
> > > >
> > > >
> > > > No virus found in this incoming message.
> > > > Checked by AVG - www.avg.com
> > > > Version: 8.5.339 / Virus Database: 270.12.50/2150 - Release Date:
> > > > 06/02/09 06:47:00
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=nsullivan@cappex.com
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org