<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Wed May 22, 2013 12:48 am by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Wed May 22, 2013 12:48 am</pubDate>
  <lastBuildDate>Wed May 22, 2013 12:48 am</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: mysql joins</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=108642#108642</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=11435'&gt;maverickprowls&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sat Nov 10, 2012 1:14 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;bobthebob1234 wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;&lt;br /&gt;
I want to select all the events (only once) and order them by the (most recent) date&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
You say that you want to select your events only once, but this query will join each time there is a date for an event, so you'll get several rows returned for any event with more than one date.&lt;br /&gt;
&lt;br /&gt;
For a single row for each event showing the most recent event you'd need something like this (note: not tested, and my experience is SQL Server so you might need to check the syntax for MySQL)&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
SELECT e.event_id, d.maxdate FROM events AS e&lt;br /&gt;
INNER JOIN &lt;br /&gt;
&amp;nbsp; &amp;#40;SELECT event_id, MAX&amp;#40;date&amp;#41; AS maxdate FROM dates GROUP BY event_id&amp;#41; AS d ON d.event_id = e.event_id&lt;br /&gt;
ORDER BY d.maxdate DESC&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
This joins your events to only the most recent (max) date in your dates table.&lt;br /&gt;
&lt;br /&gt;
Additionally, it's good practice to expand * to the fields you want to return, that way your queries don't (necessarily) break when you change your tables.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=108642#108642</comments>
                                        <author>maverickprowls</author>
                                        <pubDate>Sat Nov 10, 2012 1:14 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=108642#108642</guid>
                                      </item>
                                      <item>
                                        <title>Re: mysql joins</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=103852#103852</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=2007'&gt;CJLL&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sat Jan 14, 2012 6:32 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      &lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;bobthebob1234 wrote:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;quote&quot;&gt;Well its more the query that is bothering me.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
SELECT * FROM events, dates WHERE events.event_id = dates.event_id GROUP BY `event_name` ORDER BY `date` DESC &lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
does the same thing, so I feel I may be missing the point of joins...&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
Straight joins between multiple tables can easily be performed in the WHERE section.&lt;br /&gt;
&lt;br /&gt;
However in your application you will eventually need to use left or right joins, where one table may be populated and the other isn't.&lt;br /&gt;
&lt;br /&gt;
For example, supposing you want to list &lt;span style=&quot;font-weight: bold&quot;&gt;ALL&lt;/span&gt; events, with dates where appropriate.&lt;br /&gt;
&lt;br /&gt;
Using your example, you would only list events that had dates  but not the events which yet had to have dates created.&lt;br /&gt;
&lt;br /&gt;
Therefore you need a different type of join, which will list all events and any dates attaching to those events.&lt;br /&gt;
&lt;br /&gt;
The solution to this is a LEFT JOIN.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
SELECT * FROM events LEFT JOIN dates ON events.event_id = dates.event_id GROUP BY `event_name` ORDER BY `event_name` DESC &lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
To take this even further suppose you need to know which events do not have any dates, by checking to see if the date record is populated, you can easily find the answer.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
SELECT * FROM events LEFT JOIN dates ON events.event_id = dates.event_id WHERE dates.event_id IS NULL GROUP BY `event_name` ORDER BY `event_name` DESC &lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight: bold&quot;&gt;*disclaimer* it's 5am and I've not tested the above queries&lt;/span&gt;</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=103852#103852</comments>
                                        <author>CJLL</author>
                                        <pubDate>Sat Jan 14, 2012 6:32 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=103852#103852</guid>
                                      </item>
                                      <item>
                                        <title>Re: mysql joins</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=103756#103756</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=23984'&gt;bobthebob1234&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sun Jan 08, 2012 10:55 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Well its more the query that is bothering me.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
SELECT * FROM events, dates WHERE events.event_id = dates.event_id GROUP BY `event_name` ORDER BY `date` DESC &lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
does the same thing, so I feel I may be missing the point of joins...</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=103756#103756</comments>
                                        <author>bobthebob1234</author>
                                        <pubDate>Sun Jan 08, 2012 10:55 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=103756#103756</guid>
                                      </item>
                                      <item>
                                        <title>Re: mysql joins</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=103664#103664</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=3981'&gt;greg.d&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Jan 04, 2012 5:00 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Is it the output you dont like or the query?&lt;br /&gt;
&lt;br /&gt;
From what I can gather, because your 2 columns have the same name, you can use the USING clause instead of ON. Ie. USING(event_id) or something like that.&lt;br /&gt;
&lt;br /&gt;
Other than that it looks ok to me, but I'm no expert.</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=103664#103664</comments>
                                        <author>greg.d</author>
                                        <pubDate>Wed Jan 04, 2012 5:00 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=103664#103664</guid>
                                      </item>
                                      <item>
                                        <title>mysql joins</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=103663#103663</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=23984'&gt;bobthebob1234&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Jan 04, 2012 11:30 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      I've not used joins before, but think they would be idea for what I want to do here.&lt;br /&gt;
&lt;br /&gt;
I have tables events &amp;amp; dates.&lt;br /&gt;
events has event_id as its primary key&lt;br /&gt;
dates has event_id as a foreign key, and also date&lt;br /&gt;
&lt;br /&gt;
an event can have many dates&lt;br /&gt;
&lt;br /&gt;
I want to select all the events (only once) and order them by the (most recent) date&lt;br /&gt;
&lt;br /&gt;
I have&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
SELECT * FROM events INNER JOIN dates ON events.event_id = dates.event_id GROUP BY `event_name` ORDER BY `date` DESC&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
which appears to work but isn't very pretty. Is there a better way?&lt;br /&gt;
&lt;br /&gt;
Thanks</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=103663#103663</comments>
                                        <author>bobthebob1234</author>
                                        <pubDate>Wed Jan 04, 2012 11:30 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=103663#103663</guid>
                                      </item></channel></rss>