我想问一下如何加入这个查询?
当星期一全都取完时,我会从我的餐桌日历中获得第一个信息,而当月则是第一个。

SELECT
calender As Firsts
FROM
calender
WHERE
calender >= '2017-03-01'
    AND calender <= '2017-03-31'
    AND DAYOFWEEK(calender) = 2
    OR calender = '2017-03-01'
GROUP BY calender;


然后我从日历表中取出月份中的所有星期日和最后一天。

SELECT
calender As Lasted
FROM
calender
WHERE
calender.calender >= '2017-03-01'
    AND calender.calender <= '2017-03-31'
    AND DAYOFWEEK(calender) = 1
    OR calender = '2017-03-31'
GROUP BY calender;


我想要这样的输出

First | Last
2017-03-01  | 2017-03-05
2017-03-06  | 2017-03-12
2017-03-13  | 2017-03-19
2017-03-20  | 2017-03-26
2017-03-27  | 2017-03-31


我尝试使用子查询,然后错误1242子查询返回多于1行,是否有可能将这两个查询联接在一起?

谢谢

最佳答案

这是假设您在表calendar上具有主键

SELECT
   (SELECT calender
    FROM calendar ic
    WHERE ic.primary_key = oc.primary_key
     AND DAYOFWEEK(ic.calender) = 2
    GROUP BY calendar) As First,
   (SELECT calender
    FROM calendar ic
    WHERE ic.primary_key = oc.primary_key
     AND DAYOFWEEK(ic.calender) = 1
    GROUP BY calendar) As Last,
FROM
  calender oc
WHERE
  calender >= '2017-03-01'
 AND calender <= '2017-03-31'
 OR calender = '2017-03-01';

10-04 21:49