php - Combining 2 queries into 1 -
i'm looking optimize 2 queries one, if possible.
my first query searches authors of lyrics... then, each author found, want find total numbers of lyrics author involded in...
right now, im executing first query , each row found, i'm launching query authors total lyrics involved... so, if there 4 authors end launching 4 more queries... many queries in opinion. why i've decided write here, can on how optimize query...
this query i'm executing author(s) responsable lyrics:
$sql = "select author.author_id, author.name track inner join lyrics_author on track.lyrics_id = lyrics_author.lyrics_id inner join author on lyrics_author.author_id = author.author_id track.track_id = $trackid "; this query total number of lyrics author writing:
$total = "select lyrics_author.author_id, count(*) total lyrics_author lyrics_author.author_id = $author_id group lyrics_author.author_id"; this sample of code:
<?php $trackid = 5; $sql = "select author.author_id, author.name track inner join lyrics_author on track.lyrics_id = lyrics_author.lyrics_id inner join author on lyrics_author.author_id = author.author_id track.track_id = $trackid "; $result_author = @ $conn->query($sql); while ($row_author = $result_author->fetch_assoc()) { $author_id = $row_author['author_id']; $total = "select lyrics_author.author_id, count(*) total lyrics_author lyrics_author.author_id = $author_id group lyrics_author.author_id"; $result_total_lyrics = @ $conn->query($total); $t = $result_total_lyrics->fetch_assoc(); echo $t['total']; $result_total_lyrics->free(); } $result_author->free(); ?> is posible optimize query? if yes, how? there link refer, can learn...
thanks marco
select author.author_id, author.name, count(distinct more_tracks.lyrics_id) total track inner join lyrics_author using (lyrics_id) inner join author using (author_id) left join lyrics_author more_tracks using (author_id) track.track_id = $trackid group author.author_id
Comments
Post a Comment