我想问一下如何加入这些查询? 第一个是从我的桌面日历中获取的,当时所有星期一都会被取出,而第一个星期一则是。
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行,是否可以加入这两个查询?
由于
答案 0 :(得分:0)
这是假设您在表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';
答案 1 :(得分:0)
一种方法可能是
SELECT ( SELECT calender FROM calender WHERE
calender >= '2017-03-01'
AND calender <= '2017-03-31'
AND DAYOFWEEK(calender) = 2
OR calender = '2017-03-01'
GROUP BY calender) As Firsts, ( SELECT calender 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
) As Last FROM calender
答案 2 :(得分:0)
SELECT
startdate.Start, Enddate.End
FROM
(SELECT
@counter0:=@counter0 + 1 AS ID, calender AS Selesai
FROM
calender, (SELECT @counter0:=0) T
WHERE
calender BETWEEN startdate AND enddate
AND DAYOFWEEK(calender) = 1
OR calender = enddate) Enddate
JOIN
(SELECT
@counter:=@counter + 1 AS ID, calender AS Mulai
FROM
calender, (SELECT @counter:=0) T
WHERE
calender BETWEEN startdate AND enddate
AND DAYOFWEEK(calender) = 2
OR calender = startdate) Startdate ON Startdate.ID = Enddate.ID
这是我用row_number()解决问题的方法,谢谢我最好的朋友#Lun_Ng