这个MySQL查询有联合和规范化,我不知道如何写最小的查询。
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;发布于 2019-02-12 22:36:00
你可以在你的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子查询不应该是必需的,因为联合查询本身应该删除所有重复的日期值。
发布于 2019-02-13 01:02:19
$userP = UserPresentation::get()
->only('date', 'accept_date', 'question_date', 'success_date')
->unique();100% Laravel方式,使用Collection,这将一起选择所有四个日期,Laravel Collection将从这些选择中找到唯一的行。
https://stackoverflow.com/questions/54652357
复制相似问题