SQL Adding count (from 2 tables) to existing select (of 3 tables) - mysql

Doing a query on forum database. I am using this query to get thread name, poster, date etc.
(Left only thread_subject for now)
SELECT `thread_subject` FROM `fusion_posts` JOIN `fusion_threads`
ON fusion_posts.thread_id=fusion_threads.thread_id JOIN `fusion_users` ON
fusion_posts.post_author=fusion_users.user_id
GROUP BY fusion_posts.thread_id ORDER BY `post_id` DESC LIMIT 16
Basically, I also need to add something like the count below to the existing select, to count posts of each thread.
SELECT COUNT(*) AS PostCount FROM fusion_posts,fusion_threads WHERE fusion_threads.thread_id = fusion_posts.thread_id group by fusion_threads.thread_id
How could I do that?

Try this:-
SELECT `thread_subject`, COUNT(*) AS PostCount
FROM `fusion_posts` JOIN `fusion_threads`
ON fusion_posts.thread_id=fusion_threads.thread_id JOIN `fusion_users`
ON fusion_posts.post_author=fusion_users.user_id
GROUP BY fusion_posts.thread_id, `thread_subject`
ORDER BY `post_id` DESC
LIMIT 16

Related

mysql query with SELECT IN SELECT in more efficient way

Please help me optimize query about getting reccomended (rec) for movies. I have many records and query run quite slow. The following query run for 2 mins
SELECT rec.toMovieID, sum(rec.score)
FROM rec
WHERE movieID in
(SELECT movieid as movieID FROM userFavorites as ufv WHERE ufv.userid = 29)
GROUP BY rec.toAMovieID
ORDER BY rec.score DESC
LIMIT 10
Do you think I can optimize it more?
You can use an inner join instead of a subselect
SELECT
rec.toMovieID,
sum(rec.score)
FROM rec INNER JOIN userFavorites ON rec.movieID = userFavorites.movieid
WHERE
userid = 29
GROUP BY rec.toAMovieID
ORDER BY rec.score DESC
LIMIT 10
You should set indexes on rows in where clause, at least for movieid and userid. (If not allready done)
You can use exists:
SELECT rec.toMovieID, sum(rec.score)
FROM rec r
WHERE EXISTS (SELECT 1 FROM userFavorites as ufv WHERE ufv.userid = 29 and ufv.MovieId = r.MovieId)
GROUP BY rec.toAMovieID
ORDER BY rec.score DESC
LIMIT 10;
You have to be careful using a join because of duplicate records.

How to avoid multiple results from sub query

Hi guys i am new bie to MySQL,this might be
easier to question but i am totally new for mysql.
i have two tables order and shops the Desc of two
tables look like this....
OrdersTable.
order id:
ordername:
shopnum(fk)
Shopstable*
shopname:
shopnum(pk):
i am using sub query like this,to get the shops name which have most number of orders....
like...19 xyzshop
. 13 hjjddshop
. 6 reebok shop
select shopname
from shopstable
where shopnum in
(select count(orderid) as highest ,shopnum
from orderTable
group by shopnum)
it throws error,display column should be 1,its because the subquery is returning 2 results...so how do i avoid that and get the appropriate result...help will be appreciated...:):)
It's not complaining because the subquery returns 2 results but two columns. But even if it did only return a single column, it would return 2 results and the main query would do the same.
No need for a subquery in any case:
SELECT s.shopname
FROM Shopstable s
JOIN OrdersTable o ON s.shopnum=o.shopnum
GROUP BY s.shopname
ORDER BY count(*) DESC
LIMIT 1
Use this:
select shopname
from shopstable
where shopnum in
(select shopnum
from orderTable
group by shopnum
order by count(*) DESC
limit 1)
remove count(orderid) as highest , in your query. you should only select one column in your subquery
i think you want something like this...
SELECT shopname, count(*) as highest
FROM shopstable
INNER JOIN orderTable ON (shopstable = orderTable.shopname)
GROUP BY shopstable.shopname
i think this is the result you want?

MySQL - get data from 2 tables similtanusly

