简体   繁体   中英

Join multiple columns into one and getting upcoming expiration dates

So I have an table that looks like this

+--------------+------------+------------+------------+
| licxp1       | licxp2     | licxp3     | licxp4     |
+--------------+------------+------------+------------+
| 2014-12-18   | 2014-10-29 | 2014-12-11 | 2014-12-01 |
| 2014-12-29   | 2014-10-22 | 2014-12-18 | 2014-12-15 |
| 2014-12-18   | 2014-11-18 | 2014-12-17 | 2014-12-12 |
| 2014-12-27   | 2014-12-19 | 2014-12-18 | 2014-12-07 |
| 2014-12-18   | 2014-12-18 | 2014-12-19 | 2014-12-05 |
| 2014-12-23   | 2014-12-18 | 2014-12-18 | 2014-12-16 |
| 2014-12-18   | 2014-12-18 | 2014-12-18 | 2014-12-18 |
+--------------+------------+------------+------------+

And I need it to look like this while getting the First name from another table.

+---------------+--------------+
| Name          | Expring lics |
+---------------+--------------+
| John Doe      | 2014-10-29   |
| Johnny Cash   | 2014-10-22   |
| Bruice Willis | 2014-11-18   |
| Led Zeplin    | 2014-12-19   |
| Def leapord   | 2014-12-18   |
| Iron Madden   | 2014-12-18   |
| etc.          | 2014-12-18   |
+---------------+--------------+

The Expring lics field gets all the upcoming expiration that are 15 days from today from the license table.

Also I need it to repeat for example if there are 2 licenses for John Doe I need it to display like this:

+---------------+------------+
| John Doe      | 2014-10-10 |
| John Doe      | 2014-10-21 |
| John Doe      | 2014-10-27 |
| John Doe      | 2014-10-20 |
| John Doe      | 2014-10-22 |
| Johnny Cash   | 2014-10-21 |
| Bruice Willis | 2014-11-18 |
| Led Zeplin    | 2014-12-19 |
| Def leapord   | 2014-12-18 |
| Iron Madden   | 2014-12-18 |
| etc.          | 2014-12-18 |
+---------------+------------+

Ask me anything I'm very new to mysql so I will try my hardest

So I have an table that looks like this

+--------------+------------+------------+------------+
| licxp1       | licxp2     | licxp3     | licxp4     |
+--------------+------------+------------+------------+
| 2014-12-18   | 2014-10-29 | 2014-12-11 | 2014-12-01 |
| 2014-12-29   | 2014-10-22 | 2014-12-18 | 2014-12-15 |
| 2014-12-18   | 2014-11-18 | 2014-12-17 | 2014-12-12 |
| 2014-12-27   | 2014-12-19 | 2014-12-18 | 2014-12-07 |
| 2014-12-18   | 2014-12-18 | 2014-12-19 | 2014-12-05 |
| 2014-12-23   | 2014-12-18 | 2014-12-18 | 2014-12-16 |
| 2014-12-18   | 2014-12-18 | 2014-12-18 | 2014-12-18 |
+--------------+------------+------------+------------+

And I need it to look like this while getting the First name from another table.

+---------------+--------------+
| Name          | Expring lics |
+---------------+--------------+
| John Doe      | 2014-10-29   |
| Johnny Cash   | 2014-10-22   |
| Bruice Willis | 2014-11-18   |
| Led Zeplin    | 2014-12-19   |
| Def leapord   | 2014-12-18   |
| Iron Madden   | 2014-12-18   |
| etc.          | 2014-12-18   |
+---------------+--------------+

The Expring lics field gets all the upcoming expiration that are 15 days from today from the license table.

Also I need it to repeat for example if there are 2 licenses for John Doe I need it to display like this:

+---------------+------------+
| John Doe      | 2014-10-10 |
| John Doe      | 2014-10-21 |
| John Doe      | 2014-10-27 |
| John Doe      | 2014-10-20 |
| John Doe      | 2014-10-22 |
| Johnny Cash   | 2014-10-21 |
| Bruice Willis | 2014-11-18 |
| Led Zeplin    | 2014-12-19 |
| Def leapord   | 2014-12-18 |
| Iron Madden   | 2014-12-18 |
| etc.          | 2014-12-18 |
+---------------+------------+

Ask me anything I'm very new to mysql so I will try my hardest

SELECT
id_1.full_name,
id_1.hire_date,
id_1.active,
id.professional_license,
id.license_verification,
id.cpr,
id.drivers_license,
id.car_insurance,
id.inital_competemcy_checklist,
id.annual_performance,
id.hand_washing,
id.pain_assessment,
id.competency_ev_glucometer,
id.glucometer_competency
FROM
registered_nurses id_1 INNER JOIN licenses id ON id_1.id = id.id
WHERE
id.professional_license,  id.license_verification,
id.cpr,id.drivers_license,
id.car_insurance, id.inital_competemcy_checklist,
id.inital_competemcy_checklist,
id.competency_ev_glucometer,
glucometer_competency     >=            DATE(now())
 AND
 id.professional_license,         
 id.license_verification,
 id.cpr,id.drivers_license,
 id.car_insurance, 
 id.inital_competemcy_checklist,
 id.competency_ev_glucometer,  
 glucometer_competency 
  <= DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
 ORDER BY
 id.id
                                                                                            <=   DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
 ORDER BY
 id.id

use joins

syntax:

SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;


SELECT users.unm,licenses.licxp1 FROM licenses INNER JOIN users ON licenses.uid = users.uid 
where licenses.licxp1 <= DATE_ADD(DATE(now()), INTERVAL 5 DAY); 

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