50
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi muốn danh sách các ngày nằm giữa hai ngày bằng truy vấn chọn. Ví dụ:
Nếu tôi đưa ra '2012-02-10' và '2012-02-15', tôi cần kết quả.
date
----------
2012-02-10
2012-02-11
2012-02-12
2012-02-13
2012-02-14
2012-02-15
Làm thế nào tôi có thể nhận được?
OMG Ponies
318K79 Huy hiệu vàng511 Huy hiệu bạc494 Huy hiệu đồng79 gold badges511 silver badges494 bronze badges
hỏi ngày 15 tháng 2 năm 2012 lúc 14:55Feb 15, 2012 at 14:55
4
Try:
select * from
[select adddate['1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i] selected_date from
[select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t0,
[select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t1,
[select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t2,
[select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t3,
[select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t4] v
where selected_date between '2012-02-10' and '2012-02-15'
-Có ngày dao động đến gần 300 năm trong tương lai.
[Đã sửa theo một chỉnh sửa được đề xuất bởi Urvishattynapse.]
Đã trả lời ngày 15 tháng 2 năm 2012 lúc 15:32Feb 15, 2012 at 15:32
7
set @i = -1;
SELECT DATE[ADDDATE['2012-02-10', INTERVAL @i:=@i+1 DAY]] AS date FROM `table`
HAVING
@i < DATEDIFF['2012-02-15', '2012-02-10']
Điều này sẽ trả về kết quả của bạn đặt chính xác như quy định. Truy vấn này chỉ yêu cầu bạn thay đổi hai ngày khác nhau trong Datediff và Bổ sung.
Đã trả lời ngày 15 tháng 2 năm 2012 lúc 15:34Feb 15, 2012 at 15:34
HexparrothExparrothexparrot
3.3311 Huy hiệu vàng22 Huy hiệu bạc32 Huy hiệu đồng1 gold badge22 silver badges32 bronze badges
6
Câu trả lời được chấp nhận không phù hợp với tôi trong MySQL 5.5. Tôi đã cập nhật truy vấn để làm việc cho tôi:
select * from
[select adddate['1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0] selected_date from
[select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t0,
[select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t1,
[select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t2,
[select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t3,
[select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9] t4] v
where selected_date between '2012-02-10' and '2012-02-15'
Đã trả lời ngày 11 tháng 12 năm 2012 lúc 6:34Dec 11, 2012 at 6:34
1
Bạn có thể tạo một bảng chứa tất cả các ngày bạn có thể cần sử dụng:
date
2000-01-01
2000-01-02
2000-01-03
...etc..
2100-12-30
2100-12-31
Sau đó, truy vấn bảng đó như sau:
SELECT date
FROM dates
WHERE date BETWEEN '2012-02-10' AND '2012-02-15'
Đã trả lời ngày 15 tháng 2 năm 2012 lúc 14:58Feb 15, 2012 at 14:58
Mark Byersmark ByersMark Byers
784K187 Huy hiệu vàng1552 Huy hiệu bạc1440 Huy hiệu đồng187 gold badges1552 silver badges1440 bronze badges
4
SELECT * FROM tablexxx WHERE datecol BETWEEN '2012-02-10' AND '2012-02-15';
Taryn
Phim thương hiệu vàng 238K5555 gold badges362 silver badges403 bronze badges
Đã trả lời ngày 15 tháng 2 năm 2012 lúc 14:58Feb 15, 2012 at 14:58
Mark Byersmark Byersblankabout
784K187 Huy hiệu vàng1552 Huy hiệu bạc1440 Huy hiệu đồng2 gold badges17 silver badges27 bronze badges
3