Retrieving documents on keys using nested query - couchbase

Started learning NIQL on couchbase. I am trying to retrieve multiple documents based on the keys using following nested query. Can't get it working. Is that even possible ?
SELECT * FROM Cart USE KEYS (
SELECT META().id FROM Cart WHERE META().id LIKE "100%"
)

USE KEYS expecting array of strings. Subquery generates array of Object of strings.
Use RAW in subquery to remove object when you projecting single field.
You have one of the following options
SELECT META().id, * FROM Cart WHERE META().id LIKE "100%";
OR
SELECT * FROM Cart USE KEYS (SELECT RAW META().id FROM Cart WHERE META().id LIKE "100%");
You can checkout N1QL tutorial https://query-tutorial.couchbase.com/tutorial/#1

Related

MySQL using JSON_ARRAYAGG in a SELECT IN clause?

Our database solution is very JSON heavy, and as such, our SQL queries are all JSON based (for the most part). This includes extensive use of JSON_ARRAYAGG().
The problem I'm encountering is using a returned array of indexes in WHERE IN, which simply doesn't work. From what I can tell it's a simple formatting issue where MySQL wants an () encapsulation and a JSON array is a [] encapsulation.
For example:
SELECT COUNT(si.ID) AS item_count, JSON_ARRAYAGG(si.ID) AS item_array
FROM sourcing_item si;
Returns:
7, [1,2,3,4,5,6,7]
What I need to do is write a complex nested query that allows for selecting record IDs that are IN the JSON_ARRAYAGG result. Like:
SELECT si.item_name
FROM sourcing_item si
WHERE si.ID IN item_array
Of course the above doesn't work because MySQL doesn't recognize [] vs. ().
Is there a viable workaround for this issue? I'm surprised they haven't updated MySQL to allow the WHERE IN clause to work with a JSON array...
The MEMBER OF operator does this.
SELECT si.item_name
FROM sourcing_item si
WHERE si.ID MEMBER OF (item_array)

get distinct "title" in mysql in django

I have used django to develop a web app.
I want to get the distinct "title" form the queryset get by filter.
But I use mysql so could not pass "title" to distict.
How could I filter the queryset with the distinct "title"?
query_set = CourseInfo.objects.filter(discipline_id=id).distinct('title')
return render(request, 'main.html',
context={'query_set':query_set})
I get error for this in mysql as it may only used in postgresql
`
It will give you distinct titles:
titles = CourseInfo.objects.filter(
discipline_id=id
).order_by('title').values('title').distinct()
Note:
there is no such thing called SELECT DISTINCT ON in MySQL.
You can only use it in Postgresql but maybe GROUP BY helps you for converting SELECT DISTINCT ON query to MySQL query.
Check out this link then you kinda can convert this query to MySQL query.

Retrieve a key-value data structure from MariaDB query

I would like to do a query to retrieve a key-value data structure without putting the burden on the code.
For example, if I have the following query:
SELECT id, nome FROM articolo
is there a way in mariaDB to throw this data into a key-value data structure so you don't do it from code and do it directly from query? Something like:
SELECT {id: nome} FROM articolo
Thanks in advance
You can use the JSON functions.
This creates an object for each row:
SELECT JSON_OBJECT(id, nome) FROM articolo;
If you want all the values in a single object, use:
SELECT JSON_OBJECTAGG(id, nome) FROM articolo

how to include hard-coded value to output from mysql query?

I've created a MySQL sproc which returns 3 separate result sets. I'm implementing the npm mysql package downstream to exec the sproc and get a result structured in json with the 3 result sets. I need the ability to filter the json result sets that are returned based on some type of indicator in each result set. For example, if I wanted to get the result set from the json response which deals specifically with Suppliers then I could use some type of js filter similar to this:
var supplierResultSet = mySqlJsonResults.filter(x => x.ResultType === 'SupplierResults');
I think SQL Server provides the ability to include a hard-coded column value in a SQL result set like this:
select
'SupplierResults',
*
from
supplier
However, this approach appears to be invalid in MySQL b/c MySQL Workbench is telling me that the sproc syntax is invalid and won't let me save the changes. Do you know if something like what I'm trying to achieve is possible in MySQL and if not then can you recommend alternative approaches that would help me achieve my ultimate goal of including some type of fixed indicator in each result set to provide a handle for downstream filtering of the json response?
If I followed you correctly, you just need to prefix * with the table name or alias:
select 'SupplierResults' hardcoded, s.* from supplier s
As far as I know, this is the SQL Standard. select * is valid only when no other expression is added in the selec clause; SQL Server is lax about this, but most other databases follow the standard.
It is also a good idea to assign a name to the column that contains the hardcoded value (I named it hardcoded in the above query).
In MySQL you can simply put the * first:
SELECT *, 'SupplierResults'
FROM supplier
Demo on dbfiddle
To be more specific, in your case, in your query you would need to do this
select
'SupplierResults',
supplier.* -- <-- this
from
supplier
Try this
create table a (f1 int);
insert into a values (1);
select 'xxx', f1, a.* from a;
Basically, if there are other fields in select, prefix '*' with table name or alias

Laravel: eloquent select * vs select column

In my Laravel API everyone and everywhere written select *, even when it does not need to collect all the column data.
Is it better to use eloquent select instead, specifying exactly what's needed?
There are several ways of doing this:
To select only specific fields you can do something like:
ModelName::all('column1', 'column2', 'column3');
Or using get:
ModelName::get(['id', 'date']);
For Models with relationships:
$model->relation()->only(['column1', 'column2']);
If you need to eager load while selecting specific columns with relationship:
$data = Model::with(array('relation' => function($query)
{
$query->select('name');
}))->get();
You can read more at collections