Mysql for NodeJS not expanding array in queries - mysql

I'm using NodeJS and the mysql package from npm. I'm having trouble selecting specific rows from the database by ID. I'm using a simple WHERE id IN (...) query, and passing in the ids as an array of numbers.
According to the documentation,
Arrays are turned into list, e.g. ['a', 'b'] turns into 'a', 'b'
So I've written this code to debug the SQL it generates:
console.log(ids);
console.log(this.connection.format(
"SELECT `invitations`.* FROM `invitations` WHERE `invitations`.`id` IN (?)",
ids
));
I expect to see a list of ids first, then the SQL statement where those ids are in the IN section of the query.
However, only the first id is present in the query:
console.log tests/fakers/InvitationFaker.ts:70
[ 207, 208 ]
console.log tests/fakers/InvitationFaker.ts:71
SELECT `invitations`.* FROM `invitations` WHERE `invitations`.`id` IN (207)
Why doesn't the query look like this:
... WHERE `invitations`.`id` IN (207, 208)
?

try to convert the ids to string with "," between the ids
this.connection.format(
"SELECT `invitations`.* FROM `invitations` WHERE `invitations`.`id` IN (?)",
ids.join(",")
));

I'm an idiot :) I forgot that the second argument can be either a single value or an array, but when it's an array, the first value fills the first ? and so on. To expand an array, I need to do this:
" ... WHERE `id` IN ?", [ids]
(note the double array, since ids is already an array). For example:
" ... WHERE `id` IN ?", [[1, 2, 3, 4]]

Related

How to get another key value from an array of json?

Lets say I have these arrayed JSON values
[{operation_id: 2, operation_name: FAITHFUL BELIEVERS},
{operation_id: 3, operation_name: SAMPLE OP},
{operation_id: 4, operation_name: SAMPLE OP 2}]
Now I will select the operation name 'SAMPLE OP' but I want to display the value of its operation_id. How would I do that?
Your JSON is a list of maps, so use where on the list to filter it by your predicate. Better still, use firstWhere as we assume there's just one match.
The match function returns true if the operation name member of the map matches.
firstWhere returns the first matching map, and you want the operation id member of that map.
final id = list
.firstWhere((m) => m['operation_name'] == 'SAMPLE OP')['operation_id'];

Postgres searching through arrays within JSON

Many similar questions, but unfortunately non helped me solve my problem. I tried to && and #> and similar, but no success.
I have a postgres DB with a table, that has a "value" column typed "json". All rows have the same basic structure, a simple JSON object, with the att "value" holding an array of strings:
{
value: ['one', 'two', 'three']
}
I need to make a query accepting an array of strings and returns all the rows, in which the value array and the passed array of strings have at least one common element.
Following the upper example, if I send ['one', 'four'], it should return the row with value: ['one', 'two', 'three'], since there is an intersection - 'one'.
If I send the array ['four', 'five', 'six'], it will not return this row.
You can use the ?| operator for that. But as you are using json and not the recommended jsonb type, you need to cast your column:
select *
from the_table
where (value::jsonb -> 'value') ?| array['one', 'four']

mysql query works in phpmyadmin but not in node.js

