此MySQL查询具有联合和规范化功能,我不知道如何编写laravel查询。
SELECT DISTINCT val
FROM
(
SELECT 'date' date_type, date val FROM user_presentation
UNION
SELECT 'accept_date', accept_date FROM user_presentation
UNION
SELECT 'question_date', question_date FROM user_presentation
UNION
SELECT 'success_date', success_date FROM user_presentation
) normalized;
答案 0 :(得分:2)
您可以在Laravel代码中链接一系列联合:
$first = DB::table('user_presentation')
->select('date');
$second = DB::table('user_presentation')
->select('accept_date');
$third = DB::table('user_presentation')
->select('question_date');
$fourth = DB::table('user_presentation')
->select('success_date AS val')
->union($first)
->union($second)
->union($third)
->get();
请注意,上面的代码实际上与此查询相对应:
SELECT date AS val FROM user_presentation UNION
SELECT accept_date FROM user_presentation UNION
SELECT question_date FROM user_presentation UNION
SELECT success_date FROM user_presentation;
此外,您不必使用DISTINCT
子查询,因为联合查询本身应删除所有重复的日期值。
答案 1 :(得分:0)
$userP = UserPresentation::get()
->only('date', 'accept_date', 'question_date', 'success_date')
->unique();
使用Collection的100%Laravel方式,这将一起选择所有四个日期,Laravel Collection将从这些选择中找到唯一的行。