Rails where clause match integer with the array - mysql

I have an issue in finding the correct records from the database. This is my query:
#interested_individual = Profile.where('category_id = ?', #event.interests).all
Where category_id is the column in the profiles table what holds the integer value and #event.interests returns an array that is ["1", "2", "3"]
What I want here is to fetch all the profiles where its category_id is present in the #event.interests array.
Please let me know the solution. Thanks in advance

You don't really need to manually write the binding with ? and all.
Try this:
#interested_individual = Profile.where(category_id: #event.interests).all
Active Record will then turn that into category_id IN (?) for you if you pass in an array.

This query will translate to category_id IN [interest_ids...]
Profile.where(category_id: #event.interests).all

You were close. Use IN.
#interested_individual = Profile.where('category_id IN (?)', #event.interests).all
But, I would suggest find you here, if you are only wanted to get collection of all Profile records :
#interested_individual = Profile.find(#event.interests)

Related

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`)

Mysql for NodeJS not expanding array in queries

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]]

How to control field order in Couchbase N1QL response?

How can I organize the json or table that the query show me? This in chouchbase with n1ql.
example:
select rol, count(*) as cantidad from PPS where type='Usuario'
group by rol
result
[
{
"cantidad": 2,
"rol": "8847cda1-cf52-4af0-880c-5f7c5a281348"
},
{
"cantidad": 2,
"rol": "ef35059f-5953-4da7-b5d5-ee0f9a1c893f"
}
]
I need rol first
I'm sorry, but what you're asking for isn't possible. Within each object the fields are returned in sorted order by name. You could rename the fields to something like "1_rol" and "2_cantidad", but that's the best that N1QL can do.
You might also alias the attributes in the select so that they auto order the way you want:
“select rol as 1, candidad as 2...”
Or, order them into an array:
“select [rol, candidad] as _res...”

Couchbase N1qlQuery: use key value from select

I have this queries:
select otherDocKey from bucket use keys '1234'
update bucket use keys 'hear I need the result of the first query' set ...
I want to do something like that:
update bucket use keys (select otherDocKey from bucket use keys '1234') set kuku = 3
but the response I get is:
[
{
"code": 5030,
"msg": "Missing or invalid primary key map[otherDocKey:\"56443\"] of type map[string]interface {}."
}
]
is there a way to do that in one query?
I am using couchbase version 4.5
The problem with your query is that the nested subquery returns a json result. I.e., the query:
select otherDocKey from bucket use keys '1234'
will return a result that looks like:
{"otherDocKey":"This_is_the_key_for_the_other_doc"}
But you don't want json, you just want the value from the json. For that you need to use 'select raw'. E.g.,
select raw otherDocKey from bucket use keys '1234'
That should give you a result that looks like:
["This_is_the_key_for_the_other_doc"]
When the subquery returns that kind of result, the "use keys" should work properly.

How can I groupBy and change content of the value in couchbase?

Exple :
I have a result like this
Result1 AFTER EMIT:
Key-------------------------------value
"2014/10/31" ----------------- {"A":a}
"2014/10/31" ----------------- {"B":b}
"2014/10/31"----------------- {"C":c}
"2014/10/31"----------------- {"D":d}
How can I output this new result from the previous one:
Key-------------------------value
"2014/10/31" -----------------{"Array":["A":a, "B":b, "C":c, "D":d]}
Is there a way? Any help will be appreciated. Thanks
You can use Couchbase v3 new N1QL query language for that. Also there is Query Language Tutorial. Specifically Array Comprehensions syntax.
I found it . Just do : group = true in the path or select it from the filter result .
and a reduce function to return all the values from the map. also you can add the start key = "2014/10/31" and end key= "2014/10/31".