Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the last record inserted into a select query
    primarykey
    data
    text
    <p>I am creating a small message board and I am stuck</p> <p>I can select the subject, the original author, the number of replies but what I can't do is get the username, topic or date of the last post.</p> <p>There are 3 tables, boards, topics and messages.</p> <p>I want to get the author, date and topic of the last message in the message table. The author and date field are already fields on the messages table but i would need to join the messages and topics table on the topicid field.</p> <p>this is my query that selects the subject, author, and number of replies</p> <pre><code>SELECT t.topicname, t.author, count( message ) AS message FROM topics t INNER JOIN messages m ON m.topicid = t.topicid INNER JOIN boards b ON b.boardid = t.boardid WHERE b.boardid = 1 GROUP BY t.topicname </code></pre> <p>Can anyone please help me get this finished?</p> <p>This is what my tables look like</p> <pre><code>CREATE TABLE `boards` ( `boardid` int(2) NOT NULL auto_increment, `boardname` varchar(255) NOT NULL default '', PRIMARY KEY (`boardid`) ); CREATE TABLE `messages` ( `messageid` int(6) NOT NULL auto_increment, `topicid` int(4) NOT NULL default '0', `message` text NOT NULL, `author` varchar(255) NOT NULL default '', `date` timestamp(14) NOT NULL, PRIMARY KEY (`messageid`) ); CREATE TABLE `topics` ( `topicid` int(4) NOT NULL auto_increment, `boardid` int(2) NOT NULL default '0', `topicname` varchar(255) NOT NULL default '', `author` varchar(255) NOT NULL default '', PRIMARY KEY (`topicid`) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload