請先執行下面的查詢來設置,這樣就可以幫助我: -MySQL查詢幫助 - 改變這個MySQL查詢得到這些結果?
CREATE TABLE IF NOT EXISTS `Tutor_Details` (
`id_tutor` int(10) NOT NULL auto_increment,
`firstname` varchar(100) NOT NULL default '',
`surname` varchar(155) NOT NULL default '',
PRIMARY KEY (`id_tutor`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;
INSERT INTO `Tutor_Details` (`id_tutor`,`firstname`, `surname`) VALUES
(1, 'Sandeepan', 'Nath'),
(2, 'Bob', 'Cratchit');
CREATE TABLE IF NOT EXISTS `Classes` (
`id_class` int(10) unsigned NOT NULL auto_increment,
`id_tutor` int(10) unsigned NOT NULL default '0',
`class_name` varchar(255) default NULL,
PRIMARY KEY (`id_class`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=229 ;
INSERT INTO `Classes` (`id_class`,`class_name`, `id_tutor`) VALUES
(1, 'My Class', 1),
(2, 'Sandeepan Class', 2);
CREATE TABLE IF NOT EXISTS `Tags` (
`id_tag` int(10) unsigned NOT NULL auto_increment,
`tag` varchar(255) default NULL,
PRIMARY KEY (`id_tag`),
UNIQUE KEY `tag` (`tag`),
KEY `id_tag` (`id_tag`),
KEY `tag_2` (`tag`),
KEY `tag_3` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(1, 'Bob'),
(6, 'Class'),
(2, 'Cratchit'),
(4, 'Nath'),
(3, 'Sandeepan'),
(5, 'My');
CREATE TABLE IF NOT EXISTS `Tutors_Tag_Relations` (
`id_tag` int(10) unsigned NOT NULL default '0',
`id_tutor` int(10) default NULL,
KEY `Tutors_Tag_Relations` (`id_tag`),
KEY `id_tutor` (`id_tutor`),
KEY `id_tag` (`id_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Tutors_Tag_Relations` (`id_tag`, `id_tutor`) VALUES
(3, 1),
(4, 1),
(1, 2),
(2, 2);
CREATE TABLE IF NOT EXISTS `Class_Tag_Relations` (
`id_tag` int(10) unsigned NOT NULL default '0',
`id_class` int(10) default NULL,
`id_tutor` int(10) NOT NULL,
KEY `Class_Tag_Relations` (`id_tag`),
KEY `id_class` (`id_class`),
KEY `id_tag` (`id_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `Class_Tag_Relations` (`id_tag`, `id_class`, `id_tutor`) VALUES
(5, 1, 1),
(6, 1, 1),
(3, 2, 2),
(6, 2, 2);
- 在本系統中的數據,我給了,名爲「Sandeepan納特」導師有已創建的類名爲「我的班」和導師名爲‘鮑伯’已創建名爲類‘Sandeepan類’。
要求 -
要對結果有限制執行一個查詢來顯示搜索結果按照與邏輯上是這樣的搜索關鍵字: -
- 如果「Sandeepan班」是(因爲「Sandeepan」是Sandeepan Nath的名字,Class在Sandeepan的班級名稱中存在)
- 如果搜索到「Class」這兩個Tutor_details表中的導師都將被返回(因爲「Sandeepan」是Sandeepan Nath的名字)被提取因爲Class是以兩個導師創建的班級的名字出現的。
以下是我迄今爲止所取得(PHP MySQL的): -
<?php
$searchTerm1 = "Sandeepan";
$searchTerm2 = "Class";
mysql_select_db("test");
$sql = "SELECT td.*
FROM Tutor_Details AS td
LEFT JOIN Tutors_Tag_Relations AS ttagrels ON td.id_tutor = ttagrels.id_tutor
LEFT JOIN Classes AS wc ON td.id_tutor = wc.id_tutor
LEFT JOIN Class_Tag_Relations AS wtagrels ON td.id_tutor = wtagrels.id_tutor
LEFT JOIN Tags as t1 on ((t1.id_tag = ttagrels.id_tag) OR (t1.id_tag = wtagrels.id_tag))
LEFT JOIN Tags as t2 on ((t2.id_tag = ttagrels.id_tag) OR (t2.id_tag = wtagrels.id_tag))
where t1.tag LIKE '%".$searchTerm1."%'
AND t2.tag LIKE '%".$searchTerm2."%'
GROUP BY td.id_tutor
LIMIT 10
";
$result = mysql_query($sql);
echo $sql;
if($result)
{
while($rec = mysql_fetch_object($result)) $recs[] = $rec;
//$rec = mysql_fetch_object($result);
echo "<br><br>";
if(is_array($recs))
{
foreach($recs as $each)
{
print_r($each);
echo "<br>";
}
}
}
?>
但結果是: -
如果 「Sandeepan納特」 進行搜索,它不返回任何如果搜索到「Sandeepan Class」,則返回Sandeepan的行(而不是兩個導師) 如果搜索到「Bob Class」,它會正確返回Bob的行 如果搜索到「Bob Cratchit」它不會返回任何導師(而不是onl Ÿ
嗨邁克感謝您的解決方案。它的工作原理非常完美,我完全同意「試圖通過連接進行這種搜索將會變得非常非常沉重」。 但是我並沒有完全明白你的意思,說「解決方案,如果你想用SQL來做,就是生成用於每個導師/類關係的標籤的所有2個搜索項排列」。 如果你至少可以給我更多的線索,讓我得到更清晰的圖片,那會很棒。再次感謝您的幫助 – 2010-05-28 09:49:12
嗨邁克,這個查詢會變得非常沉重,就像我們所看到的一樣。請檢查我的問題http://stackoverflow.com/questions/2927142/mysql-help-me-alter-this-query-to-apply-and-logic-instead-of-or-in-searching其中用戶已經給更好的解決方案。請看看你是否可以進一步幫助我,謝謝 – 2010-06-02 20:20:46
嗨邁克,我改進了一點我的搜索邏輯。爲了減少連接的複雜性和數量,我現在在搜索執行期間創建一個名爲All_Tag_Relations的臨時表,其中包含來自Tutors_Tag_Relations和Webclasses_Tag_Relations(現在忽略Learning_Packs_Tag_Relations)的所有數據。但是,仍然有一個問題,請檢查我的新問題,如果你可以幫助http://stackoverflow.com/questions/3030022/mysql-help-me-alter-this-search-query-to-get-desired-結果 – 2010-06-12 21:58:30