I have products table with name and no_of_sell column..
I wanted to get highest 5 sold products based on no_of_sell column from products table.
how do i use it with query builder?
$propular_products = DB::table('users')
->join('products','products.auth_id','users.id')
->select('products.*')
->orderBy('products.no_of_sell', 'desc')
->where('products.status','=','1')
->paginate(5);
suppose products table:
name no_of_sell
x 6
y 9
z 10
t 23
u 3
h 11
r 5
i wanted to find
products list of 5 max no_of_sell ie, x y z t h
So if I understand it correctly in the column no_of_sell is a int. I should write it as follow :
$best_sell = DB::table('products')
->orderBy('no_of_sell', 'desc')
->limit(5)
->where('products.status','=','1')
->paginate(4)
->get();
https://laravel.com/docs/5.4/queries#retrieving-results
https://laravel.com/docs/5.4/queries#ordering-grouping-limit-and-offset
$best_sell = DB::table('products')
->select('no_of_sell')
->where('products.status','=','1')
->orderBy('no_of_sell', 'desc')
->paginate(4);
$best_sell = DB::table('products')
->select() // your selection criteria
->where('products.status','=','1')
->take(5)->get();
Related
I have made a query that counts how many products are bought together, but I don't want to have the products both ways in it
Code
DB::table(DB::raw('tenant_db.sales_data as b'))
->selectRaw('a.product_id as product_id, b.product_id as bought_with, count(*) as times_bought_together')
->join(DB::raw('tenant_db.sales_data as a'), function ($join){
$join->on('a.sale_id', '=', 'b.sale_id');
$join->on('a.product_id', '!=', 'b.product_id');
})
->groupBy('a.product_id', 'b.product_id');
Output
Product_id Bought_with times_bought_together'
52 24 3
24 52 3
So I only want to have it one time not twice, is that possible (based on quantity)?
Use < rather than !=:
$join->on('a.product_id', '<', 'b.product_id');
working with Laravel 5.6 and mysql. I have following table name as vehicles
id name number type
1 def 123 Car
2 lki 589 Van
3 loi 256 Truck
4 oiu 569 Car
5 hyy 589 Van
Now I need count number of each type columns values I do not wanna loop count. just I need count each Type and show on blade file. like this,
Car 2
Van 2
Truck 1
etc.. how can do this?
You need to simply Group By on the type column, and use Count(*) to count the number of rows:
SELECT type, COUNT(*) AS count
FROM vehicles
GROUP BY type
This should work
$vehiclesInfo = DB::table('vehicles')
->select('type', DB::raw('count(*) as total'))
->groupBy('type')
->get();
For more info on group by and count you can follow this tutorial.
Larvel queries
Just use DB facade to get count with groupby
$vehicles= DB::table('vehicles')
->select('type', DB::raw('count(*) as total'))
->groupBy('type')
->get();
So i am confused on laravel nested queries.
I am trying to prepared a search function that filters the products that is in the sales_agreement table but also if the record has financing i want it to be filtered too.
Below is my sample.
(1) Table Products
id product_name price
1 car 500k
2 jeep 200k
3 motor 100k
4 bicycle 5k
(2) Sales Agreement
id product_id price financing
1 2 500k BPI
2 1 200k MetroBank
3 3 100k
Expected Output In my query
product_id product_name
2 jeep
1 car
4 bicycle
My Query
$result = DB::table('products')
->whereNotIn('id',function($q){
$q->select('product_id')->from('sales_agreement');
})->where('product_name','LIKE','%' . $get['q'] . '%')
->select('id','product_name')
->get();
This filters all of the products that is not in the sales_agreement table. But i also want to consider the items that has a Financing record in the sales_agreement table. In that point i dont have an idea how to do it.
Any comments/suggestion/answer will be much appreciated. Thanks !
You can try where null
$result = DB::table('products')
->whereNotIn('id',function($q){
$q->select('product_id')->from('sales_agreement')
->whereNull('financing');
})->where('product_name','LIKE','%' . $get['q'] . '%')
->select('id','product_name')
->get();
If you want to exclude the products that don't have financing record you can do a left join with additional clause
$result = DB::table('products as p')
->leftJoin('sales_agreement as s', function ($query) {
$query->on('p.id', '=', 's.product_id')
->whereNull('s.financing');
})
->whereNull('s.product_id')
->where('p.product_name', 'LIKE', '%' . $get['q'] . '%')
->select('p.id', 'p.product_name')
->get();
I have a table that contains:
id seller_id amount created_at
1 10 100 2017-06-01 00:00:00
2 15 250 2017-06-01 00:00:00
....
154 10 10000 2017-12-24 00:00:00
255 15 25000 2017-12-24 00:00:00
I want to get all the latest rows for each individual seller_id. I can get the latest row for one like this:
$sales = Snapshot::where('seller_id', '=', 15)
->orderBy('created_at', 'DESC')
->first();
How do I get only the latest row for each seller?
To get latest record for each seller_id you can use following query
select s.*
from snapshot s
left join snapshot s1 on s.seller_id = s1.seller_id
and s.created_at < s1.created_at
where s1.seller_id is null
Using query builder you might rewrite it as
DB::table('snapshot as s')
->select('s.*')
->leftJoin('snapshot as s1', function ($join) {
$join->on('s.seller_id', '=', 's1.seller_id')
->whereRaw(DB::raw('s.created_at < s1.created_at'));
})
->whereNull('s1.seller_id')
->get();
This worked:
DB::table('snapshot as s')
->select('s.*')
->leftJoin('snapshot as s1', function ($join) {
$join->on('s.seller_id', '=', 's1.seller_id');
$join->on('s.created_at', '<', 's1.created_at');
})
->whereNull('s1.seller_id')
->get();
I want to write laravel eloquent query which could select all the titles from titles table where title_id doesnt't exist in title_count table. Here is an example. titles table:
title_id
1
2
3
4
title_count table:
title_id
3
4
So my query should select titles with id's 1, 2 from title table. To be honest, I have no ideo how to do it. I'm using laravel 5. I hope you can help me. Thanks in advance!
Use a join to to identify titles that do not appear in title_count.
DB::table('titles')->leftJoin('title_count', 'titles.title_id', '=', 'title_count.title_id')
->select('titles.*')
->whereNull('title_count.title_id')
->get();
Try this
DB::table('titles')->whereNotExists(function($query)
{
$query->select(DB::raw(1))
->from('title_count')
->whereRaw('title_count.title_id = titles.title_id');
})->get();
Not tested
DB::table('title_count')
->leftJoin('titles as t', 't.title_id', '=', 'title_count.title_id')
->select('t.*')
->where('t.title_id', '!=', 'title_count.title_id')
->get();