您好,并提前感谢您的回答。 我有两张桌子,曲目(音乐信息曲目,标题,长度,cd_id..etc)和 purchd_items (购买曲目,cd_id,user_id..etc)。我有这个问题:
功能蓝色($ id)
{
$user_id = $this->tank_auth->get_user_id();
$query = $this->db->query("SELECT tracks.id, tracks.track_id, tracks.cd_id, tracks.title, tracks.lenght, tracks.price, tracks.file, tracks.preview, tracks.hash, purchd_items.cd_id, purchd_items.name FROM tracks LEFT JOIN purchd_items ON tracks.cd_id = purchd_items.cd_id WHERE purchd_items.user_id = $user_id AND tracks.cd_id = $id GROUP BY tracks.title, purchd_items.name ORDER BY tracks.track_id");
return $query->result();
}
我要做的是检查两个表格是否匹配,并显示来自曲目表格中的所有信息,以及来自 purchd_items 表格的匹配项目,这些项目将替换曲目中的条目表。如果用户已经购买了任何曲目,我想显示下载链接,而不是通常的购买曲目链接。我想这就像itunes一样。
我有点能够使用上面的查询来解决这个问题,但是返回数组中的项目数乘以 purchd_items 表中的条目数,我无法摆脱重复的。
希望我在这里有意义!谢谢!
答案 0 :(得分:0)
我明白了!
SELECT tracks.id,tracks.track_id,tracks.cd_id,tracks.title,tracks.lenght,tracks.price,tracks.file,tracks.preview,tracks.hash,purchd_items.cd_id,purchd_items.name FROM tracks LEFT JOIN purchd_items ON tracks.cd_id = purchd_items.cd_id AND purchd_items.name = tracks.title AND purchd_items.user_id = $ user_id WHERE tracks.cd_id = $ id ORDER BY tracks.id