[sql] ต้องการ select ข้อมูลที่อายุไม่เกิน 7 วันต้องเขียนอย่างไรครับ

เริ่มโดย wch2013, 30 เมษายน 2013, 20:32:36

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

wch2013

ตาราง date เก็บรูปแบบข้อมูลเป็น DATETIME

ถ้าต้องการเลือกเฉพาะที่ date ผ่านไปแล้วไม่เกิน 7 วัน ต้องเขียนอย่างไรครับ

select * from table where date<=7  <== แบบนี้จะใช้ได้หรือเปล่าครับ

ขอบคุณครับ


wch2013


xvlnw.com

[direct=https://cloudhost.in.th/wordpress-hosting]ツ ⓌⓄⓇⒹⓅⓇⒺⓈⓈ ⒽⓄⓈⓉⒾⓃⒼ [/direct] :wanwan014:  :D
[direct=https://cloudhost.in.th/cloudhosting.html]Cloud Hosting[/direct] [direct=https://cloudhost.in.th/cloudvps.html]Cloud Server[/direct] [direct=https://cloudhost.in.th/vpshosting.html]Cloud VPS Hosting[/direct] [direct=https://cloudhost.in.th/windowsvps.html]Cloud Windows[/direct] Tel: 080-348-0843 LINE: ixvlnw

MapTwoZa

select * from your_table
where your_date_column > curdate()-interval 7 day


or

select * from your_table
where date(your_datetime_column) > curdate()-interval 7 day
Good code quality Developer :D

thai2ads

รับทำเว็บไซต์ เขียนระบบต่างๆตามที่ต้องการ
รับทำเว็บไซต์ | ทองคำราคาวันนี้

xvlnw.com

น่าสนใจดีนะครับ มีหลากหลายรูปแบบ

ลองจำลองข้อมูลขึ้นมาเยอะๆ ซัก 1 ล้าน Rec. แล้วลองใช้คำสั่ง SQL แต่ละแบบจับเวลาดูครับ ว่าแบบไหนไวกว่ากันครับ

:wanwan003:
[direct=https://cloudhost.in.th/wordpress-hosting]ツ ⓌⓄⓇⒹⓅⓇⒺⓈⓈ ⒽⓄⓈⓉⒾⓃⒼ [/direct] :wanwan014:  :D
[direct=https://cloudhost.in.th/cloudhosting.html]Cloud Hosting[/direct] [direct=https://cloudhost.in.th/cloudvps.html]Cloud Server[/direct] [direct=https://cloudhost.in.th/vpshosting.html]Cloud VPS Hosting[/direct] [direct=https://cloudhost.in.th/windowsvps.html]Cloud Windows[/direct] Tel: 080-348-0843 LINE: ixvlnw

wch2013