MySQL查询以选择具有相同artistID但CDID不同的记录

匿名5671

选择专辑后,我试图显示艺术家的详细信息。但与此同时,与该艺术家相关的其他专辑也应显示。有什么办法可以做到这一点。

左列是CDID,右列是artistID。

这是我当前的mysql代码:

<?php

require "pdoDB.php";
$albumtitle = $_GET['title'];
$db = database::connect();
$artistsql = "SELECT a.artistName, p.pubName, cd.CDTitle FROM tiptop_artist a
             INNER JOIN tiptop_artistcd acd ON a.artistID = acd.artistID
             INNER JOIN tiptop_cd cd ON cd.CDID = acd.CDID 
             INNER JOIN tiptop_publisher p ON p.pubID = cd.pubID  
             WHERE cd.CDTitle = ?";
$stmt = $db->prepare($artistsql);
// $stmt->bindParam("catid", $categoryid);
$stmt->execute(array($albumtitle));

echo "<table>
<tr>
<th>Artist Name</th>
<th>Publisher</th>
<th>Other Album</th>
</tr>";

while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<tr style=\"cursor: pointer;\">";
    // echo "<td>" . $row1['artistName'] . "</td>";
    echo "<td>" . $row['artistName'] . "</td>";
    echo "<td>" . $row['pubName'] . "</td>";
    echo "<td>" . $row['CDTitle'] . "</td>";
    echo "</tr>";
}

?>

最终输出应获得如下内容:

artist name | publisher | related albums by the artist
------------------------------------------------------
hardwell    | sony music| I am hardwell
                        | Spaceman   

先感谢您。

前锤

您可以通过几种方法在单个查询或多个查询中完成此操作。您可能在某一时刻需要比较不同的查询策略,以查看哪个是性能最高的。

(注意:在下面的示例中,您可能希望在第一个查询之后添加一个检查,以确保它在运行第二个查询之前返回结果。)

这是一个使用两个查询的示例:

$sql = "SELECT artist.name as 'artist.name', 
               artist.id as 'artist.id',
               publisher.name as 'publisher.name', 
               cd.title as 'cd.title',
               cd.id as 'cd.id'
        FROM tiptop_cd as cd
        INNER JOIN tiptop_artistcd as artistcd ON(artistcd.cd_id = cd.id)
        INNER JOIN tiptop_artist as artist ON(artist.id = artistcd.artist_id)
        INNER JOIN tiptop_publisher as publisher ON(publisher.id = cd.publisher_id)
        WHERE cd.title = :title";

$stmt = $db->prepare($artistsql);
$stmt->bindParam(":title", $_GET['title']);
$stmt->execute();
$details = $stmt->fetch(PDO::FETCH_ASSOC);

$sql = "SELECT cd.title as 'cd.title'
        FROM tiptop_artistcd as artistcd
        INNER JOIN tiptop_cd as cd ON(cd.id = artistcd.cd_id AND cd.id != :cd_id)
        WHERE artistcd.artist_id = :artist_id";

$stmt = $db->prepare($artistsql);
$stmt->bindParam(":artist_id", $details['artist.id']);
$stmt->bindParam(":cd_id", $details['cd.id']);
$stmt->execute();

echo "<table>".
     "<tr>".
     "<th>Artist Name</th>".
     "<th>Publisher</th>".
     "<th>Other Album</th>".
     "</tr>".

     "<tr>".
     "<td>".$details['artist.name']."</td>".
     "<td>".$details['publisher.name']."</td>".
     "<td>".$details['cd.title']."</td>".
     "</tr>";

while ($album = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<tr style=\"cursor: pointer;\">";
    echo "<td></td>";
    echo "<td></td>";
    echo "<td>" . $album['cd.title'] . "</td>";
    echo "</tr>";
}

echo "</table";

这是一个查询版本:

SELECT artist.name as 'artist.name', 
       publisher.name as 'publisher.name', 
       cd.title as 'cd.title',
       albums.title as 'albums.title'
FROM tiptop_cd as cd
INNER JOIN tiptop_artistcd as artistcd ON(artistcd.cd_id = cd.id)
INNER JOIN tiptop_artist as artist ON(artist.id = artistcd.artist_id)
INNER JOIN tiptop_publisher as publisher ON(publisher.id = cd.publisher_id)
INNER JOIN tiptop_artistcd as artistcd2 ON(artistcd2.artist_id = artist.id)
INNER JOIN tiptop_cd as albums ON(albums.id = artistcd2.cd_id)
WHERE cd.title = :title

这是在单个查询中执行此操作的另一种巧妙的(可能是更高性能的)方法。返回的第一行将包含您的详细信息和当前选择的cd,其余行将包含其他专辑:

