How to change this query to Laravel 5.5
"SELECT ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM data_ap))*100,1)
FROM data_ap WHERE STATUS LIKE '%UP%'";
Something like that :
\DB::table('data_ap')
->select(\DB::raw('ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM data_ap))*100,1) as calc'))
->where('STATUS','like','%UP%')
->get();
I think you must keep native query with Raw Expressions, for more here https://laravel.com/docs/5.6/queries#raw-expressions
#Muhammad Anwari: You can simply point the targeted column/alias
\DB::table('data_ap')
->select(\DB::raw('ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM data_ap))*100,1) as calc'))
->where('STATUS','like','%UP%')
->first()->calc;
Related
I just want to use the following raw DB query with Laravel Eloquent model.
SELECT
<column_name>,
COUNT(<column_name>) AS `value_occurrence`
FROM
<my_table>
GROUP BY
<column_name>
ORDER BY
`value_occurrence` DESC
LIMIT 1;
Let's say I have a model called TestModel. I just would like to do something like the following.
TestModel::select('column_name', 'COUNT(column_name) AS occurrences')
->groupBy('column_name')
->orderBy('occurences')
->limit(10)
->get();
Can you help me please? Thanks...
I just found out that I can use selectRaw to use Count(column_name) in the code however, if there is any better way of doing it, I would like to see it.
Thanks...
The answer:
TestModel::selectRaw('column_name, COUNT(column_name) AS occurrences')
->groupBy('column_name')
->orderBy('occurences')
->limit(10)
->get();
I want to write this raw query using Eloquent. I have tried using ->whereRaw but it shows error due to special characters in it.
SELECT id,shipping_email FROM orders WHERE 1 AND `shipping_email` NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,63}$' AND id = '<>'
Help is appreciated...
Thanks in advance.
I have got answer, I have tried these codes..
\Model\Order::query()
->select('id', 'shipping_email')
->whereRaw("`shipping_email` NOT REGEXP ?", ["^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,63}$"])
->get();
Here my MySQL query (work in phpMyAdmin) :
SELECT workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 AS total FROM `team` GROUP by workcenter ORDER BY total
then, i try in Laravel Sintax like this below (not work) :
$sql = Team::groupBy('workcenter')->select('workcenter', \DB::raw('(SUM(w1+w2+w3+w4)/ (COUNT(DISTINCT(teknisi))*30*4) )*100 AS total'))
->OrderBy('total', 'Desc')
->get();
When i run the laravel sintax, its doesn't show any errors, but the output is nothing..
Please anyone help me to convert the MySQL query to Laravel Sintax. Thank you!
I think you are close enough, however, this doesn't look like a correct way to group by with Eloquent ORM. Try using raw expressions, something like this might work:
$sql = DB::table('team')
->select(DB::raw('workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 as total'))
->orderBy('total', 'desc')
->groupBy('workcenter')
->get();
More about raw expressions here - https://laravel.com/docs/6.x/queries#raw-expressions
Whenever I want to convert SQL query to Laravel I always change one column name, the laravel error report will show your current query and u can compare it to the SQL query
I'm unsure what whereRaw is in PHP Laravel framework. Could you provide good and easily understandable example, please?
WhereRaw() is a function of Laravel query builder which puts your input as it is in the SQL query's where clause.
Think of it as the where() function whose input argument will not be processed before inserting into queries.
See the example below:
$Query = DB::table('some_table')->where('YEAR(date)', 'YEAR(CURRENT_DATE)');
In this Laravel will resolve your arguments to build a query. Which will result in the following query because your input will be treated as some field and its its value :
SELECT * FROM `some_table` WHERE `YEAR(date)` = `YEAR(CURRENT_DATE)`
Which is not desired.
And now if you use whereRaw like:
$Query = DB::table('some_table')->whereRaw('YEAR(date) = YEAR(CURRENT_DATE)');
Now Laravel put this where clause as it is in your query, like below:
SELECT * FROM `some_table` WHERE YEAR(date) = YEAR(CURRENT_DATE)
Hope it helped (:
WhereRaw: Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings.
If you are unable to generate the query you need via the fluent interface, feel free to use whereRaw()
Ex:
$users = User::whereRaw('age > ? and votes = 100', array(25))->get();
which is equals to:
"SELECT * FROM users WHERE age > 25 AND votes = 100";
Reference
In laraval we use query builder. But Sometime you need to execute raw sql query.
So you can inject it to whereRaw as this example :
whereRAW('YEAR(event_datetime) =?', [$year])
I have this query in laravel 5.2
$obj_custom_stdy_data = QstCustomStudyData::where('student_id', $this->data_user['student_id'])
->select($list_id . ' as list_id ', 'chapter_id', 'subject_id', 'subject_code_id')
->get()
->toArray();
Well I have a fixed value $list_id got from top code. Actually I want to add new field during query selection as list_id. However I got error for such that method.
When I tried in mysql IDE for example:
SELECT (1+2) as total, c.* FROM users
Then the result is no wrong at all.
Is that anyway to write in query builder for laravel instead of raw style?
You can take the use of DB::raw() method of QueryBuilder like this:
->select(DB::raw('(1+2) as total'));
See more about Query Builder's Raw Expressions
Hope this helps!