I have a table that looks like this
+------+------------------------------------+
| id | details |
+------+------------------------------------+
| 1 | {"price":"24.99","currency":"USD"} |
+------+------------------------------------+
Is it possible to, with a single MySQL select statement, obtain the value of price 24.99?
Yes, you can using JSON_EXTRACT
It probably should be like:
SELECT JSON_EXTRACT(details, "$.price")
FROM table_name
or another form:
SELECT details->"$.price"
FROM table_name
(I don't have MySql to test it)
Note that the price in your JSON stored as a string, not a number and you probably would want to cast it to a DECIMAL.
Related
My team has stored array data as a string in MySQL like below
["1","2","22","11"]
How can we select data from the table where the column contains a certain branch number.
Example of table
sno | Name | Branch
1. | Tom. | ["1","2","22"]
2. | Tim. | ["1","2"]
Can you suggest a query to select all rows containing branch 2?
We tried using FIND_IN_SET() but that is not working as the double quotes and square brackets are also a part of string.
Use like:
select *
from mytable
where Branch like '%"2"%'
I have table region with fields
id | name | dates
Sample data
1 | local | "2018-01-01", "2018-01-02", "2018-01-03"
I want a query like this
SELECT * FROM region WHERE "2018-01-02" in (region.dates)
but this does not work. I do not use json data in this case. How can I change it?
(My)SQL doesn't work like that as it will see region.dates as a simple string. In which case, you can do SELECT * FROM region WHERE dates LIKE '%"2018-01-02"%';
However, a better solution would be to devolve that column into another table.
I'm storing permissions into DB with Array JSON String, and i want select them by permission specific permission. at this time I'm selecting them like this:
1 | Dog | [3,4]
2 | Cat | [33,4]
3 | Tiger | [5,33,4]
4 | wolf | [3,5]
SELECT * FROM `pages` WHERE access REGEXP '([^"])3([^"])'
it works but not as it should work. This query gives me all records which contains 3 but also it gives which contains 33. my question is how i must format my regexp to get row by specific value into json string.
p.s i have mysql 5.5 so as i know on this version json functions is not supported
If you only have numbers in the fields, you can alter your regexp to only take values where the string you are looking for (here the '3') does not have another number immediately close to it :
SELECT * FROM `pages` WHERE access REGEXP '([^"0-9])3([^"0-9])'
REGEXP '[[:<:]]3[[:>:]]'
That is, use the "word boundary" thingies.
I am using Apache Drill to run SQL queries on a HBase table. The value in one of the columns is:
0: jdbc:drill:schema:hbase:zk=localhost> select cast(address['street'] as varchar(20)) from hbase.students;
+------------+
| EXPR$0 |
+------------+
| {"id": 123} |
+------------+
1 row selected (0.507 seconds)
I would like to access the id field using a query. Something like:
0: jdbc:drill:schema:hbase:zk=localhost> select tbl.address['street']['id'] from hbase.students as tbl;
+------------+
| EXPR$0 |
+------------+
| null |
+------------+
As you can see, this does not work. I am run to similar queries on JSON data in a file. My question is can I query JSON data in HBase.
OK. I found the answer to this question, in case someone else has the same requirement.
The first step is to convert the HBase data to JSON using the built-in convert_from() function. A view can be created against which the queries can be run.
> create or replace view Street as select convert_from(Students.address.street, 'JSON') json from hbase.Customer;
Then, run query against the view
> select * from Street;
> select Street.json.id from Street;
You can also use a subquery to convert the data in your HBase column into JSON:
select t.json.id
from (select convert_from(Students.address.street, 'JSON') json
from hbase.Customer) t;
I have a bunch of regular expressions in a MySQL table. I want to know whether a given string matches a part of any regular expression or not.
Eg:
+----+--------------------------------+
| id | regexps |
+----+--------------------------------+
| 1 | foo-[0-9]*\.example\.com |
| 2 | (bar|tux)-[0-9]*\.example\.com |
+----+--------------------------------+
(The regexps attribute is of VARCHAR type)
foo-11.example.com matches the first regexp.
I want a MySQL query that returns the first row with the given string as foo-11
This should do it on MySql:
select * from table t where 'foo-11.example.com' rlike t.data;
There are other ways in PostGreSQL. Here's the link from where I have referenced this:
http://www.tutorialspoint.com/mysql/mysql-regexps.htm
Match a Query to a Regular Expression in SQL?
PS: Using * is tricky though!