我有一个带有日期选择器的表,我正在尝试添加当前日期选项,除非该日期已存在行。
以下是它的外观:
今天是25日,但它显示了20日的日期,因为日期为07/20/2013的日期中存在行。我该怎么做才能显示当前日期?
这是代码
<div class="lookup">
<form action="index.php" method="get">
<select name="exam_date" ONCHANGE="location = this.options[this.selectedIndex].value;" id="type" class="neutral">
<?php
$sql_gdate = "SELECT distinct pat_date from patients ORDER BY pat_date DESC";
$result_gdate = mysql_query($sql_gdate);
while ($row_gdate = mysql_fetch_assoc($result_gdate)) {
echo '<option value="?exam_date=' . $row_gdate['pat_date'] . '"';
IF ($exam_date == $row_gdate['pat_date']) {
echo 'selected="selected"';
} else {
echo '';
}
echo '>' . $row_gdate['pat_date'] . '</option>';
}
?>
</select>
</form>
</div>
答案 0 :(得分:1)
select dt from
(
SELECT distinct pat_date as dt from patients
union
SELECT CURRENT_DATE() from dual
)
ORDER BY dt DESC
答案 1 :(得分:0)
您可以使用以下SQL修改表格,以便正确存储日期 -
ALTER TABLE patients
ADD COLUMN pat_date_new DATE NOT NULL;
UPDATE patients SET pat_date_new = STR_TO_DATE(pat_date,'%m/%d/%Y');
-- NOW CHECK THAT THE VALUES ARE STORED CORRECTLY BEFORE DROPPING THE OLD COLUMN
ALTER TABLE patients
DROP COLUMN pat_date,
CHANGE COLUMN pat_date_new pat_date DATE NOT NULL;
一旦日期以正确的格式存储,您就可以使用之前发布的查询 -
SELECT DISTINCT pat_date FROM patients UNION SELECT CURRENT_DATE ORDER BY pat_date DESC
您需要更改日期选择器中收到的日期格式。您可以在PHP或SQL中轻松完成此操作 -
INSERT INTO `patients` (`pat_date`) VALUES(STR_TO_DATE('07/25/2013','%m/%d/%Y'));