I am completely new to Laravel Framework. And I am using 4.2 version.
I am trying to run below sql statements through function calling from routes.php.
The page shows no error.
But there is no 'truncate' or 'insert' happend in Database.
Here is the code in Controller funtion:
DB::statement('TRUNCATE TABLE calc2');
DB::statement(
"Insert into calc2
Select groups, members, date(timestamp) as Date, count(id) as Total, sum(order) as Order
from Order
where order > 0 and date(timestamp) >= '$lastday'
group by groups, members, date(timestamp)
");
I checked DB connections. I also run these query directly in MySQL. It is working fine.
I tried DB::select instead of DB::statement and I tried with Eloquent/raw too.
But still Database having 0 records only.
Thanks in advance if anyone can suggest me where I am doing mistake.
Are you commiting your queries?
If not, just use DB::commit();
Doc: http://laravel.com/docs/4.2/database#database-transactions
Related
I am using laravel 8. I want to insert multiple rows by selecting from one table to another. I am using DB::statement() but now want to do it using eloquent. Here is my sample code:
DB::statement("insert into bank_information (user_id, bank_id, account_name,account_number)
select applicant_id,bank_id,account_name,account_number from applicant_bank_information
where erec_applicant_bank_information.applicant_id =".$applicant_id);
How can I do it using single eloquent command ? Also, Is there any other smarter/faster way for it in laravel ??
I do not know of a Eloquent way for this, but after Laravel 5.7.17 we have a new method in the query builder insertUsing(). Unfortunately this is not mentioned in the official documentation.
The query builder code will be something like this:
$select = DB::table('applicant_bank_information as abi')
->select('applicant_id','bank_id','account_name','account_number')
->where('abi.applicant_id', $applicant_id);
$rows_inserted = DB::table('bank_information')
->insertUsing(['user_id','bank_id', 'account_name','account_number'], $select);
Tested and working. Keep in mind that you do not run the select request with ->get().
I am working on one module in yii2 and please count me as a noob in this. I usually work in Laravel.
My query looks as below.
$query = SomeModel::find()
->joinWith(['relation1 s', 'relation2 r','relation3','relation4 c'])
->where(some where contions)
->where(some where contions);
The issue is in the view the yii2 debugger (Database Queries) shows "187 duplicated queries found."
My model doesn't have any loop around the query.
I have tried with removing joinWith relations but it will increase the duplicate queries to 220.
The query which gets repeated is one SELECT query
SELECT * FROM `relation4` WHERE `id`= 2159
I have checked the "relation4" model but it has nothing suspicious in it.
What should I do or where should I look to resolve this issue??
I need to know how to do i do a subquery type selection with phalcon models?
for example i want to select all the users who viewed me, they are stored in the UserView table with columns 'id','user_from','user_to' (mapped by User table user_id to either user_from or user_to)
so i want to select all the users who has a user_to as with the current user, and group by user_to make sure i only get one recorded, I wrote below function to do this but there is fundamental two problems
1. Is how to do sub-query using phalcon models
2. Is my logic correctly applied on the back-end of the DB (as i cant see real executed query)
public function getUserWithViewedMe($limit=1000000){
return User::query()
->rightJoin("XYZ\Models\UsersView")
->andWhere(" XYZ\Models\UsersView.user_from IN :user_id: ",
array('user_id' => $this->user->user_id) )
->group('user_id')
->order("XYZ\Models\UsersView.id DESC ")
->limit($limit)
->execute();
}
This returns empty set...
So far it is not possible to model subqueries in Phalcon. There is also topic according to standard implementation issues.
To query params according to other table, here is an answer.
To query IN you can use queryBuilder
$this->modelsManager->createBuilder()
// ...
->inWhere('column', $array);
SELECT * FROM `properties` order by (`queue_type` = 'update') desc;
how can we run this query using cakephp using pagination, I need to sort my properties according to the queue type. option in queue type will be update, folio , verification , new and all.
My query is running perfectly in mysql but I need to do it in cakephp but not need to use query function of cakephp.
Please help me quickly.
I am using a custom query as the Active Record equivelant did not work for me.
When placing the Query in my Database Software SQLYOG it works fine however in CodeIgniter it says
A Database Error Occurred
Error Number: 1096
No tables used
SELECT *
Filename: C:\xampp\htdocs\midas\system\database\DB_driver.php
Line Number: 330
Here is my Query:
SELECT intervention.department_id, department_name, COUNT(*)
FROM intervention
LEFT JOIN department ON department.department_id = intervention.department_id
GROUP BY intervention.department_id, department.department_name
ORDER BY COUNT(*) desc
LIMIT 1
It is a bit of a strange problem.
Here is my Schema also:
http://i.imgur.com/mKNtc.png
Its ok I figured it out.
For custom query in Codeigniter you cannot use get method after.
EDIT
This will not work. As noted below only COUNT(*) or COUNT(table.field) work.
I think you need to specify which table you are using COUNT(*) on, so change it to something like COUNT(department.*) or COUNT(intervention.*)
If you use custom queries in Code Igniter you must return the result (Database object) to controller, because get method (from $this->db) doesn´t work.