基本上我正在为我的网站构建一个serch引擎,但我的sql数据库基本上包含两组表(一组用于网站上的页面,另一组用于文件(.doc等))
我已经让populate工作了,我让它返回页面的结果,现在我想搜索页面和文件,我想出了运行2个查询的想法(因为有2个表集),然后将这2个结果数组合并为一个,然后按查询添加的"occurrence"对它们进行排序。但是输出与输入数组不匹配。无论如何,一些代码可以让你在上工作
// Search the DB for pages
$page_result = mysql_query("SELECT p.page_url AS url, COUNT(*) AS occurrences FROM page p, word w, occurrence o WHERE p.page_id = o.page_id AND w.page_word_id = o.page_word_id AND w.word_word LIKE '' '" . $stemmed_string . "' '%' GROUP BY p.page_id ORDER BY occurrences DESC") // LIMIT " . $results . "")
or die("Invalid query: " . mysql_error());
// Search the DB for files
$file_result = mysql_query("SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo WHERE f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word LIKE '' '" . $stemmed_string . "' '%' GROUP BY f.file_id ORDER BY occurrences DESC")
or die ("Invalid query: " . mysql_error());
$page_array = mysql_fetch_array($page_result);
$file_array = mysql_fetch_array($file_result);
$results = array_merge((array)$page_array, (array)$file_array);
带有var转储的搜索项(a)的输出看起来像这个
array(4){[0]=>string(33)"/index.php?page=foot_tipping.htm"
array(4){[0]=>string(43)"/files/forms/misc/Adjustment%20TEMPLATE.xls"
array(6){[0]=>string(33)"/index.php?page=footy_tipping.htm"调整%20TEMPLATE.xls"[3]="字符串(1)"2"}
它们的排序与上面的变量相同
看了一下手册,没有发现任何关于如何按键对数组排序的有用信息=>值(例如排序($results['occurrence'],DESC))
任何帮助都会得到回应,谢谢:)
如何在sql中实现联合?
SELECT * FROM (
SELECT p.page_url AS url,
COUNT(*) AS occurrences
FROM page p, word w, occurrence o
WHERE p.page_id = o.page_id
AND w.page_word_id = o.page_word_id
AND w.word_word LIKE '' '" . $stemmed_string . "' '%'
GROUP BY p.page_id
UNION
SELECT f.file_url AS url,
COUNT(*) AS occurrences
FROM files f, filenames fn, fileoccurrence fo
WHERE f.file_id = fo.file_id
AND fn.file_word_id = fo.file_word_id
AND fn.file_word LIKE '' '" . $stemmed_string . "' '%'
GROUP BY f.file_id
) t
ORDER BY occurrences DESC
你看过array_multisort吗?你必须这样做:
<?php
// Obtain a list of columns
foreach ($results as $key => $row) {
$occurrences[$key] = $row['occurrences'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($occurrences, SORT_DESC, $results);
?>