Query with multiple conditions on one column in the database, laravel [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 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();

Related

Conversion to laravel eloquent query [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 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();

SQL UPDATE table with more than 1 word fail [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 3 years ago.
Improve this question
my table "list_article_groups have several groups which is the same, but have different spellings and small differences that cause duplicates in system. So I want to join (seach and replace) all the duplicates.
I am running this expression;
UPDATE `list_articles_groups`
SET `name` = replace(name, '%front%cable%', 'Front cable')
But I get 0 result. If I replace % with space, i get 1000 results. Any clues?
Run this, ltrim and ltrim work with most sql plateforms. What is yours and we can adjust the code below
for sql server
UPDATE list_articles_groups
SET name = 'Front cable' where name like '%' + 'front' + '%'+ 'cable' + '%'
for postgres
UPDATE list_articles_groups
SET name = 'Front cable' where name like '%' || 'front' || '%'|| 'cable' || '%'.
You get the idea.
I found the solution. Using "where".
UPDATE list_articles_groups
SET name = 'Front Cable' WHERE name LIKE '%front%cable%'

Error: SELECT bookedseat from bookings where sdate=2018-04-26 and stime=5PM Unknown column '5PM' in 'where clause' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is the html form code
And this is the php for the above form.After executing this,I get an error saying " Error: SELECT bookedseat from bookings where sdate=2018-04-26 and stime=5PM
Unknown column '5PM' in 'where clause' "
Why is it?
You need to wrap your variables in single quotes.
$query = "SELECT bookedseat from bookings where sdate='$sdate' and stime='$stime'"
PD: As everybody says, this is not the best approach. You need to make a prepared statement instead to avoid sql injection and other type of errors like this

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.

How to get the numbers of a column in phpmyadmin? [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
I have the database as below picture shown:
I want to get the numbers under "ID" colm with "0" value in "is_parent".
How to do that with a sql query?
I want the numbers going to be link this:
1792,2304,1793,1794,2308.. etc..
Tried my best, with failed attempt, hopefully someone can help me with the correct query.
Thank you,
regards.
Try this, it is simple
SELECT id FROM `table_name` WHERE is_parent= 0
well, if
SELECT id FROM `table_name` WHERE is_parent= 0
fails, you may try:
SELECT id FROM `table_name` WHERE is_parent IN (1792,2304,1793,1794,2308....)
or:
SELECT id FROM `table_name` WHERE is_parent != 0

Categories