1

I need to be able to bring back records from an SQL stored procedure and display them. but I do not want to show duplicates.

I have stored a procedure that brings back all records where ID = the selected ID. It then displays something like this

id value
12 34
12 34
12 33
12 33

I need to bring back only one occurence where ID = 12 and value = 33, and one occurence where ID = 12 and value = 34

I'm working in a PHP laravel framework, I know that

DB::selectOne will bring back a single column, and that DB::select will bring back all records. Is there a laravel/PHP function that will bring back records and will only show one of each value? like so:

id value
12 34
12 33
Karma
  • 1
  • 1
  • 3
  • 9
Nikk96
  • 83
  • 1
  • 8
  • Does this answer your question? [How to get distinct values for non-key column fields in Laravel?](https://stackoverflow.com/questions/25228823/how-to-get-distinct-values-for-non-key-column-fields-in-laravel) – Nico Haase Jan 25 '21 at 12:47

4 Answers4

2

Yes. To display a single occurrence, you must use "distinct"

$result = DB::table('YourTableName')
->select('id', '//other columns')
->distinct()
->get();
Atika
  • 1,025
  • 2
  • 6
  • 17
0

You can do it through MYSQL:

SELECT id, col2, col3, col4
FROM yourtable
GROUP BY col2, col3;

or something like that though Eloquent:

$data = Model::select('id', 'value')->groupBy(['id', 'value'])->get;
V-K
  • 1,297
  • 10
  • 28
0

Use distinct as shown in documentation. Distinct will remove the duplicate records.

$data = DB::table('{{table_name}}')->select('id', 'desired_column')->distinct()->get();
Gazmend Sahiti
  • 443
  • 3
  • 13
0

You can use groupBy() which will remove your duplicate record and read it as a single record. You have to use Select() before groupBy(). I.e. select('require name') then groupBy('require name').

TABLENAME::Select('require name')->groupBy('require name')->get();
buddemat
  • 4,552
  • 14
  • 29
  • 49
bay_beast
  • 11
  • 5