MySQL Query - Convert ResultSet to JSON - mysql

I need to convert select * from {table_name} into JSON output.Instead of specifying separate key-value pair in 'JSON_OBJECT'.
I have tried this query
select JSON_ARRAYAGG(json_object("col_name_1",alias.column_1,"col_name_2",alias.column_2)) from {table_name} alias;
This one is giving me results what I need, but my question is instead of giving as key-value pair in json_object I need to fetch all columns by giving something like '*'.
MySQL Database Version: 5.7.24

Related

Convert whole row into a JSON string in MySQL

I want to log deleted rows into a single column, by converting them into a JSON string.
How can I convert the whole row into JSON without specifying column names one by one?
For example result I want to convert the result of the following query into a JSON, without specifying each column name:
select * from table_1 limit 1

Selecting rows in mySQL table by comparing the value to python dictionary

I have 2 databases one in mongoDB and one table in mySQL. I am taking data from mongoDB which is being stored in a dictionary and looks like this-
dictionary = {
"message_code": "12345"
}
I want to match this message_code value in the mySQL tables colums called final_code and retrieve table rows which match the message_code. How can I do that?
SELECT *
FROM mysql_table
WHERE final_code = dictionary.message_code
Something like this?
You pass a string to mysql, not python objects. So you must build that string interpolating the values you want in python, i.e.
'SELECT * FROM mysql_table WHERE final_code='+dictionary["message_code"]
and pass that string to mysql as query.

Match Numeric value after comma separated, concatenated by underscore values using MYSQL/MariaDB & REGEXP_SUBSTR

I have field column values stored like:
texta_123,textb_456
My SQL:
SELECT *
FROM mytable
WHERE 456 = REGEXP_SUBSTR(mytable.concatenated_csv_values, 'textb_(?<number>[0-9]+)')
NOTE: I'm aware there are multiple ways of doing this, but for the purposes of example I simplified my query substantially; the part I need to work is REGEXP_SUBSTR()
Effectively, I want to: "query results where an id equals the numeric value extracted after an underscore in a column with comma-separated values"
When I test my Regex, it seems to work fine.
However, in MySQL (technically, I'm using MariaDB 10.4.19), when I run the query I get a warning: "Warning: #1292 Truncated incorrect INTEGER value:textb_456"
You should seriously consider fixing your database design to not store unnormalized CSV data like this. As a temporary workaround, we can use REGEXP_REPLACE along with FIND_IN_SET:
SELECT *
FROM mytable
WHERE FIND_IN_SET(
'456',
REGEXP_REPLACE(concatenated_csv_values, '^.*_', '')) > 0;
The regex trick used here would convert a CSV input of texta_123,textb_456 to just 123,456. Then, we can easily search for a given ID using FIND_IN_SET.

Get Column names of MySQL table with PDO

I'm trying to write a function that returns an array of column names for a MySQL table.
When I use something like this in HeidiSQL
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE table_schema = 'myDB'
AND TABLE_NAME = 'myTable'
I get exactly what I want. A single column of output showing the column names.
when I took the above code and used it in a PHP program using PDO calls every array value is of the format:
string(xx) "column_name"
I do not want the leading "string(xx)" in front of the column name.
I'm using the pdo function as follows:
fetchAll(PDO::FETCH_COLUMN)
I don't see other PDO fetch options to just give me the column names, without the leading "string(xx)" value.
I could parse the results and strip the leading string(xx) value, but I was wondering if there's an easier/better trick.
Oh, mental lapse... as I dug deeper into my output array, I was thrown off by my output because I was using var_dump. var_dump prefaces the values with the "string(xx)" value. MySQL isn't the one putting the "string(xx)" prefix.
Live and learn...

How to extract values from a numeric-keyed nested JSON field in MySQL

I have a MySQL table with a JSON column called sent. The entries in the column have information like below:
{
"data": {
"12":"1920293"
}
}
I'm trying to use the mysql query:
select sent->"$.data.12" from mytable
but I get an exception:
Invalid JSON path expression. The error is around character position 9.
Any idea How I can extract the information? The query works fine for non-numeric subfields.
#Ibrahim,
You have an error in your code. If you use number (or spaced words) as key in a JSON data type in MySQL, you'll need to double-quote it.
Therefore, the correct MySQL statement in your case is:
select sent->'$.data."12"' FROM mytable;
Thanks,
#JeffreyKilelo