Skip to content

Tag: mysql

Apply limit to CASE statement

I am trying to collect counts of different columns for players (you don’t need to know the ins and outs of all the columns). We have a player_1 and player_2 column so I have made it distinct to get total counts, as you can see below: I want to also get the frequency of goals for last 32 games for

mySQL foreach row run an update

Progress table has progressId, lessonId and uniqueId TestResults table has progressId TestResults table now has lessonId and uniqueId This SQL statement foreach progrssId in TestResults will get lessonId and uniqueId from Progress: Now I want to update lessonId and uniqueId in TestResults table foreach associ…

SQL count rows where column value contains string

I want to count the number of rows where a certain column has a substring in the column value. This doesn’t work Example: I want to count the rows including ‘no’ Expected result: 2 Because ‘no’ is a substring in ‘noa’ and ‘noit’. Answer Use a case expressi…

How to select matching results with other random results in mysql

I want to select all the matching results in a database table with also random results but with the matching results being at the top. With the way, I am doing now I am using two queries first one being the matching query, and if the count is zero I now select random results. I would like to do this

MySQL – Slow query

Sorry for the long post, but the tables involved are quite big. When I run the query bellow it normally takes about 1m to run. However, when I remove the correlated sub-query I get the query down to 15 seconds. So I think that’s the actual problem. Problem is, I don’t really know how to get the SU…