SELECT artist.name as 'artist.name', 
       publisher.name as 'publisher.name', 
       cd.title as 'cd.title',
       IF(cd.title = :title, 1, 0) as selected
FROM tiptop_cd as cd
INNER JOIN tiptop_artistcd as artistcd ON(artistcd.cd_id = cd.id)
INNER JOIN tiptop_artist as artist ON(artist.id = artistcd.artist_id)
INNER JOIN tiptop_publisher as publisher ON(publisher.id = cd.publisher_id)
ORDER BY selected DESC

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

MySQL选择不同的记录

来自分类Dev

sql查询以选择两列中具有相同id但不同值的记录

来自分类Dev

MYSQL从不同的行中选择2个具有相同值的不同字段

来自分类Dev

MySQL多选择在同一查询中具有多个不同的where条件

来自分类Dev

MySQL选择具有相同列的多个条件的查询

来自分类Dev

SQL查询以选择在同一列中具有不同值的子记录的父项

来自分类Dev

SQL以不同的顺序选择和分组具有相同单词的记录

来自分类Dev

SQL查询以选择具有不同主题的最新记录

来自分类Dev

仅选择具有相同值的记录

来自分类Dev

高级MySQL查询可获取具有相同ID和不同子ID的最新记录的范围

来自分类Dev

mysql在一个查询中具有不同记录的两个表

来自分类Dev

sql查询以选择两列中具有相同id但不同值的记录

来自分类Dev

具有不同参数的相同查询

来自分类Dev

具有相同表值的MYSQL查询

来自分类Dev

选择具有多个记录的正确MySQL记录

来自分类Dev

MySQL选择具有相同值的行

来自分类Dev

MySQL多选择在同一查询中具有多个不同的where条件

来自分类Dev

MySQL:选择2次相同字段且具有不同的值

来自分类Dev

Rails查询选择具有不同列的最后一条记录

来自分类Dev

MYSQL查询选择具有相同列和分组依据的

来自分类Dev

MySql-当一行与结果匹配时,选择具有相同字段的所有记录

来自分类Dev

选择在列中具有相同值而在另一列中具有不同值的记录

来自分类Dev

MySQL-选择具有相同值的多行,其中一行必须不同

来自分类Dev

MySQL查询以选择没有特定状态的记录,具有相同ID且具有多个状态的数据

来自分类Dev

MySQL 选择查询:SUM() 行具有不同的值,来自另一列

来自分类Dev

如何查询特定列Mysql中相同id具有不同值的位置

来自分类Dev

选择具有完全相同的多对多关联的记录

来自分类Dev

选择具有相同id但不同值的不同行

来自分类Dev

如何从数据库中选择具有相同查询但条件不同的表中的数据?

Related 相关文章

  1. 1

    MySQL选择不同的记录

  2. 2

    sql查询以选择两列中具有相同id但不同值的记录

  3. 3

    MYSQL从不同的行中选择2个具有相同值的不同字段

  4. 4

    MySQL多选择在同一查询中具有多个不同的where条件

  5. 5

    MySQL选择具有相同列的多个条件的查询

  6. 6

    SQL查询以选择在同一列中具有不同值的子记录的父项

  7. 7

    SQL以不同的顺序选择和分组具有相同单词的记录

  8. 8

    SQL查询以选择具有不同主题的最新记录

  9. 9

    仅选择具有相同值的记录

  10. 10

    高级MySQL查询可获取具有相同ID和不同子ID的最新记录的范围

  11. 11

    mysql在一个查询中具有不同记录的两个表

  12. 12

    sql查询以选择两列中具有相同id但不同值的记录

  13. 13

    具有不同参数的相同查询

  14. 14

    具有相同表值的MYSQL查询

  15. 15

    选择具有多个记录的正确MySQL记录

  16. 16

    MySQL选择具有相同值的行

  17. 17

    MySQL多选择在同一查询中具有多个不同的where条件

  18. 18

    MySQL:选择2次相同字段且具有不同的值

  19. 19

    Rails查询选择具有不同列的最后一条记录

  20. 20

    MYSQL查询选择具有相同列和分组依据的

  21. 21

    MySql-当一行与结果匹配时,选择具有相同字段的所有记录

  22. 22

    选择在列中具有相同值而在另一列中具有不同值的记录

  23. 23

    MySQL-选择具有相同值的多行,其中一行必须不同

  24. 24

    MySQL查询以选择没有特定状态的记录,具有相同ID且具有多个状态的数据

  25. 25

    MySQL 选择查询:SUM() 行具有不同的值,来自另一列

  26. 26

    如何查询特定列Mysql中相同id具有不同值的位置

  27. 27

    选择具有完全相同的多对多关联的记录

  28. 28

    选择具有相同id但不同值的不同行

  29. 29

    如何从数据库中选择具有相同查询但条件不同的表中的数据?

热门标签

归档