我正在使用查询来搜索两个表格(曲目和艺术家):
SELECT trackID, title
FROM tracks WHERE title LIKE '%$s%'
LIMIT 5
UNION
SELECT artistID, name
FROM artists WHERE name LIKE '%$s%'
LIMIT 5";
我使用mysql_num_rows($result)
来显示此查询的结果,但我不知道如何正确显示它。
因为当行来自艺术家但保持相同的顺序时,我希望输出结果不同。
我希望这很清楚,在此先感谢!!
答案 0 :(得分:3)
您可以使用其他列来标识结果行,例如
SELECT trackID AS id, title AS title, 'track' AS result_type
FROM tracks WHERE title LIKE '%$s%'
LIMIT 5
UNION
SELECT artistID AS id, name AS title , 'artist' AS result_type
FROM artists WHERE name LIKE '%$s%'
LIMIT 5;
/* $result= fetch results from query*/
foreach($result as $row){
if($row['result_type'] =='track'){
/* your code */
}
if($row['result_type'] =='artist'){
/* your code */
}
}