从同一表中的多列中选择日期

时间:2014-02-27 21:01:02

标签: php mysql sql html-table

我正在寻找一些帮助创建一个HTML表格,该表格将列出存储在Mysql表格中的下5个即将到来的日期。 我有一个表有2个String列& 10个不同的日期列:

我想要做的是创建一个包含以下标题的HTML表格: 的母马, 治疗与治疗 日期

日期列将填充5个日期。

处理列将填充该日期的MySql列名称

Mare 列将填充该日期行的母马字符串

我正在尝试编写一个SQL语句,该语句将从每个列中获取每个日期,检查是否大于今天的日期,按日期排序并限制为5。

到目前为止我所拥有的内容如下:

SELECT covering, 
 ovulation, 
 pregnancy, 
 scanfortwins, 
 heartbeatscan, 
 antiabortion1, 
 antiabortion2, 
 antiabortion3, 
 removestitches, 
 duedate 
 FROM   `dates` 
 WHERE  'covering' 
  AND 'ovulation' 
  AND 'pregnancy' 
  AND 'scanfortwins' 
  AND 'heartbeatscan' 
  AND 'antiabortion1' 
  AND 'antiabortion2' 
  AND 'antiabortion3' 
  AND 'removestitches' 
  AND 'duedate' >= Curdate() 
  ORDER  BY Year(date) DESC, 
  Month(date) DESC, 
  Day(date) DESC 
  LIMIT  5; 

如果我能获得一些帮助来创建HTML表格,那将非常感激

1 个答案:

答案 0 :(得分:0)

尝试从UNION子查询中进行选择,如下所示:

SELECT *
FROM
(
   SELECT ovulation AS `Date`, 'ovulation' AS Treatment, Mare
   FROM `dates`
   WHERE ovulation > DATE()
   ORDER BY ovulation ASC
   LIMIT 5
   UNION
   SELECT pregnancy AS `Date`, 'pregnancy' AS Treatment, Mare
   FROM `date`
   WHERE pregnancy > DATE()
   ORDER BY pregnancy ASC
   LIMIT 5
   ...
) AS x
ORDER BY x.`Date` ASC
LIMIT 5;