I want to get all rows which is the same name and location from Users Table
**id** |name |location |phone_number
1 |John | Europe |0988884434
2 |john | Europe |0933333333
3 |Michael |Europe |0888888888
4 |Smith |Dubai |082388888888
5 |Smith |Dubai | 03939494944
I want to get all rows which is the same name and location like
john |Europe
john |Europe
Smith |Dubai
Smith |Dubai
here is how i tried to do
$duplicates = DB::table('users')
->select('name','location', DB::raw('COUNT(*) as `count`'))
->groupBy('name', 'location')
->having('count', '>', 1)
->get();
but this is just showing only one row which is duplicates like
john |Europe
Smith|Dubai
Any help or advice you have would be greatly appreciated.
Use havingRaw:
$duplicates = DB::table('users')
->select('name','location', DB::raw('COUNT(*) as `count`'))
->groupBy('name', 'location')
->havingRaw('COUNT(*) > 1')
->get();
I also wasn't sure of the syntax, but the Laravel documentation seems to imply that the alias you defined in the select clause is not available in the normal having() function.
To get All Rows rather than a total count of a group of duplicate rows would look like the following;
$duplicates = DB::table('users')
->select('id', 'name', 'location')
->whereIn('id', function ($q){
$q->select('id')
->from('users')
->groupBy('name', 'location')
->havingRaw('COUNT(*) > 1');
})->get();
Related
DB::select("select encoded_datas.* from encoded_datas inner join
(select data_user_firstname, data_user_lastname, data_user_barangay from encoded_datas t
group by data_user_firstname, data_user_lastname, data_user_barangay
having count(*)>1) t1
on encoded_datas.data_user_firstname=t1.data_user_firstname and encoded_datas.data_user_lastname=t1.data_user_lastname and encoded_datas.data_user_barangay=t1.data_user_barangay");
It's a working piece of query code, but it is so slow to load compare to Eloquent/Proper Query Builder.
Can anyone here help me convert this to Laravel's Query Builder format..
try this solution
DB::connection(your_connection)
->table('encoded_datas')
->select('encoded_datas.*')
->join(
DB::raw("SELECT data_user_firstname, data_user_lastname, data_user_barangay FROM encoded_datas t GROUP BY data_user_firstname, data_user_lastname, data_user_barangay HAVING COUNT(*) > 1) t1"),
function($join {
$join->on('encoded_datas.data_user_firstname', '=', 't1.data_user_firstname')
->on('encoded_datas.data_user_lastname', '=', 't1.data_user_lastname')
->on('encoded_datas.data_user_barangay', '=', 't1.data_user_barangay');
})
)
but I don't think it will speed up execution
Sometimes sending multiple requests might be less expensive then sending one nested.
You can try getting the ids of the duplicate rows first
$ids = DB::table('datas')
->select('firstname','lastname', DB::raw('COUNT(*) as `count`'))
->groupBy('firstname', 'lastname')
->havingRaw('COUNT(*) > 1')
->pluck('id');
Then get datas
$datas = Datas::whereIn('id', $ids)->get();
In your example you are executing two queries and a join.
Here we are executing 2 queries only, 1 of which uses primary key index, which should be instant. Technically its should be faster, looks cleaner as well
I've tried Alexandr's code:
$ids = DB::table('datas')
->select('firstname','lastname', DB::raw('COUNT(*) as `count`'))
->groupBy('firstname', 'lastname')
->havingRaw('COUNT(*) > 1')
->pluck('id');
$datas = Datas::whereIn('id', $ids)->get();
But it only shows single data/record.. What i want to get is something like this:
**id** |firstname |lastname
1 | John | Legend
2 | john | Legend
3 | Michael | Mooray
4 | Smith | West
5 | smith | West
so Im expecting to get the following result:
john |Legend
john |Legend
Smith |West
Smith |West
I have the following piece of code
$not_paid = Tenant::where('property_id', $property_id)
->whereNotExists(function ($query) use ($property_id) {
$query->select('user_id')
->from('rent_paids');
})
->get();
which is supposed to get all the tenants in a certain property, look them up in the rent_paids table and return the users who are not in the rent_paids table, as follows:
tenants table
Id
user_id
property_id
1
1
1
2
2
1
3
3
1
rent_paids
Id
user_id
property_id
amount_paid
1
1
1
3000
I want to be able to return the users in the tenants table and not in the rent_paids table. In this case, users 2 and 3. But the above code returns an empty array.
You're missing the where clause to tie it back to the original table.
$not_paid = Tenant::where('property_id', $property_id)
->whereNotExists(function ($query) use ($property_id) {
$query->select('user_id')
->from('rent_paids')
->whereColumn('tenants.user_id', '=', 'rent_paids.user_id');
})
->get();
I have two tables 'users' and 'channel'
Table: users
id name channel
1 user1 1,2,3
2 user2 2,3
3 user3 2
Table: channel
id channel_name
1 IT
2 CS
3 EC
I need result as
name channel_name
user1 IT,CS,EC
user2 CS,EC
user3 CS
Using laravel query builder how I write the query?
I tried below, but I got channel_name as NULL.
try 1
$UserChannelList = Users::select('users.name as username', DB::raw("(GROUP_CONCAT(channels.channel_name SEPARATOR ',')) as 'channel_name'"))
->leftjoin('channels', function ($join) {
$join->whereRaw("FIND_IN_SET('channels.id', 'users.channel')");
})
->groupBy('users.name')
->orderBy('users.name', 'ASC')
->get();
try 2
$UserChannelList = Users::select('users.name as username', DB::raw("(GROUP_CONCAT(channel.channel_name SEPARATOR ',')) as 'channel_name'"))
->leftjoin('channel', function ($join) {
$join->on(DB::raw("CONCAT(',', 'users.channel', ',')"), 'like', DB::raw("CONCAT(',','channel.id',',')"));
})
->groupBy('users.name')
->orderBy('users.name', 'ASC')
->get();
Try with this query.
\DB::table("users")
->select("users.*",\DB::raw("GROUP_CONCAT(channels.channel_name) as channel_name"))
->leftjoin("channels",\DB::raw("FIND_IN_SET(channels.id,users.channel)"),">",\DB::raw("'0'"))
->get();
Please check my answer
Here is your solution please check
$UserChannelList = DB::table('users')
->select('users.name', DB::raw("(GROUP_CONCAT(channels.channel_name)) as 'channel_name'"))
->rightJoin('channels', function($join){
$join->whereRaw('FIND_IN_SET(channels.id, users.channel)');
})
->groupBy('users.name')
->orderBy('users.name', 'ASC')
->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 have this query which basically lists topics that two users have in common.
$subcommon= SubjectUser::selectRaw('topic_id, count(topic_id) AS aggregate')
->whereIn('user_id', [4, 2])->groupBy('topic_id')
->having('aggregate','>',1)->get();
For example the query result for the table below would be
{"topic_id":3,"aggregate":1}
tableone
id|user_id|topic_id
1|2 |3
2|4 |3
3|5 |1
I have another table(tabletwo) that also has topic_id which I would like to join so that I get the query result of row 2 from the second table. How would I go about doing this?
tabletwo
id|group_id|topic_id
1|6 |2
2|7 |3
3|7 |1
DB::table('tableone')->join('tabletwo', function($join) {
$join->on(tableone.topic_id, '=', tabletwo.topic_id);
})->get();
try this,
$subcommon= DB::table('tableone')
->selectRaw('topic_id, count(topic_id) AS aggregate')
->join('tabletwo','tabletwo.topic_id', '=', 'tableone.topic_id')
->whereIn('user_id', [4, 2])
->groupBy('tableone.topic_id')
->having('aggregate','>',1)
->get();