sql server - Re-writing this sql query -


I have written a SQL query that receives unread messages but I think I will code (speed and readability) Can improve The first selection is for the COUNT function, the second is to group message according to chat_and, and the last nested selection is to select previous messages.

Please suggest me in advance thank you.

  SELECT COUNT (*) as unread (SELECT id FROM (SELECT id, conversation_id message WHERE to_id =? And read = 0 and NOT hide_from =? ORDER by SECRED DESC ) As temp_messages2  

as a query) by AS temp_messages group - You need to define all the columns which are not wrapped in the set in GROUP BY .

This is not clear, but if you want to count specific conversations, use it:

  SELECT COUNT (DISTINCT m.conversation_id) messages unread WHERE m.to_id =? And m.readed = 0 and m.hide_from! =?  

... Otherwise, use:

  SELECT COUNT (*) As messages have been unread with WHERE m.to_id =? And m.readed = 0 and m.hide_from! =?  
  • Subkeys are unnecessary
  • ORDER BY is a waste of resources because it is not used in the final output nor top is being used
  • Group By will not work because MESSAGES.id is not in the list of columns < / Li>

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -