简体   繁体   中英

Select dates from Multiple columns in same table

I am looking for a bit of help creating an HTML table that will list the next 5 upcoming dates stored within a Mysql table. I have a table which has 2 String columns & 10 different date columns:

What I want to do is create a HTML table with the following headers: Mare, Treatment & Date

The Date column will be filled with the 5 dates.

The Treatment column will be filled with the MySql column name of that date

The Mare column will be filled with the mare string of that dates row

I am trying to write an SQL statement that will take every date from each of the columns, check to see if is greater than today's date, order by date and limit to 5.

What I have so far is as follows:

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; 

If I could get a bit of help creating the HTML table it would be much appreciated

Try selecting from a UNION sub-query, as follows:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM