gpt4 book ai didi

php - MYSQL联合展示

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:38 26 4
gpt4 key购买 nike

我正在使用查询在两个表(轨道和艺术家)中进行搜索:

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) 来显示这个查询的结果,但我不知道如何正确显示它。 因为当行来自艺术家但保持相同顺序时,我希望输出结果不同。

我希望这是清楚的,提前致谢!!

最佳答案

您可以使用另一列来标识结果行,例如

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 */
}
}

关于php - MYSQL联合展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com