I have a query like this...
SELECT *
FROM `000027`,`000028`
WHERE `000027`.id=(SELECT max(`000027`.id) FROM `000027`)
AND `000028`.id=(SELECT max(`000028`.id) FROM `000028`)
which returns something like this in phpmyadmin...
id time value id time value
However, in react.js it is only returning one of these like this...
id time value
2 questions, Why is it doing this? and, how can I get it to return both instead of one?
my node.js code...
const sqlSelect = "SELECT * FROM `000027`,`000028` WHERE `000027`.id=(SELECT max(`000027`.id) FROM `000027`) AND `000028`.id=(SELECT max(`000028`.id) FROM `000028`)"
dbPlant.query(sqlSelect, (err, result) => {
console.log(result)
res.send(result)
res.end()
})
and it sends this back with only one rowdatapacket when it should be two, or two of each of those values...
[
RowDataPacket {
id: 652,
time: 2021-01-24T17:28:01.000Z,
value: '262'
}
]
Your two tables have some column names in common. This is okay to have repeated column names in a result set in the mysql client, but some programming interfaces map a rows of a result set into a hash array, where the column names are the keys. So if you have duplicate column names, one naturally overwrites the other.
The remedy is to define column aliases for one or the other of each duplicate, so they are mapped into distinct keys in the result set.
You must do this one column at a time. Sorry, you can't use SELECT * anymore (you shouldn't use SELECT * anyway). There is no "automatic alias all columns" option.
SELECT
`000027`.id AS id27,
`000027`.time AS time27,
`000027`.value AS value27,
`000028`.id AS id28,
`000028`.time AS time28,
`000028`.value AS value28
FROM `000027`,`000028`
WHERE `000027`.id=(SELECT max(`000027`.id) FROM `000027`)
AND `000028`.id=(SELECT max(`000028`.id) FROM `000028`)

Use GROUP_CONCAT query in Rails

I need the ids of a table but #pluck is not fast enough because there are too many records.
The thing is that I would like to get them in a string directly from mysql instead of get any Array or ActiveRecord::Relation
[1, 2, 3] => "1,2,3"
There is no group_concat in Rails, so I just asked via sql. Example:
sql = User.select("GROUP_CONCAT(users.id)").to_sql
ActiveRecord::Base.connection.exec_query(sql)
The thing is that I don't know why but it does not return all the ids of the table, but just some of them.
Any idea why is not returning all of them or how can I achieve it in a different way?
Apparently the result is truncated to the maximum length that is given by the group_concat_max_len.
Maybe you could increase that value. Follow this answer to get more information:
https://stackoverflow.com/a/5545904/8195530
You could just call it like this
sql = "select GROUP_CONCAT(id) from users"
data = ActiveRecord::Base.connection.exec_query(sql)
# #<ActiveRecord::Result:0x0000560661a2b7d8 #columns=["ids"], #rows=[["41,40,38,42,39,43,45,44"]], #hash_rows=nil, #column_types={}>
then you can get the data as ids with
ids = data['ids'].split(',').map(&:to_i)
# [41, 40, 38, 42, 39, 43, 45, 44]

MariaDB COLUMN_JSON query returns binary

I've been trying to use dynamic columns with an instance of MariaDB v10.1.12.
First, I send the following query:
INSERT INTO savedDisplays (user, name, body, dataSource, params) VALUES ('Marty', 'Hey', 'Hoy', 'temp', COLUMN_CREATE('type', 'tab', 'col0', 'champions', 'col1', 'averageResults'));
Where params' type was defined as a blob, just like the documentation suggests.
The query is accepted, the table updated. If I COLUMN_CHECK the results, it tells me it's fine.
But when I try to select:
"SELECT COLUMN_JSON(params) AS params FROM savedDisplays;
I get a {type: "Buffer", data: Array} containing binary returned to me, instead of the {"type":"tab", "col0":"champions", "col1":"averageResults"} I expect.
EDIT: I can use COLUMN_GET just fine, but I need every column inside the params field, and I need to check the type property first to know what kind of and how many columns there are in the JSON / params field. I could probably make it work still, but that would require multiple queries, as opposed to only one.
Any ideas?
Try:
SELECT CONVERT(COLUMN_JSON(params) USING utf8) AS params FROM savedDisplays
In MariaDB 10 this works at every table:
SELECT CONVERT(COLUMN_JSON(COLUMN_CREATE('t', text, 'v', value)) USING utf8)
as json FROM test WHERE 1 AND value LIKE '%12345%' LIMIT 10;
output in node.js
[ TextRow { json: '{"t":"test text","v":"0.5339044212345805"}' } ]