简体   繁体   English

从我的三个表中选择数据

[英]Select data from my three tables

I have three tables, one named album , while the other one is soundtrack and last but definitely not the least, is the artist . 我有三张桌子,一张叫album ,另一张是soundtrack ,最后但并非最不重要的是artist

Under one album is more than one track (obviously) and an artist name... 一张专辑下有多个曲目(很明显)和一个艺术家的名字...

So this is how it should look like: 因此,它应该是这样的:
Desired Output: 所需输出:

 |---------------------|
 |AlbumName by Charlie |
 |---------------------|
 | 1. See you again    |
 |---------------------|
 | 2. One call Away    |
 |---------------------|
 | Album by Ed Sheeran |
 |---------------------|
 | 1. Perfect          |
 |---------------------|
 | 2. Dive             |
 |---------------------|

And below is mysqli_fetch_array() that I used along side with while-loop to populate the <table> tag. 下面是mysqli_fetch_array() ,它与while-loop用于填充<table>标记。

$query = "SELECT a.* AS Album, b.* AS Track, c.* AS Artist 
        FROM album a 
        INNER JOIN track b ON a.album_id=b.album_id 
        INNER JOIN artist c ON a.artist_id=c.artist_id 
        ORDER BY a.album_id ASC";

$result = mysqli_query($con, $query);
$c=0;
while($row = mysqli_fetch_assoc($result)) {
   $album = $row['Album']." (".$row['Artist'].") ";
   $track = $row['Track'];
}

The problem is, when I ran this code... I could only get this result... 问题是,当我运行这段代码时……我只能得到这个结果……

|---------------------|
|AlbumName by Charlie |
|---------------------|
|  1. See you again   |
|---------------------|
|  2. One call away   |
|---------------------|
|  1. Perfect         |
|---------------------|
|  2.  Dive           |
|---------------------|

It got the list of the soundtracks, including the soundtrack's ID right, however, things didn't do well on the Album Name 它获得了原声带的列表,包括原声带的ID,但是, Album Name上的情况并不理想

I want it just to look like the Desired Output . 我希望它看起来像“ 所需的输出” Any ideas? 有任何想法吗? I've tried to search stackoverflow, but it seems it couldn't direct me on the same question, so I made my own. 我尝试搜索stackoverflow,但似乎无法指导我解决同一问题,因此我自己做了一个。 Please forgive (do not flag) me if it had an existing already, its just that I didn't know where to find it. 请原谅(不要标记)我是否已经存在,只是我不知道在哪里找到它。 And pleeeeease... I need an immediate and working answer. 还有pleeeeease ...我需要一个立即有效的答案。 Thanks guys! 多谢你们!

As for DATABASE Structure 至于数据库结构

album = {album_id, artist_id,album_title}
artist = {artist_id, artist_name}
track = {song_id, album_id, song_title}

UPDATE! 更新!
Okay, so this goes a little bit out of control. 好的,这有点失控了。 When I tried @swellar's suggestion this what happened. 当我尝试@swellar的建议时,发生了什么事。 And when I undo it, the output remains the same. 当我撤消它时,输出保持不变。 See the result below... 看到下面的结果...

|---------------------|
|AlbumName by Charlie |
|---------------------|
|  1. See you again   |
|---------------------|
|AlbumName by Charlie |
|---------------------|
|  2. One Call Away   |
|---------------------|
|AlbumName by Charlie |
|---------------------|
|  1.  Perfect        |
|---------------------|
|AlbumName by Charlie |
|---------------------|
|  2.   Dive          |
|---------------------|

What I want is to give each Album one or more than soundtrack but it get so messed up each time I dare to change the query . 我想要的是给每个Album一个或多个配乐,但是每次我敢更改query时,它都会变得混乱。 How can I sanitize the mysqli_fetch_array() for it to display the desired output? 如何mysqli_fetch_array()使其显示所需的输出?

Here is your SQL 这是你的SQL

Notice I've put 3 order by in there. 注意,我在那边下了3个订单。 First by the artist name, which would keep all artist's stuff grouped together, then by album name and then by song id. 首先是歌手姓名,这会将所有歌手的资料分组在一起,然后是专辑名称,然后是歌曲ID。

$sql = "SELECT a.album_title AS Album,
               t.song_title AS Track,
               t.song_id AS ID,
               n.artist_name AS Artist
        FROM album AS a
            INNER JOIN track AS t
                ON a.album_id = t.album_id
            INNER JOIN artist AS n
                ON a.artist_id = n.artist_ID
        ORDER BY n.artist_name ASC,
                 a.album_id ASC,
                 t.song_id ASC";


/*
That SQL will generate the following array

Array
(
    [Album] => AlbumName
    [Track] => See you again
    [ID] => 1
    [Artist] => Charlie
)
Array
(
    [Album] => AlbumName
    [Track] => One call Away
    [ID] => 2
    [Artist] => Charlie
)
Array
(
    [Album] => Album
    [Track] => Perfect
    [ID] => 1
    [Artist] => Ed Sheeran
)
Array
(
    [Album] => Album
    [Track] => Dive
    [ID] => 2
    [Artist] => Ed Sheeran
)
*/

Below is your while loop 以下是您的while循环

<table>
<?php
// Some placeholders
$album = null;

while($row = mysqli_fetch_assoc($result))
{
    if($row['Album'] !== $album)
    {
?>
    <tr>
        <td style="border: 1px solid #000000; background-color: #dedede;"><?php echo $row['Album']; ?> by <?php echo $row['Artist']; ?></td>
    </tr>
    <tr>
        <td style="border: 1px solid #000000;"><?php echo $row['ID']; ?>: <?php echo $row['Track']; ?></td>
    </tr>
<?php
    }
    else
    {
?>
    <tr>
        <td style="border: 1px solid #000000;"><?php echo $row['ID']; ?>: <?php echo $row['Track']; ?></td>
    </tr>
<?php
    }

   $album = $row['Album'];
}
?>
</table>

Overall Output is: 总输出为:

 <table> <tr> <td style="border: 1px solid #000000; background-color: #dedede;">AlbumName by Charlie</td> </tr> <tr> <td style="border: 1px solid #000000;">1: See you again</td> </tr> <tr> <td style="border: 1px solid #000000;">2: One call Away</td> </tr> <tr> <td style="border: 1px solid #000000; background-color: #dedede;">Album by Ed Sheeran</td> </tr> <tr> <td style="border: 1px solid #000000;">1: Perfect</td> </tr> <tr> <td style="border: 1px solid #000000;">2: Dive</td> </tr> </table> 

There are better ways to go around doing this, but not knowing your work environment, skills, requirements, other code, w/e this is the best quick and dirty solution for you to base your solution of. 有更好的方法可以解决此问题,但是不知道您的工作环境,技能,要求,其他代码,这是您基于其解决方案的最佳快速而又肮脏的解决方案。 This works to what you requested. 这可以满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM