I've got a table that contains a column (rated_by) with some id's in json format. Example: ["59"]
I would like to search for a specific number in all rated_by columns of the entire table.
I've done this before in the same table for another column that also contain id's in the same format as above with the following query (for the column producten):
SELECT * FROM review WHERE JSON_SEARCH(producten,"one", "26") IS NOT NULL ORDER BY useful DESC
This works fine and this is because every row of producten is filled with json, not a single one is empty, but for rated_by some rows can be empty.
Using the exact same query like this:
SELECT * FROM review WHERE JSON_SEARCH(rated_by,"one", "59") IS NOT NULL
I get: Invalid JSON text in argument 1 to function json_search: "The document is empty." at position 0.
I tested it by emptying one row of producten and trying the working query again, it stopped working.
So the problem is my query stops working when one row of rated_by does not contain json or is empty.
Why is that? I thought using IS NOT NULL would tackle this.
I am using MYSQL.
I fixed it using JSON_VALID (first check if the row contains valid json, only if it does, retrieve data) as commented above.
Working query:
SELECT * FROM review WHERE JSON_VALID(rated_by) AND review_id = "10" AND JSON_SEARCH(rated_by,"one", "59") IS NOT NULL
I have a MySQL table setup where one column's values are a string of comma-separated True/False values (1s or 0s). For example, in the column, one field's value may be "0,1,0,0,0,0,1,1,0" and another may be "1,0,0,1,1,1,0,0,0" (note: these are NOT 9 separate columns, but a string in one column). I need to QUERY the MySQL table for elements that are "true"(1) for the "nth element" of that column's value/string.
So, if I was looking for rows, with a specific column, where the 3rd element of the column's value was 1, it would produce a list of results. So, in this case, I would only be searching for "1" in the fth place (12345 = X,X,X...) of the string (X,X,1,X,X,X,X,X,X,X). How can I query this?
This is a crude example of what I am trying to do ...
"SELECT tfcolumn FROM mytable WHERE substr({tfcolumn}, 0, 5)=1"
{tfcolumn} represents the column value
5 represents the 5th position of the string
=1 represents what I need that position to equal to.
Please help. Thanks
You can't. Once you put a serialized data type into a column in SQL (like comma separated lists, or JSON objects) you are preventing yourself from performing any query on the data in those columns. You have to pull the data in a different way and then use a program like python, VB, etc to get the comma separated values you are looking for.
Unless you want to deal with trying to make this mess of a query work...
I would recommend changing your table structure before it's too late. Although it is possible, it is not optimized in a format that a DBMS recognizes. Because of that the DBMS will spend a significant amount of time going through every record to parse the csv values which is something that it was not meant to be doing. Doing the query in SQL will take as much time (if not more time) than just pulling all the records and searching with a tool that can do it properly.
If the column contains values exactly like the ones you posted, then the Nth element is at the 2 * N - 1 position in the comma separated list.
So do this:
SELECT tfcolumn
FROM tablename
WHERE substr(tfcolumn, 2 * 5 - 1, 1) = '1'
Replace 5 with the index that you search for.
See the demo.
Or remove all commas and get the Nth char:
SELECT tfcolumn
FROM tablename
WHERE substr(replace(tfcolumn, ',', ''), 5, 1) = '1'
See the demo.
Try this
if substring_index(substring_index('0,1,0,0,0,0,1,1,0',',',3),',',-1)='1'
The first argument can be your column name. The second argument (',') tells the function that the string is comma-separated. The third argument takes the first 3 elements of the string. So, the output of inner substring_index is '0,1,0'.
The outer substring_index has -1 as the last argment. So, it starts counting in reverse direction & takes only 1 element starting from right.
For example, if the value in a particular row is '2,682,7003,14,185', then the value of substring_index(substring_index('2,682,7003,14,185',',',3),',',-1) is '7003'.
I have table matches with these columns:
|sport|region|country|league
If the input string for sport is empty I want to return everything and don't bother with matching region, country etc.
If sport is not empty then find rows with matched sports and proceed to region and do the same thing.
Is this possible to do in SQL? I know I can filter this out in PHP and then run different SQL queries.
Try this
WHERE (sport_param IS NULL OR sport_column = sport_param)
You might want to use the LIKE operator or consider case-insensitive checking instead of simply comparing the exact sport.
Take a look at this
... you can basically make an if statement in SQL to match whether sport is empty or not and depending on that execute two different queries.
How do I capitalize all text in a column of data in an Access Query while keeping the name of the field the same?
I tried entering "SPEC: StrConv([SPEC],3), but I get an error that I have a circular argument (which, isn't too surprising). So how do I get around this?
Is there a totally different approach to capitalizing in queries?
Given: we have a field named [SPEC].
Problem: need query to grab [SPEC] and convert it to all caps, but
with the same field name
Added: We will call the table that holds the field [SPEC],
[tblTable]
Solution:
What we need to put in the query builder is the following:
SPEC: UCase([tblTable].[SPEC])
That way the machine can figure out that Query.SPEC isn't the same identifier as tblTable.SPEC
Equivalently:
SELECT UCase([tblNames].[FirstName]) AS FirstName
FROM tblNames;
How about using the Ucase function
Ucase(String)
Is it possible to find the min value of a column of floating numbers using a mysql function? Suppose I have the following table:
id | value a | 24.88 a | 119.99
If I try:
SELECT MIN(value) FROM [table name] GROUP BY id;
mysql returns:
119.99
After testing this with different floating numbers I believe that this is the case because mysql takes the first character in each of the strings "1" and "2" and then selects a min based on which character is smaller.
I've read through this forum and others trying to find an answer but it seems nobody has raised this problem.
I should mention I've also tried CEIL(value) but that function also seems to have some bugs and I'd prefer to keep the number a floating number and not an integer.
Thanks everyone.
It looks like the column is being stored as a character-based data type. You can solve this in one of two ways:
Change the column type to a numeric type
change the query to add CAST around the value: MIN(CAST(value AS DECIMAL))
The column change might look like this:
ALTER TABLE my_table MODIFY COLUMN value double;
And, as far as I know, MySQL will attempt to convert the data for you. See the note here, which states it "tries".