Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
how to write a find() with rollup :
$leadsCount = Approval::find()
->select(['COUNT(id) AS cnt, coalesce(status, "total")'])
->groupBy(['status'])
->with(rollup)
->all();
While running the query am
getting an error like this : Use of undefined constant rollup - assumed 'rollup'
I guess you mean this:
$leadsCount = Approval::find()
->select(['COUNT(id) AS cnt, coalesce(`status`, "total")'])
->groupBy(new \yii\db\Expression('`status` ASC WITH ROLLUP'))
->all();
The method with() is about object relations what is something completly different.
You also need to quote the column status since it is a reserved word in MySQL.
btw: you may want to use asArray() as well (->asArray()->all();) since you don't get Approval objects with this query.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am creating a query and executing it in my project. I want to set null column value to 0 in select statement.
We need to set null to blank or zero. See below screenshot
I think you can use model casting, using "if" statement https://laravel.com/docs/8.x/eloquent-mutators#custom-casts
UPDATE table_nameSETcolumn_name = 0 WHERE column_name is NULL
Edit: You can add as many columns as needed by adding a comma ,
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i am new to laravel , i wrote a query in sql , but i dont know how to convert it into laravel eloquent, can you please help me in that
SELECT * from clinics
INNER join locations ON locations.clinicID = clinics.clinicID
INNER JOIN location_services ON location_services.locationID = locations.locationID
inner JOIN services ON services.serviceID = location_services.serviceID
I have already reffered larvel documentation and provided necessary relatonships
There are many ways to run eloquently in the laravel.
A relationship is the best way but here you have not defined relation so try below:
DB::table('clinics')
->join('locations', 'locations.clinicID', '=', 'clinics.clinicID')
->join('location_services ', 'location_services.locationID', '=', 'locations.locationID')
->join('services', 'services.serviceID', '=', 'location_services.serviceID')
->select('locations.*', 'location_services.*', 'services.*')
->get();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Trying to query db. I need to show all fields, but need to exclude one name that is in the db.
Example:
The db contains column 'marketer' when I try to query it I don't want marketer 'Tommy' but all the others. I have tried tried where clause with all the names and not working.
This is the query you're looking for
SELECT * FROM <table_name> WHERE marketer<>'Tommy';
use the 'where' to add your conditions
SELECT * FROM your_table WHERE marketer!='Tommy'
For your Reference
http://www.w3schools.com/sql/sql_where.asp
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this code:
$date=mysql_query("SELECT * FROM players where AdminLevel>='1' ORDER BY AdminLevel,UltReg DESC LIMIT 0,100");
I wanna sort something, and i don't know how to use for more "orders"
You can add additional levels to the ORDER BY statement by adding them separated by commas, as you have done for two levels.
For example:
$date=mysql_query("SELECT * FROM players where AdminLevel>='1' ORDER BY AdminLevel,UltReg DESC, anotherField ASC, yetAnotherField DESC, oneMoreFieldHere LIMIT 0,100");
Additionally, you should be using mysqli_query() instead of mysql_query() as mysql_query() has been deprecated.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a query that is returning a different amount of user_id's each time it's run (based on the number of subscribers).
What I need to do is insert each of these user_id results into separate rows within a table along with a simple message of "new alert" in a separate column.
How could I possibly go about doing this? Would a for each loop work in this situation?
Try this:
INSERT INTO alert_table SELECT user_id, 'new alert' FROM ... WHERE ...
Use your own query, just prepend it with the INSERT INTO alert_table clause.