我正在尝试为歌手插入专辑,例如:
我的心:歌手1
可爱:Singer1和Singer3
跟我说:歌手5
可爱的专辑,由两位歌手共享
等等,在PHP中显示结果时,我喜欢这样:
我的心:歌手1 可爱:歌手1 - 歌手3 碰我:Singer5
偶然的机会,我有一个相同的专辑名称,歌手之间没有共享,在显示结果时,它显示如下:
我的心:歌手1 - 歌手8
可爱:歌手1 - 歌手3
跟我说:歌手5
我的心:歌手1 - 歌手8
然而,Singer1和Singer8并没有分享这张专辑,我想显示如下:
我的心:歌手1
可爱:歌手1 - 歌手3
跟我说:歌手5
我的心:歌手8
$queryy = 'SELECT silsila_ar, id_shksj
FROM mariyat_silsila
JOIN mariyat_silsila_join on
mariyat_silsila.ids = mariyat_silsila_join.id_silsilaj
WHERE mariyat_silsila.silsila_tran = "'.$albumName.'"';
echo $queryy.'<br>';
$reqq = $connexion->query($queryy);
$resultt = $reqq->fetchAll();
if($resultt){
//$listShk = array();
$listShk = '';
foreach($resultt as $kv) {
$id_shksj = $kv['id_shksj'];
$silsila_ar = $kv['silsila_ar'];
// get sheikh name
//$queSh = 'SELECT shk_fname, shk_lname FROM sheikh_tbl WHERE shk_tran = "'.$main_shk.'"';
$queSh = 'SELECT shk_fname, shk_lname, shk_tran
FROM sheikh_tbl
WHERE id_shk = "'.$id_shksj.'"';
echo $queSh.'</br>';
$reqSh = $connexion->query($queSh);
$resSh = $reqSh->fetchAll();
foreach($resSh as $aSh)
{
$shk_fname = $aSh['shk_fname'];
$shk_lname = $aSh['shk_lname'];
$shk_tran = $aSh['shk_tran'];
$shkFullName = $shk_fname.' '.$shk_lname;
//$listShk[] = $shkFullName;
$listShk .= '<a href="Sheikhs/'.$shk_tran.'.html" target="_BLANK">';
$listShk .= $shkFullName;
$listShk .= '</a>';
$listShk .= ' - ';
$listSheikhs = substr($listShk, 0,-2).'<br />';
}
}
答案 0 :(得分:0)
您想要实现的内容取决于您的数据库结构(抱歉,我无法理解您的查询语言)。您的行应具有唯一标识符;通常是数字ID。你可能有一张桌子将专辑与歌手联系起来,对吗?
album_singer
+---------------------+
| album | singer |
+---------------------+
| My Heart | Singer 1|
| My Heart | Singer 8|
| ... |
此表应该只包含相册和歌手的 ID 。
albums singers album_singer
+---------------------+ +---------------------+ +----------------------+
| Id | Name | | Id | Name | | album_id | singer_id |
+---------------------+ +---------------------+ +----------------------+
| 201 | My Heart | | 101 | Singer 1 | | 201 | 101 |
| 202 | My Heart | | 102 | Singer 8 | | 202 | 102 |
| ... | | ... | | ... |
如果这是您的结构,您的查询应该是:
SELECT albums.name as album_name,
singers.name as singer_name
FROM album_singer
JOIN albums on album_singer.album_id = albums.id
JOIN singers on album_singer.singer_id = singer.id
# Output:
# +------------------------------------+
# | album_name | singer_name |
# +------------------------------------+
# | My Heart | Singer 1 |
# | My Heart | Singer 2 |
# +------------------------------------+
答案 1 :(得分:0)