1

私は自分自身を十分に理解できることを望んでいます!次のSQLクエリがあります

SELECT DATE_FORMAT(calendar_date,'%W %D, %M, %Y') AS calendar_date,calendar_entry_title,calendar_entry_teaser
        FROM calendar_month
        LEFT JOIN calendar_entry ON calendar_entry.calendar_id = calendar_month.calendar_id
        ORDER BY calendar_date

これが私が扱っているテーブルの詳細です。

CREATE TABLE IF NOT EXISTS `calendar_entry` (
  `calendar_entry_id` int(11) NOT NULL AUTO_INCREMENT,
  `calendar_id` int(11) NOT NULL,
  `school_id` int(11) NOT NULL,
  `calendar_entry_title` varchar(250) NOT NULL,
  `calendar_entry_teaser` varchar(250) NOT NULL,
  `calendar_entry_text` text NOT NULL,
  PRIMARY KEY (`calendar_entry_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `calendar_entry`
--

INSERT INTO `calendar_entry` (`calendar_entry_id`, `calendar_id`, `school_id`, `calendar_entry_title`, `calendar_entry_teaser`, `calendar_entry_text`) VALUES
(1, 1, 1, 'School Event 1', 'School event information 1', 'This would be the full body of the text that would show on the full page for this given entry'),
(2, 1, 1, 'School Event 2', 'School event information 2', 'This would be the full body of the text that would show on the full page for this given entry');



CREATE TABLE IF NOT EXISTS `calendar_month` (
  `calendar_id` int(11) NOT NULL AUTO_INCREMENT,
  `school_id` int(11) NOT NULL,
  `calendar_date` date NOT NULL,
  PRIMARY KEY (`calendar_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `calendar_month`
--

INSERT INTO `calendar_month` (`calendar_id`, `school_id`, `calendar_date`) VALUES
(1, 1, '2012-08-11'),
(2, 1, '2012-08-12');

私が抱えている問題は、calendar_month テーブルに 2 行しかないことです。これらの行の 1 つには、month_entry テーブルに関連する 2 つの行があります。私が持っているクエリを実行すると、3行が表示されます。私がする必要があるのは、2行だけを表示することです。2行ある月は1行として表示する必要があります。これは、私が設定した方法で実行できますか?

ありがとう

結果 -

Saturday 11th, August, 2012     School Event 1  School event information 1
Saturday 11th, August, 2012     School Event 2  School event information 2
Sunday 12th, August, 2012   NULL    NULL

私が実際に欲しいもの -

Saturday 11th, August, 2012     School Event 1  School event information 1 School Event 2   School event information 2
Sunday 12th, August, 2012   NULL    NULL
4

2 に答える 2

0

試しましたか

SELECT DATE_FORMAT(calendar_date,'%W %D, %M, %Y') AS calendar_date, TMP.var1
FROM calendar_month
LEFT JOIN 
    (SELECT GROUP_CONCAT(calendar_entry_title, ' ',calendar_entry_teaser) AS var1, calendar_id
         FROM calendar_entry
     GROUP BY calendar_id) AS TMP ON TMP.calendar_id = calendar_month.calendar_id
ORDER BY calendar_date
于 2012-08-12T00:24:14.580 に答える
0

そのような結果は、mysql だけでは得られません。または、各行に 2 つのサブクエリを使用すると、遅かれ早かれサーバーがクラッシュします。むしろ、php を使用して結果をソートし、現在の calendar_entry_title を保存します。例:

$query =" SELECT DATE_FORMAT(calendar_date,'%W %D, %M, %Y') AS calendar_date,
calendar_entry_title,
calendar_entry_teaser
        FROM calendar_month
        LEFT JOIN calendar_entry ON calendar_entry.calendar_id = calendar_month.calendar_id
        ORDER BY calendar_date, calendar_entry_title";

$result = mysql_query($query);
$events = array();
foreach($result as $r){
     $events[$r['calendar_date']][] = $r;
}
echo '<pre>';
print_r($events);
echo '</pre>';
于 2012-08-12T00:24:01.983 に答える