工作筆記——常用時間視圖

  • 零點到24點
SELECT
	date_format(curdate(), '%H:%i:%s') AS `day_hour`
UNION
	SELECT
		date_format(
			(curdate() + INTERVAL 1 HOUR),
			'%H:%i:%s'
		) AS `day_hour`
	UNION
		SELECT
			date_format(
				(curdate() + INTERVAL 2 HOUR),
				'%H:%i:%s'
			) AS `day_hour`
		UNION
			SELECT
				date_format(
					(curdate() + INTERVAL 3 HOUR),
					'%H:%i:%s'
				) AS `day_hour`
			UNION
				SELECT
					date_format(
						(curdate() + INTERVAL 4 HOUR),
						'%H:%i:%s'
					) AS `day_hour`
				UNION
					SELECT
						date_format(
							(curdate() + INTERVAL 5 HOUR),
							'%H:%i:%s'
						) AS `day_hour`
					UNION
						SELECT
							date_format(
								(curdate() + INTERVAL 6 HOUR),
								'%H:%i:%s'
							) AS `day_hour`
						UNION
							SELECT
								date_format(
									(curdate() + INTERVAL 7 HOUR),
									'%H:%i:%s'
								) AS `day_hour`
							UNION
								SELECT
									date_format(
										(curdate() + INTERVAL 8 HOUR),
										'%H:%i:%s'
									) AS `day_hour`
								UNION
									SELECT
										date_format(
											(curdate() + INTERVAL 9 HOUR),
											'%H:%i:%s'
										) AS `day_hour`
									UNION
										SELECT
											date_format(
												(curdate() + INTERVAL 10 HOUR),
												'%H:%i:%s'
											) AS `day_hour`
										UNION
											SELECT
												date_format(
													(curdate() + INTERVAL 11 HOUR),
													'%H:%i:%s'
												) AS `day_hour`
											UNION
												SELECT
													date_format(
														(curdate() + INTERVAL 12 HOUR),
														'%H:%i:%s'
													) AS `day_hour`
												UNION
													SELECT
														date_format(
															(curdate() + INTERVAL 13 HOUR),
															'%H:%i:%s'
														) AS `day_hour`
													UNION
														SELECT
															date_format(
																(curdate() + INTERVAL 14 HOUR),
																'%H:%i:%s'
															) AS `day_hour`
														UNION
															SELECT
																date_format(
																	(curdate() + INTERVAL 15 HOUR),
																	'%H:%i:%s'
																) AS `day_hour`
															UNION
																SELECT
																	date_format(
																		(curdate() + INTERVAL 16 HOUR),
																		'%H:%i:%s'
																	) AS `day_hour`
																UNION
																	SELECT
																		date_format(
																			(curdate() + INTERVAL 17 HOUR),
																			'%H:%i:%s'
																		) AS `day_hour`
																	UNION
																		SELECT
																			date_format(
																				(curdate() + INTERVAL 18 HOUR),
																				'%H:%i:%s'
																			) AS `day_hour`
																		UNION
																			SELECT
																				date_format(
																					(curdate() + INTERVAL 19 HOUR),
																					'%H:%i:%s'
																				) AS `day_hour`
																			UNION
																				SELECT
																					date_format(
																						(curdate() + INTERVAL 20 HOUR),
																						'%H:%i:%s'
																					) AS `day_hour`
																				UNION
																					SELECT
																						date_format(
																							(curdate() + INTERVAL 21 HOUR),
																							'%H:%i:%s'
																						) AS `day_hour`
																					UNION
																						SELECT
																							date_format(
																								(curdate() + INTERVAL 22 HOUR),
																								'%H:%i:%s'
																							) AS `day_hour`
																						UNION
																							SELECT
																								date_format(
																									(curdate() + INTERVAL 23 HOUR),
																									'%H:%i:%s'
																								) AS `day_hour`
  • 當前月往前30天
SELECT
	date_format(curdate(), '%Y-%m-%d') AS `month_day`
