如何在Laravel 4中获得独特和加入工作

时间:2015-01-15 16:18:27

标签: mysql sql laravel laravel-4

我似乎无法使用sql内置的查询来使用Laravel 4。

它应该输出100行,但我不断得到850并且无法弄清楚如何使DISTINCT在Laravel 4中工作

//I am trying to achieve a result set of 100, which is easily done using sql: Result 100 rows
SELECT DISTINCT a.name, a.brand FROM table1 a
INNER JOIN table2 b ON a.name=b.name

我尝试了以下内容,但似乎没有一个应用DISTINCT

//But when trying to use eloquent or query builder, the distinct does not get applied: Results 850 rows
DB::table('table1')
->join('table2', 'table1.name', '=', 'table2.name')
->select(DB::raw('DISTINCT `table1`.`name`'), 'table1.brand')
->count();

//results 850 rows
table1::distinct()
->join('table2', 'table1.name', '=', 'table2.name')
->select(array('table1.name','table1.brand'))
->orderBy('table1.name')
->count();

0 个答案:

没有答案