简体   繁体   English

将两个查询合并为一个查询

[英]Merge two queries into single query

I have this function which gets information from a database, namely the amount of songs in the songs table and the amount of artists from the artists table who have songs in the song table: 我有一个从数据库获取信息的功能,即歌曲表中的歌曲数量和歌手表中具有歌曲表中歌手的歌手数量:

function getInfo() {
    try {
        $q = $this->connection->prepare('SELECT artist_id FROM '.TBL_SONG.'');
        $q->execute();
        if ($q->rowCount() > 0) {
            $songs = $q->rowCount();
        } else {
            $songs = '0';
        }
        $q = $this->connection->prepare('SELECT id FROM '.TBL_ARTIST.' a WHERE EXISTS (SELECT * FROM '.TBL_SONG.' s WHERE a.id = s.artist_id)');
        $q->execute();
        if ($q->rowCount() > 0) {
            $artists = $q->rowCount();
        } else {
            $artists = '0';
        }
        return "<span class='italic'>Current songs: </span>".$songs." <span class='italic'>Active artists: </span>".$artists;
    } catch (PDOException $e) {
        echo RESULTS_ERROR;
        logError($e->getMessage());
    }
}

The first query gets the amount of songs from the song table and returns the rowcount to a variable. 第一个查询从歌曲表中获取歌曲数量,并将行数返回到变量。 The second query gets the artist id from the artist table, if they have songs in the songs table. 如果歌曲表中有歌曲,则第二个查询将从歌手表中获取歌手ID。 The result of this function is to return both values. 该函数的结果是返回两个值。

I want to be able to have both these values returned from a single query. 我希望能够从单个查询返回这两个值。 I've tried writing it as one query and fetching the results and using the count function to get the amount of the rows I need but this doesn't seem to work. 我尝试将其作为一个查询编写并获取结果,并使用count函数获取所需的行数,但这似乎不起作用。 Don't really know where I'm going wrong here. 真的不知道我在哪里出问题了。 Also, is it pointless checking if the row count is > 0 with an if statement and storing it in a variable as it'll return the value '0' anyway? 另外,使用if语句检查行数是否> 0是否毫无意义,并将其存储在变量中,因为它无论如何都会返回值'0'? Thanks. 谢谢。

This is actually pretty easy. 这实际上很容易。 You want to join the artist table and the song table using the artist id. 您要使用艺术家ID来加入艺术家表和歌曲表。 From that join, you want to know the number of distinct artist ids and song ids. 通过该联接,您想知道不同的艺术家ID和歌曲ID的数量。 The query you want will be something like this: 您想要的查询将是这样的:

select count(distinct a.id) as artists, count(distinct s.id) as songs
from artists a
inner join songs s on s.artist_id = a.id;

I highly recommend you get your query right from a console of some kind before plugging it into PHP. 我强烈建议您在将查询插入到PHP之前,先从某种类型的控制台获取查询。 The output will be a single row that looks something like this: 输出将是一行,看起来像这样:

+---------+-------+
| artists | songs |
+---------+-------+
|      20 |   150 |
+---------+-------+

From PHP, you just need to fetch the one-row answer and use it in your response: 从PHP,您只需要获取单行答案并将其用于响应中:

if ($q->rowCount() > 0) {
    $c = $q->fetchObject();
    $output = "<span class='italic'>Current songs: </span>{$c->songs}<span class='italic'>Active artists: </span>{$c->artists}";
}

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

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