UNION
	SELECT
		date_format(
			(curdate() - INTERVAL 1 DAY),
			'%Y-%m-%d'
		) AS `month_day`
	UNION
		SELECT
			date_format(
				(curdate() - INTERVAL 2 DAY),
				'%Y-%m-%d'
			) AS `month_day`
		UNION
			SELECT
				date_format(
					(curdate() - INTERVAL 3 DAY),
					'%Y-%m-%d'
				) AS `month_day`
			UNION
				SELECT
					date_format(
						(curdate() - INTERVAL 4 DAY),
						'%Y-%m-%d'
					) AS `month_day`
				UNION
					SELECT
						date_format(
							(curdate() - INTERVAL 5 DAY),
							'%Y-%m-%d'
						) AS `month_day`
					UNION
						SELECT
							date_format(
								(curdate() - INTERVAL 6 DAY),
								'%Y-%m-%d'
							) AS `month_day`
						UNION
							SELECT
								date_format(
									(curdate() - INTERVAL 7 DAY),
									'%Y-%m-%d'
								) AS `month_day`
							UNION
								SELECT
									date_format(
										(curdate() - INTERVAL 8 DAY),
										'%Y-%m-%d'
									) AS `month_day`
								UNION
									SELECT
										date_format(
											(curdate() - INTERVAL 9 DAY),
											'%Y-%m-%d'
										) AS `month_day`
									UNION
										SELECT
											date_format(
												(curdate() - INTERVAL 10 DAY),
												'%Y-%m-%d'
											) AS `month_day`
										UNION
											SELECT
												date_format(
													(curdate() - INTERVAL 11 DAY),
													'%Y-%m-%d'
												) AS `month_day`
											UNION
												SELECT
													date_format(
														(curdate() - INTERVAL 12 DAY),
														'%Y-%m-%d'
													) AS `month_day`
												UNION
													SELECT
														date_format(
															(curdate() - INTERVAL 13 DAY),
															'%Y-%m-%d'
														) AS `month_day`
													UNION
														SELECT
															date_format(
																(curdate() - INTERVAL 14 DAY),
																'%Y-%m-%d'
															) AS `month_day`
														UNION
															SELECT
																date_format(
																	(curdate() - INTERVAL 15 DAY),
																	'%Y-%m-%d'
																) AS `month_day`
															UNION
																SELECT
																	date_format(
																		(curdate() - INTERVAL 16 DAY),
																		'%Y-%m-%d'
																	) AS `month_day`
																UNION
																	SELECT
																		date_format(
																			(curdate() - INTERVAL 17 DAY),
																			'%Y-%m-%d'
																		) AS `month_day`
																	UNION
																		SELECT
																			date_format(
																				(curdate() - INTERVAL 18 DAY),
																				'%Y-%m-%d'
																			) AS `month_day`
																		UNION
																			SELECT
																				date_format(
																					(curdate() - INTERVAL 19 DAY),
																					'%Y-%m-%d'
																				) AS `month_day`
																			UNION
																				SELECT
																					date_format(
																						(curdate() - INTERVAL 20 DAY),
																						'%Y-%m-%d'
																					) AS `month_day`
																				UNION
																					SELECT
																						date_format(
																							(curdate() - INTERVAL 21 DAY),
																							'%Y-%m-%d'
																						) AS `month_day`
																					UNION
																						SELECT
																							date_format(
																								(curdate() - INTERVAL 22 DAY),
																								'%Y-%m-%d'
																							) AS `month_day`
																						UNION
																							SELECT
																								date_format(
																									(curdate() - INTERVAL 23 DAY),
																									'%Y-%m-%d'
																								) AS `month_day`
																							UNION
																								SELECT
																									date_format(
																										(curdate() - INTERVAL 24 DAY),
																										'%Y-%m-%d'
																									) AS `month_day`
																								UNION
																									SELECT
																										date_format(
																											(curdate() - INTERVAL 25 DAY),
																											'%Y-%m-%d'
																										) AS `month_day`
																									UNION
																										SELECT
																											date_format(
																												(curdate() - INTERVAL 26 DAY),
																												'%Y-%m-%d'
																											) AS `month_day`
																										UNION
																											SELECT
																												date_format(
																													(curdate() - INTERVAL 27 DAY),
																													'%Y-%m-%d'
																												) AS `month_day`
																											UNION
																												SELECT
																													date_format(
																														(curdate() - INTERVAL 28 DAY),
																														'%Y-%m-%d'
																													) AS `month_day`
																												UNION
																													SELECT
																														date_format(
																															(curdate() - INTERVAL 29 DAY),
																															'%Y-%m-%d'
																														) AS `month_day`
  • 往前7天
SELECT
	date_format(curdate(), '%Y-%m-%d') AS `week_day`
UNION
	SELECT
		date_format(
			(curdate() - INTERVAL 1 DAY),
			'%Y-%m-%d'
		) AS `week_day`
	UNION
		SELECT
			date_format(
				(curdate() - INTERVAL 2 DAY),
				'%Y-%m-%d'
			) AS `week_day`
		UNION
			SELECT
				date_format(
					(curdate() - INTERVAL 3 DAY),
					'%Y-%m-%d'
				) AS `week_day`
			UNION
				SELECT
					date_format(
						(curdate() - INTERVAL 4 DAY),
						'%Y-%m-%d'
					) AS `week_day`
				UNION
					SELECT
						date_format(
							(curdate() - INTERVAL 5 DAY),
							'%Y-%m-%d'
						) AS `week_day`
					UNION
						SELECT
							date_format(
								(curdate() - INTERVAL 6 DAY),
								'%Y-%m-%d'
							) AS `week_day`
  • 往前12個月
SELECT
	date_format(curdate(), '%Y-%m') AS `year_month`
UNION
	SELECT
		date_format(
			(curdate() - INTERVAL 1 MONTH),
			'%Y-%m'
		) AS `year_month`
	UNION
		SELECT
			date_format(
				(curdate() - INTERVAL 2 MONTH),
				'%Y-%m'
			) AS `year_month`
		UNION
			SELECT
				date_format(
					(curdate() - INTERVAL 3 MONTH),
					'%Y-%m'
				) AS `year_month`
			UNION
				SELECT
					date_format(
						(curdate() - INTERVAL 4 MONTH),
						'%Y-%m'
					) AS `year_month`
				UNION
					SELECT
						date_format(
							(curdate() - INTERVAL 5 MONTH),
							'%Y-%m'
						) AS `year_month`
					UNION
						SELECT
							date_format(
								(curdate() - INTERVAL 6 MONTH),
								'%Y-%m'
							) AS `year_month`
						UNION
							SELECT
								date_format(
									(curdate() - INTERVAL 7 MONTH),
									'%Y-%m'
								) AS `year_month`
							UNION
								SELECT
									date_format(
										(curdate() - INTERVAL 8 MONTH),
										'%Y-%m'
									) AS `year_month`
								UNION
									SELECT
										date_format(
											(curdate() - INTERVAL 9 MONTH),
											'%Y-%m'
										) AS `year_month`
									UNION
										SELECT
											date_format(
												(curdate() - INTERVAL 10 MONTH),
												'%Y-%m'
											) AS `year_month`
										UNION
											SELECT
												date_format(
													(curdate() - INTERVAL 11 MONTH),
													'%Y-%m'
												) AS `year_month`
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章