Skip to content

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…

Problem reading uniqueidentifier from SQL response

I have tried to find the solution for this problem, but keep running my head at the wall with this one. This function is part of a Go SQL wrapper, and the function getJSON is called to extract the informations from the sql response. The problem is, that the id parameter becomes jibberish and does not match th…

Select using WHERE if any exist, else don’t use WHERE

I want to select from a table where column = ‘n’. If the result has 0 rows, I want to remove that conditional. Some pseudo-sql might be: I will only be using the first row in either result, so it would also be acceptable to somehow sort the result such that rows where COL_B = ‘mystring&#8217…

how to divide the amount for each record on specific condition

I have a table let’s assume X and I want to join it to another table Y this the content of X table Y If I made a relationship between X & Y tables the amount it will be duplicated, so if I used sum function the total will be wrong I decided to divide the amount between the stores

PHP/SQL select based on sum result

I’m having a hard time limiting my query results based on sum. Example code: I want to only select states where 0 cities are “covered”. Running my code above the “having” line seems to be ignored (it includes both zero and non-zero cases). I tried using “where” instea…

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

Query using group by with steps/range over large data

I have a table that stores a sensor temperature readings every few seconds Sample data looks like this I want to have the average temperature per device so I ran the following query Where I can pass the time range I want. My issue is that this gives me total average but I want steps or range I want to