I want to run
SELECT count(DISTINCT `ASIN`) as results FROM crawl_results WHERE user_id = 1 and website_id = 1
and expect to get 136 results
However
`select('ASIN')->where('user_id', Auth::id())->where('website_id', $scanID)->distinct()->count();`
returns all rows in the table of 814
and
`select('ASIN')->where('user_id', Auth::id())->where('website_id', $scanID)->groupby('ASIN')->count();`
returns 8 results
First create model and use the below code where you require it
use App\CrawlResult;
CrawlResult::where('user_id',1)->where('website_id',1)->distinct('ASIN')->count('ASIN');
You should write query like this:
DB::table('crawl_results')
->select(DB::raw('COUNT(DISTINCT(ASIN)) as results')
->where('user_id', 1)
->where('website_id',1)
->get();
Related
When querying a product code table I have the following
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10);
This works fine on the initial load of 10 records but when a subsequent call is made for page 2 I get the following error
Incorrect syntax near 'offset'. (SQL: select * from [stock_records] order by [stk_physical] desc offset 10 rows fetch next 10 rows only)
I'm using Laravel 8.0 with SQL
You should append query string to the pagination like this
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10);
$results->appends(["order_by" => "stk_physical"]);
This will append the &order_by=stk_physical to each link in the view and you can also use withQueryString() to take in consideration query string in future pagination like this
$results = Stock::orderBy('stk_physical', 'desc')->paginate(10)->withQueryString();
I have a table with the following columns: model_id, starts and end
I'm looking to run a query tath finds all the rows that matches the provided model_id and have a start bigger and end lower that the provided code. Example: if the provided code is 15 and the model_id is 5 I should get all the rows that have model_id = 5 and start >= 15 and end <= 15.
This is th query I'm working with:
DB::table('generations')->where([['model_id', '=', 1],['starts', '>=', '1998'], ['end', '<=', '1998']])->get();
I transformed this to the raw sql but the returned sql query does not get any results too.
I'm using Laravel 5.7
Rows with model_id = 1;
your logic's order is the problem.
DB::table('generations')->where('model_id', '=', 1)->where('start', '<=', 1995)->where('end' , '>=', 1995)->get();
Learn more in the documentation.
I have a filter on a form before displaying a list.
If the filter 'filter_parisetat' = 2, i set the where clause like this :
$query = $query->where('gri_nb_matchs','gri_nb_matchs_pec');
I want to select all the rows of my Grille table where 'gri_nb_matchs' equals 'gri_nb_matchs_pec'
but it doesn't work..nothing is selected (and of course in my table I have 10 in each columns for these 2 fields
The other Where conditions in case 1 is equal to
$query = $query->where('gri_nb_matchs','>','gri_nb_matchs_pec');
I should retrieve 0 rows, but here I retrieve all the rows..
it works upside down...
In My table, the 2 fields are described like this :
11 gri_nb_matchs tinyint(3) UNSIGNED
12 gri_nb_matchs_pec tinyint(3) UNSIGNED
In MySql, whenI write the query, results are correct..
What went wrong ?
Thanks a lot
Thierry
In this statement:
$query = $query->where('gri_nb_matchs','>','gri_nb_matchs_pec');
It seems you are comparing the field gri_nb_matchs to the string value 'gri_nb_matchs_pec', not the column named gri_nb_matchs_pec. In order to achieve this you have to use a raw query as so:
$query = $query->where('gri_nb_matchs','>',DB::raw('gri_nb_matchs_pec'));
or
$query = $query->whereRaw('gri_nb_matchs > gri_nb_matchs_pec');
My Laravel query is not working properly. but MySQL query works fine
Laravel Query :
$data = DB::table(DB::raw('select (sum(case when type="credit" then amount else -amount end)) - (select sum(amount) from total) from report'))
Please refer mysql query in sqlfiidle : http://sqlfiddle.com/#!9/2d0343/9
Rather than try rewrite your MySQL query, let's simplify it and make it more eloquent.
Assuming $data is the overall balance and your model for the report table is called Report then the below should achieve what you are after:
$data = Report::where('type', 'credit')->sum('amount') - Report::where('type', 'debit')->sum('amount');
This will give you the sum of all your credits, minus the sum of all your debits.
I want to write a query which selects rows where 2 attributes from 1 entity have equal value.
This would be an example of doing this in raw SQL:
Select * from users u where u.username = u.lastname
Does laravel have any methods that take 2 column names as parameters and return the matching results?
What you need is a DB::raw expression:
DB::table('users')
->where('username', '=', DB::raw('lastname'))
->get();
The only thing DB::raw actually does is to tell the query interpreter not to treat 'lastname' like any other string value but just interpolate it in the SQL as it is.
http://laravel.com/docs/queries#raw-expressions
In Laravel 5, there's now whereColumn for this, for cleaner code:
Users::whereColumn('username', 'lastname')->get();