Combining 2 queries into 1 - php

I'm looking to optimize two queries into one, if possible.
My first query searches for all the authors of a lyrics... Then, for each author found, i want to find the total numbers of lyrics the author was involded in...
Right now, im executing the first query and for each row found, i'm launching another query to get the authors total lyrics he was involved... So, if there is 4 authors i will end up launching 4 more queries...
That is to many queries in my opinion. That is why i've decided to write here, so i can get help on how to optimize my query...
This is the query i'm executing to get the author(s) responsable for a lyrics:
$sql = "SELECT author.author_id, author.name
FROM track INNER JOIN lyrics_author ON track.lyrics_id = lyrics_author.lyrics_id
INNER JOIN author ON lyrics_author.author_id = author.author_id
WHERE track.track_id = $trackid ";
This is the query to get the total number of lyrics the author as writing:
$total = "SELECT lyrics_author.author_id, count(*) as total
FROM lyrics_author
WHERE lyrics_author.author_id = $author_id
GROUP BY lyrics_author.author_id";
This is a sample of the code:
<?php
$trackid = 5;
$sql = "SELECT author.author_id, author.name
FROM track INNER JOIN lyrics_author ON track.lyrics_id = lyrics_author.lyrics_id
INNER JOIN author ON lyrics_author.author_id = author.author_id
WHERE 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(*) as total
FROM lyrics_author
WHERE lyrics_author.author_id = $author_id
GROUP BY 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 it posible to optimize this query? If yes, how? Is there a link you could refer, so i can learn...
Thanks
Marco

SELECT
author.author_id,
author.name,
COUNT(DISTINCT more_tracks.lyrics_id) AS total
FROM track
INNER JOIN lyrics_author USING (lyrics_id)
INNER JOIN author USING (author_id)
LEFT JOIN lyrics_author AS more_tracks USING (author_id)
WHERE track.track_id = $trackid
GROUP BY author.author_id

That's confusing as heck. Why are you passing in a trackid as a lyricsid when you have a property called lyricsid??
Anyway
Select author.author_id, author.name, Count(*)
inner join
(SELECT lyrics_author.author_id
FROM lyrics_author
INNER JOIN tracks ON track.lyrics_id = lyrics_author.lyrics_id
WHERE track.track_id = $lyricsid";
) as lyricalauthors
inner join lyrics_author on lyrics_author.author_id = lyricalauthors.author_id
On author.author_id = lyricalauthors.author_id
Group By Author.author_id,author.name
I think ...

Related

Selecting from comments and images table for each article in a while loop

