If I have to return only value from a single column of the database table. SELECT DISTINCT 'ad_advertiser' FROM 'adverts' Want to run a query which is going to return a value with No Duplicates. Examples of Laravel DistinctĪ few examples using the Laravel DISTINCT query to ease the understanding:ġ. Also, the GROUP BY query is used in conjunction with other functions like (COUNT, MAX, MIN, SUM, AVG), whereas, the DISTINCT QUERY is used as a standalone function. The SELECT GROUP BY query is used to find values that are the same across multiple rows. The SELECT DISCTINCT query is used to return values which are different from each other. Hence, the important thing to remember here is the difference between GROUPBY and DISTINCT queries. However, for searches where multiple merges do not take place, the DISTINCT query works perfectly fine. The Laravel Query Builder that has to be used for such an event would be: DB::table('order')->select('date', 'seller', DB::raw( 'SUM(total) as total')) Hence, under these circumstances, as mentioned previously we would be using the GROUPBY query.Īnd I would want to have an output similar to this: One of the drawbacks of using the DISTINCT query is that if we are to request for multiple rows containing similar data that has to be merged together for the final output, the resultant will not be what we would expect it to be. A turn around for that would be using the GROUPBY option, which essentially groups all the relevant search results and returns the same. One has to be careful while working with Laravel DISTINCT because it may not always provide you with the exact return. DB::table('tablename')->distinct('name')->count('name') How does Laravel Distinct Work?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |