I have table called teams when I query using Eloquent like this following code:
$team = Team::where('id', 23)->get();
and this code return object like this following JSON:
{
"id": 23,
"name": "Test"
"created_at": "2020-06-03 17:33:14",
"is_used": false
}
but when I tried to direct query on workbench created_at field getting different value
The query look like this following mysql code:
SELECT created_at from teams where id = 23;
-- this query return 2020-06-04 00:33:14
But the valid value is the query with Eloquent.
My question is how to make this default insert timestamp in mysql table equal with Eloquent data?
Thanks in advance
Related
My MySQL table contain json column academics.
Table values look like this :
id academics
--------------------------------------------------------
100 ["CBSE-Afternoon-12-A","CBSE-Morning-12-B"]
200 ["CBSE-Afternoon-12-C","CBSE-Morning-12-D"]
300 ["CBSE-Afternoon-12-E","CBSE-Afternoon-12-F"]
I have to find the id from the above table based on the search key:
CBSE-Morning-12 & CBSE-Afternoon-12
I have tried the below query
SELECT id
FROM ACADEMIC_TABLE
WHERE JSON_SEARCH(academics, 'all', 'CBSE-Morning-12%') IS NOT NULL
it returns id: 100,200 correctly.
But I need to search with two keywords like condition in JSON
[CBSE-Morning-12 & CBSE-Afternoon-12 ] and return id 100,200,300
Please help me
I need to search with two keywords like condition in JSON [CBSE-Morning-12 & CBSE-Afternoon-12 ] and return id 100,200,300
Looking at the sample data - you need the value contained either first or second pattern. If so then you must use OR:
SELECT id
FROM ACADEMIC_TEBLE
WHERE JSON_SEARCH(academics, 'one', 'CBSE-Morning-12%') IS NOT NULL
OR JSON_SEARCH(academics, 'one', 'CBSE-Afternoon-12%') IS NOT NULL;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bdc058111adf7d4200a1471c9873e94c
I have 2 databases(one in mySql and the other one in MongoDB) in my project and I need to perform a query like this
public function getPosts() {
$user = Auth::user();
$follow = DB::connection("mongodb")
->collection("followers")
->where("user_id", $user->id)->get();
/*$res = DB::select("
SELECT *
FROM posts
WHERE user_id = ? OR user_id IN
(SELECT user_id from $follows)
ORDER BY created_at DESC", [$user->id, $user->id]);
*/
return response()->json($res);
}
This is a query which returns the posts from the logged user and from people the user follow
The followers table (the one in MongoDB) contains "user_id" and "follows_id"
The commented line is the original query (every table in one single database on mySql
Thank you
Edit: I solved through a query in mongodb, then I edited the result to get an array which I incorporated into the sql query through orWhereIn Thank you for your answers :)
I don't think so... you may try a true multimodel database such as Oracle XE (it's free) to achieve your goal here...
I have the following json structure on my Postgres. The table is named "customers" and the field that contains the json is named "data"
{
customerId: 1,
something: "..."
list: [{ nestedId: 1, attribute: "a" }, { nestedId: 2, attribute: "b" }]
}
I'm trying to query all customers that have an element inside the field "list" with nestedId = 1.
I accomplished that poorly trough the query:
SELECT data FROM customers a, jsonb_array_elements(data->'list') e WHERE (e->'nestedId')::int = 1
I said poorly because since I'm using jsonb_array_elements on the FROM clausule, it is not used as filter, resulting in a seq scan.
I tried something like:
SELECT data FROM customers where data->'list' #> '{"nestedId": 1, attribute: "a"}'::jsonb
But it does not return anything. I imagine because the "list" field is seen as an array and not as each type of my records.
Any ideas how to perform that query filtering nestedId on the WHERE condition?
Try this query:
SELECT data FROM customers where data->'list' #> '[{"nestedId": 1}]';
This query will work in Postgres 9.4+.
I have a plain select query for which mapreduce view is already present.
Query:
select count(*) from `my-bucket` where type = 'Order' and status = 'CREATED' and timestamp > 1535605294320 and timestamp <= 1535605594320
view:
function (doc, meta) {
if (doc._class == "com.myclass.Order"){
emit([doc.type,doc.status, doc.timestamp], null);
}
}
Keys for querying the view:
Start key : ["Order","CREATED",1535605294320]
End key : ["Order","CREATED",1535605594320]
Requirement: Now, we would want this view to support a query having IN clause on status parameter. Also, we would like to add additional parameters supporting IN parameters. Sample N1Ql would be like below.
select count(*) from `my-bucket` where type = 'Order' and orderType IN ["TYPE-A","TYPE-B","TYPE-C"]and status IN ['CREATED',""READY,"CANCELLED"] and timestamp > MILLIS("2016-05-15T03:59:00Z") and timestamp <= MILLIS("2017-05-15T03:59:00Z")
How to write a query on view to accomplish this? Only solution comes to my mind is to fire multiple (lets says x) queries on views
where x = m1*m2*....*mn
AND m1=number of paremeters in first IN clause
AND n=number of IN clauses.
Is there any better solution like executing this query in batch (using java SDK) or a single mapreduce query?
I have the following database table 'observations'
I am trying to make table by group the observations using three criteria (date - user_id - Type_Name_ID):-
There is no way coming into my mind of how to form an laravel query to get the required result.
Usually you can start from the known SQL query statement to get these results and use the methods provided in Query Builder.
>>> $observationsQuery = DB::table('observations')
->selectRaw('date, count(observation_id), user_id, Type_Name_ID')
->groupBy('date', 'user_id', 'Type_Name_ID');
>>> $observationsQuery->toSql();
=> "select date, count(observation_id), user_id, Type_Name_ID from "observations"
group by "date", "user_id", "Type_Name_ID""