I have an articles table that and I am displaying it in the homepage in a while loop. Inside the while loop I want to display the comments count and images count for each article.
It is working for me now, but it is three queries in total, I am trying to combine it in the first query and then just display all of them in one while loop. Here is what I am trying to achieve:Articles page
The current format I am following:
//a represents articles table, c represents comments table, i represents image table
$query = mysqli_query($conn, "SELECT a.a_id, a.title, a.datetime, a.user_id FROM a ORDER BY a.datetime DESC");
while($fetch = mysqli_fetch_assoc($query){
$imageQ = msqli_query($conn, "SELECT COUNT(image_path), image_path FROM i WHERE a_id = '$fetch['a_id']'");
$imageFetch = mysqli_fetch_assoc($imageQ);
$commentQ = mysqli_query($conn, "SELECT COUNT(comment_id) FROM c WHERE a_id = '$fetch['a_id']'");
$commentFetch = mysqli_fetch_assoc($commentQ);
}
I want to cram all of these queries into one single query that fetches the article and comments count and image count for each article and the first image.
The images and comments are separate dimensions of the data. So, you have to be careful about how to bring them together. In your case, you can aggregate the values before doing the joins:
SELECT a.a_id, a.title, a.datetime, a.user_id,
i.num_images, c.num_comments
FROM a LEFT JOIN
(SELECT a_id, COUNT(image_path) as num_images
FROM i
GROUP BY a_id
) i
ON i.a_id = a.a_id LEFT JOIN
(SELECT a_id, COUNT(comment_id) as num_comments
FROM c
GROUP BY a_id
) c
ON c.a_id = a.a_id
ORDER BY a.datetime DESC;
You can use mysql nested queries something like
SELECT a.,tab1.,tab2.* FROM a INNER JOIN (SELECT * FROM b ) as tab1 INNER JOIN (SELECT * FROM c) as tab2
Hope this can get you to get desired output.
Thanks

getting data from four tables with tight WHERE clause

i need help getting data from different tables and insert into other different table Here are the Queries
"SELECT commentID, date, comment, subject, parentID, aBUserID FROM comments WHERE status = 'APPROVED'"
"SELECT topicID, subForumID, aBUserID, lastPostID, views, replies, startDate FROM topic WHERE status = 'APPROVED' AND topicID = $parentid";
// $parentID need to be matched from above query parentID,
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID";
// $cmtaBUserID = aBUserID from first query
"SELECT userName FROM users WHERE aBUserID = $topicaBUserID";
//$topicaBUserID = aBUserID from second query
Last 2 queries are from same table but using different where clause
i used different inner join left join from solutions posted here but non of these worked for me stuck since last 2 weeks please help
PS data from all above Queries will be inserted to a single table i need these to be combined so i can have them all in one place
If you want to perform the operation in same query use 'OR'
"SELECT userName FROM users WHERE aBUserID = $cmtaBUserID OR aBUserID = $topicaBUserID";
Please try this
SELECT userName from users where aBUserID IN(SELECT aBUserID FROM comments WHERE status = 'APPROVED')
Couldn't test it but Maybe this is what you are looking for.
SELECT c.commentID, c.date, c.comment, c.subject, c.parentID, c.aBUserID,
t.topicID, t.subForumID, t.aBUserID, t.lastPostID, t.views, t.replies, t.startDate,
u.userName
FROM
comments c
left outer join topic t on t.topicID = c.parentID
left outer join users u on u.aBUserID = c.aBUserID and u.aBUserID = t.aBUserID
WHERE
c.status = 'APPROVED' and t.status = 'APPROVED';
try this:
SELECT
comment.[commentID],
comment.[date],
comment.[comment],
comment.[subject],
comment.[parentID],
comment.[aBUserID],
commentuser.[userName],
topic.[topicID],
topic.[subForumID],
topic.[aBUserID],
topic.[lastPostID],
topic.[views],
topic.[replies],
topic.[startDate],
topic.[userName]
FROM comments comment
LEFT OUTER JOIN users commentuser
ON commentuser.aBUserID = comment.[aBUserID]
LEFT OUTER JOIN
(
SELECT
t.[topicID],
t.[subForumID],
t.[aBUserID],
t.[lastPostID],
t.[views],
t.[replies],
t.[startDate],
u2.[userName] --user from users table joined to topics table
FROM topic t
LEFT OUTER JOIN users u
ON u.aBUserID = t.[aBUserID]
WHERE t.[status] = 'APPROVED'
) topic
ON topic.topicID = comment.parentID
WHERE comment.[status] = 'APPROVED'

php, sql individual links for Group_concat

I have three table, movies, artist, and role_table. The role_table holds only movieCode from movies and artistID from artist which are the primary keys of movies and artist table. i used join to get the values and Group_concat. here is my sql code
SELECT m.Title, m.MovieDesc, m.image, m.catagory, a.ID, a.FullName,
GROUP_CONCAT(a.FullName )
FROM movies m
JOIN role_table r ON m.movieCode =r.movieCode_FK
JOIN artist a ON r.artistID_FK = a.ID
where
movieCode = '$movieCode
This code is perfect fine and it works. what i want is to get the individual links for those artist which gets the values from The group_concat.
i am stuck here and any kind of help will be great.
Thanks :)
Doing it in php (assiming you are using a database class):-
<?php
$sql = "SELECT m.Title, m.MovieDesc, m.image, m.catagory, a.ID, a.FullName,
GROUP_CONCAT(a.FullName ) AS ConcatFullName
FROM movies m
JOIN role_table r ON m.movieCode =r.movieCode_FK
JOIN artist a ON r.artistID_FK = a.ID
where movieCode = '$movieCode'";
$db->query($sql) or die($db->error());
while($row = $db->fetch_assoc())
{
$fullnames = explode(',', $row['ConcatFullName']);
foreach($fullnames AS $aFullName)
{
echo "".htmlspecialchars($aFullName)."";
}
}
?>

