duanlv2788 2016-04-14 11:43
浏览 45
已采纳

我可以编写什么sql语句来获取专辑的所有链接轨道

I'm trying to think of an SQL statement I can write up in MySQL in which I can get all the tracks included inside an album. The way I have it set up is Album_id which is the primary key to my album table is linked inside the tracks table in the form of a foreign key. That way each track row is linked to an album in the album table.

I'll further detail below the layout of my tables:

tbl_Albums

As you can see I have the Album_id as the primary key and it's auto_incremented so the id will automatically update when a new row is entered.

CREATE TABLE `tbl_Albums` (
`Album_id` int(11) NOT NULL auto_increment,
`Album_Name` varchar(32) NOT NULL,
`Number_Of_Tracks` int(11) NOT NULL,
`Genre` varchar(32) NOT NULL,
`Artist_id` int(11) NOT NULL,
 PRIMARY KEY  (`Album_id`),
 KEY `Artist_id` (`Artist_id`),
 CONSTRAINT `tbl_Albums_ibfk_2` 
 FOREIGN KEY (`Artist_id`) REFERENCES `tbl_Artist` (`Artist_id`))

tbl_Tracks

Inside the tracks table I set a foreign key with album_id, so I link the tracks to a specific album.

CREATE TABLE `tbl_Tracks` (
`Track_id` int(11) NOT NULL auto_increment,
`Track_Name` varchar(32) NOT NULL,
`Length` time NOT NULL,
`Artist_id` int(11) NOT NULL,
`Album_id` int(11) NOT NULL,
 PRIMARY KEY  (`Track_id`),
 KEY `Artist_id` (`Artist_id`),
 KEY `Album_id` (`Album_id`),
 CONSTRAINT `tbl_Tracks_ibfk_1` 
 FOREIGN KEY (`Artist_id`) REFERENCES `tbl_Artist` (`Artist_id`),
 CONSTRAINT `tbl_Tracks_ibfk_2` 
 FOREIGN KEY (`Album_id`) REFERENCES `tbl_Albums` (`Album_id`))

I have the tables linked properly, but I'm trying to think of an MySQL statement that can allow me to show all the tracks linked to a specific album and have come up empty so far. I'm then trying to implement the code into a php form for a user to use to list out the tracks

Any help would be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • dongpai9986 2016-04-14 11:48
    关注
        SELECT
          `t2`.*
        FROM
          `tbl_Albums` `t1`
        INNER JOIN
          `tbl_Tracks` `t2`
        ON
          `t2`.`Album_id`=`t1`.`Album_id`
        WHERE
          `t1`.`Album_Name`='Name of Your album'
        ORDER BY
           `t2`.`Track_id`;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?