i´m new in SQL and want to solve following case.
I have a table in a MariaDB named inventories. In this table are 2 columns: identifier and data.
I want to select and output a specific part in the column data, if the identifier is a specific one.
For example:
identifier
data
steam:xxxxxxxxxxxxxxx
...some data...,"Serial":"ZF3CJ8L1rrKjwP7nKTzb",...some more data...
The only part, that i want in the output is this: "Serial":"ZF3CJ8L1rrKjwP7nKTzb" but the part behind "Serial":" is a random value.
How can i solve this?
(i put a screenshot in here, table does not work for me in editorscreenshot of table in stackoverflow editor.)
You can try using the SubString function, which only returns a certain amount of character and starts a whatever character you want.
I got it!
SELECT (SUBSTRING(data, LOCATE('"Serial":"', DATA) + 0, 31)) FROM inventories where identifier = 'steam:xxxxxxxxxxxx';
Related
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 simple scenario as follow:
I have 2 column : 1) id and 2) text(which is long text format)
I use the simple query to extract all info from mysql as follow:
select id,text from dbtest
but the problem is for different id I might have the same text but in retrieval time I do not want to return rows with the same text again and again so I do not want to return the repeated texts,I tried to use distinct but it was not working,
How can I do that , any idea?
One option is to use user-defined variables:
select id,
#text:=if(#text=text, '', text) text
from dbtest, (select #text:='') t
order by text
SQL fiddle demo
I would generally recommend doing this on the application side rather than the database though.
Distinct works on all selected columns. You must use a GROUP BY:
SELECT id,text FROM dbtest GROUP BY text
So I have two questions, can you post the code you are using for us? And are some of the fields for the text empty?
The query might be detecting a "Null" kind of event in which if the field is empty, it repeats the last one because the variable might not be getting unset. In which case you might just want to unset your variables at the end of your (I'm assuming) while loop.
I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.
I have tried using 'LIKE' but it runs into problems which i will explain below.
i have a string column that looks like any of these.
"1010, 2020, 3030"
"1010"
""
I want to be able to see if this string contains a single ID. e.g 2020. if it does then return the row. I tried using like but if the id is 20 it will return the row because 2020 contains 20.
Selecting the entire db and then using a delimiter to go through all the strings will take far too much time. Is it possible to implement this?
This is why you don't store multiple values in a single field. Because your bad design, this is the query structure you'll have to use EVERY SINGLE TIME to compensate for it:
WHERE
foo = 2020 // exact match, only value in field
OR foo LIKE '2020,%' // value is at start of field
OR foo LIKE '%,2020,%' // value is somewhere in the middle of the field
OR foo LIKE '%,2020' // value is at the end of the field
Or you could have had a properly normalized design, and just done
WHERE childtable.foo = 2020
and be done with it.
First, you should not store lists of things in string variables. SQL has a very nice data structure for lists. It is called a table. Each row in such a table would have an id and one value from the list.
That said, sometimes you are stuck with data like this. In that case, you can use find_in-set():
where find_in_set('20', replace(stringcolumn, ', ', ',')) > 0;
You can also do the logic with like, but MySQL has the convenient built-in function for this.
EDIT:
If you want to do this with like:
where concat(',', stringcolumn, ',') like '%,20,%'
Note that the delimiters "protect" the values, so 20 is not confused with 2020.
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".