Count one table and ORDER BY it

I want to order my blog posts by the amount of comments. To retrieve my posts etc i use:
$result = DB::query("SELECT R.rumorID, R.rumor_author, R.rumor_title, R.rumor_text, T.topic_name, A.first_name, A.surname, T.topicID, R.datum_geplaatst
FROM Rumor R JOIN Account A
ON A.userID = R.rumor_author
JOIN Topic T
ON T.topicID = R.topicID
ORDER BY $sortby $order");
foreach ($result as $rumor) {
$r_id = $rumor['rumorID'];
The $sortby and $order is a $_GET, i use links to "order by" the blog.
To retrieve the number of comments that comes with a blog post, i use:
$commentcount = DB::query("SELECT count(*)
FROM Rumor R JOIN Comment C
ON R.rumorID = C.rumorID
WHERE R.rumorID = :rumor", array(':rumor' => $r_id));
$rumortext = $rumor['rumor_text'];
$date = $rumor['datum_geplaatst'];
$countcomments = $commentcount[0]['count(*)'];
So, I can order by Date,title, topic, author etc. But how do i manage to order by the amount of comments?
This could be way off. Writing from the top of my head here:
SELECT R.rumorID, R.rumor_author, R.rumor_title, R.rumor_text, T.topic_name, A.first_name, A.surname, T.topicID, R.datum_geplaatst, count(c.rumorID) AS commentCount
FROM Rumor R
JOIN Account A ON A.userID = R.rumor_author
JOIN Topic T ON T.topicID = R.topicID
LEFT JOIN Comment C ON R.rumorID = C.rumorID
GROUP BY R.rumorID
ORDER BY commentCount ASC

Using INNER JOIN twice in the same query

I'm still trying to get the hang of SQL. I built 1 query that pulls out thread_id's from a filter_thread(contains 'filter_id' and 'thread_id' columns) table and a filter('filter_id and 'tag') table'
Then, I use an entirely different query to find the thread table contents, which contains the 'thread_id.'
I realize this isn't the best way to do it, but can't get a successful query. Would anyone be able to help?
$query = "SELECT ft0.thread_id
FROM filter f0
INNER JOIN filter_thread ft0 ON ft0.filter_id = f0.filter_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%'"
$result = $query->result_array();
$thread = array();
foreach ($result as $thread_id)
{
$id = $thread_id['thread_id'];
$query = $this->db->query("SELECT * FROM thread WHERE thread_id='$id'");
$thisRow = $query->result_array();
array_push($thread, $thisRow[0] );
}
THANKS!
You can do it in a single query, like so:
SELECT t.*
FROM filter AS f0
INNER JOIN filter_thread AS ft0
ON ft0.filter_id = f0.filter_id
INNER JOIN thread AS t
ON ft0.thread_id = t.thread_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%
select t.x, t.y, ... from thread t
inner join filter f on f.thread_id = t.thread_id
inner join filter_thread ft on ft.filter_id = f.filter_id
where bla bla
With maybe some group by...?
Try this
SELECT t.*, f0.*
FROM filter f0
INNER JOIN filter_thread ft0 ON ft0.filter_id = f0.filter_id
INNER JOIN thread t ON t.thread_id = ft0.thread_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%'"
GROUP BY f0.filter_id

Categories