将多个列合并为一个并获得即将到期的日期

时间:2014-12-16 03:42:39

标签: mysql sql

所以我有一张看起来像这样的表

+--------------+------------+------------+------------+
| 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 |
+--------------+------------+------------+------------+

我从另一张桌子获取名字时需要它看起来像这样。

+---------------+--------------+
| 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   |
+---------------+--------------+

Expring lics字段从许可证表中获取从今天起15天内即将到期的所有到期日期。

另外我需要重复一遍,例如,如果John Doe有2个许可证我需要它显示如下:

+---------------+------------+
| 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 |
+---------------+------------+

问我任何我对mysql都很新的事情,所以我会尽力而为

所以我有一张看起来像这样的表

+--------------+------------+------------+------------+
| 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 |
+--------------+------------+------------+------------+

我从另一张桌子获取名字时需要它看起来像这样。

+---------------+--------------+
| 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   |
+---------------+--------------+

Expring lics字段从许可证表中获取从今天起15天内即将到期的所有到期日期。

另外我需要重复一遍,例如,如果John Doe有2个许可证我需要它显示如下:

+---------------+------------+
| 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 |
+---------------+------------+

问我任何我对mysql都很新的事情,所以我会尽力而为

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

1 个答案:

答案 0 :(得分:2)

使用连接

语法:

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);