Conversion to laravel eloquent query [closed] - php

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();

Related

Query with multiple conditions on one column in the database, laravel [closed]

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 need to get users whose level is from one to another value. Well, i don't know, how to make a correct query. This is my bad try:
$thread = Threads::findOrFail($id);
$users= Users::where(
['rank', '>', $thread->rank_from],
['rank', '<', $thread->rank_to]
)->get();
My error is:
Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from accounts where (0 = rank and 1 = > and 2 = 21))
Thank you!
Change it to this format for parenthesis. But here since it's both an AND conditions, you dont really need the parenthesis, unless you're not showing us the whole request and you have some orWhere in it...
$users = Users::where(function ($query) use ($thread) {
$query->where('level', '>', $thread->level_from)
->where('level', '<', $thread->level_to);
})->get();
Here it is in the documentation Eloquent queries
If the query you posted is the whole query, just stack the wheres
$users= Users::where('rank', '>', $thread->rank_from)
->where('rank', '<', $thread->rank_to)
->get();

How to write a query in yii2 for with rollup [closed]

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.

Query db with in exlusion [closed]

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

mysql insert unique values from one column to a column of another table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
please help me in this matter: I need to copy the unique values from entries_list.nr_comanda_entries to entries_uploaded.ship_id.
The tables have these values in common: entries_list.file_id = entries_uploaded.id
Both tables have many other columns, only the entries_uploaded.ship_id is empty.
Could you please tell me the mysql query to do that?
You could use an UPDATE query and an INNER JOIN:
UPDATE
entries_uploaded INNER JOIN entries_list
ON entries_list.file_id = entries_uploaded.id
SET
entries_uploaded.ship_id = entries_list.nr_comanda_entries
Please see fiddle here.

Left join vs two queries, which is faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Supposing I have two tables to query, which query should I choose?
Left join between them or two separate queries? Which option is faster?
Same question on three tables (so basically double left join vs. three queries), which is faster?
i think only one query is better. But you should try in your SQL Server both queries and see which take a longer time
Join query is intoduced for faster data fetching..It consumes execution time..So no doubt join query is much faster...When you use seperate query then each time it has to go database and fetch result but when join all queries,it needs only single time travel to db...

Categories