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'm querying an SQL database that I have read only access to (Cannot edit tables/create columns etc)
My table contains a column with JSON strings that have (Actual strings are much larger, this is just an example) the following syntax
{"value":"442","country":"usa"}
I would like to add the values contained in the JSON string from each row together and output it as readable, if this is possible?
The values are in the same point of the JSON, as shown above. The values vary in length also, most are 3/4 characters long.
Try the following (for MySQL v5.7+):
select sum(json_extract(jsonString, '$.value')) from mytable;
An example of this is here.
I'm attempting to query on a field/column/table in a MySQL DB where the field type is varchar, but some values contains spaces. In my query, I tried to put the exact string to match on in single quotes in a where clause. However, the only rows that are returned are the strings that do not contain spaces.
Here are the values stored in the table/column:
Here is the query and the result that is only returning fields without spaces:
I expected to find a row for "New Business", a row for "Monetary Endorsement", etc. Any idea on how I can modify my query to return the desired fields? Thanks for your help in advance!
Maybe the other values have leading or trailing spaces. You can either use one of the suggestion below:
1.) Use TRIM()
WHERE TRIM(PTD_TRANS_TYPE) = 'NEW BUSINESS'
2.) Use LIKE
WHERE PTD_TRANS_TYPE LIKE '%NEW BUSINESS%'
Here's a Demo.
SELECT ID FROM REPORT where ID NOT LIKE '%-G'AND ID NOT LIKE '%-H';
The above query isnt returning what I expect. It is returning 5 row sets when it should 6. 6 ID matches the condition of query. Do I need to use a regular expression to get the right row sets? I have tried '%\-G' with single backslash and double backslash '%\\-G' both return the desired 6 row sets. Which should it be?
Sample data:
87878
54545
21545-G
45487
45454
45458
78741
23232-H
'%\-G' this is the correct way of doing it and not '%\\-G'
For further reference follow : MYSQL Escape Sequences