alright so i've got two tables here, gforum_Post and gforum_PostView
i'm trying to get information from the table gforum_Post where it has the same post_id.
"SELECT post_subject, post_username FROM `gforum_Post` WHERE post_id = 341"
i've got it to display what i want but it's hard coded, the data in the columns will change. here's what i've got so far
SELECT post_id_fk, post_thread_views FROM `gforum_PostView` ORDER BY `gforum_PostView`.`post_thread_views` DESC
SELECT post_subject, post_username FROM `gforum_Post` WHERE post_id = 341
341 is the value that is hardcoded
You can use join to join the tables:
SELECT post_id_fk, post_thread_views, post_subject, post_username FROM `gforum_PostView`
join `gforum_Post` on post_id =post_id_fk
ORDER BY `gforum_PostView`.`post_thread_views` DESC
I think what you're looking for is the JOIN query:
SELECT GPV.post_id_fk, GPV.post_thread_views, GP.post_subject, GP.post_username
FROM gforum_Post GP, gforum_PostView GPV
WHERE GP.post_id = 341
ORDER BY GPV.post_thread_views DESC

How to combine 2 mysql queries

I have the following 2 queries.
Query 1 :
select distinct(thread_id) from records where client_name='MyClient'
Query 2 :
select max(thread_no) from records
where thread_id='loop_result_from_above_query' AND action='Reviewed'
Is it possible to combine them into a single query ?
The second query is run on every result of the first query.
Thank you.
See attached image of a small snippet of mysql records.
I need a single mysql query to output only records which have action="MyAction" as the latest records for a given set of thread_ids. In the sample data set : record with Sr: 7201
I hope this helps in helping me :)
SELECT client_name, thread_id, MAX(thread_no) max_thread
FROM records
WHERE action='Reviewed' AND client_name='MyClient'
GROUP BY client_name, thread_id
UPDATE 1
SELECT a.*
FROM records a
INNER JOIN
(
SELECT thread_id, max(sr) max_sr
FROM records
GROUP BY thread_id
) b ON a.thread_id = b.thread_id AND
a.sr = b.max_sr
WHERE a.action = 'MyAction'
You can use SELF JOIN, but it is not advisable and will impact your query performance. Please check below query for your reference
SELECT DISTINCT r1.thread_id, MAX(r2.thread_no) from records r1 LEFT JOIN records r2 ON r2.thread_id=r1.thread_id WHERE r1.client_name='MyClient' AND r2.action='Reviewed'
SELECT a.maxthreadid,
b.maxthreadno
FROM (SELECT DISTINCT( thread_id ) AS MaxThreadId
FROM records
WHERE client_name = 'MyClient') a
CROSS JOIN (SELECT Max(thread_no) AS MaxThreadNo
FROM records
WHERE thread_id = 'loop_result_from_above_query'
AND action = 'Reviewed') b
Try this.
SELECT *
FROM (SELECT Row_number()
OVER (
partition BY thread_id
ORDER BY thread_no) no,
Max(thread_no)
OVER(
partition BY thread_id ) Maxthread_no,
thread_id,
action,
client_name
FROM records
Where client_name = 'MyClient') AS T1
WHERE no = 1
AND action = 'Reviewed'

Counting rows from a subquery

How could I count rows from a SELECT query as a value?
Such as
SELECT FUCNTIONIMLOOKINGFOR(SELECT * FROM anothertable) AS count FROM table;
So that count is an integer of how many rows the subquery SELECT * FROM anothertable returns.
EDIT
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(f.FlagTime)
FROM Flags as f
JOIN Posts as p
ON p.PostPID = f.FlagPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30
SELECT ( SELECT COUNT(id) FROM aTable ) as count FROM table
I assume your example is a truncated version of your actual query, so perhaps you should post what you are after to get a, possibly, more optimal query.
EDIT
Working directly from my brain, something like this should be more optimal.
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep, COUNT(v.FlagTime) as postFlags
FROM Flags as f
JOIN Posts as p ON p.PostPID = f.FlagPID
JOIN Users AS u ON p.PostUID = u.UserUID
LIMIT 0, 30
GROUP BY p.PostPID
ORDER BY PostTime DESC
You can say
SELECT COUNT(*) FROM anothertable
which will return a numeric value, which you can use in another query, such as in the select list of another query, or as a condition in another query.
SELECT someVariable FROM table
WHERE (SELECT COUNT(*) FROM anotherTable) > 5
OR
SELECT someVariable, (SELECT COUNT(*) FROM anotherTable) as count FROM table