如何过滤fname和lname? -MySQL的

时间:2019-04-12 12:23:17

标签: mysql filter union sql-like

我更新了“离开”表:

+--------+---------+---------+-------------+----------+----------+
|ID_LEAVE|ID_WORKER|    BEGIN_DATE         | END_DATE            | 
+--------+---------+---------+---------+-------------+-----------+
| 5      |   10    | 2019-03-22 07:00:00   |2019-03-25 15:00:00  | 
+--------+---------+---------+-------------+----------+----------+
| 6      |   10    | 2019-03-28 07:00:00   |2019-04-12 15:00:00  | 
+--------+---------+---------+-------------+----------+----------+
| 7      |   12    | 2019-03-28 07:00:00   |2019-04-09 15:00:00  | 
+--------+---------+---------+-------------+----------+----------+

和“工人表”:

+---------+---------+-------+
|ID_WORKER| FNAME   | LNAME | 
+---------+---------+-------+
|   10    | MARIO   | NEED  |
+---------+---------+-------+
|   12    | DARIO   | MARCO |
+---------+---------+-------+

我可以显示所有带离开时间的工人(全部来自“工人表”)。

我想做什么?

我希望将FNAME OR LNAME的过滤器短语与workers表一起加入。

我尝试了什么?

我尝试通过以下代码过滤FNAME OR LNAME的短语:

SELECT leave.ID_LEAVE, leave.ID_WORKER, workers.FNAME, workers.LNAME, leave.BEGIN_DATE, leave.END_DATE,  
FROM 
(SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS date_value 
 FROM 
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0, 
 (SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1, 
 (SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2, 
 (SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3, 
 (SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) calendar 
INNER JOIN leave ON calendar.date_value BETWEEN DATE(leave.BEGIN_DATE) AND DATE(leave.END_DATE) 
INNER JOIN workers ON leave.ID_WORKER = workers.ID_WORKER 
WHERE NOT WEEKDAY(date_value) IN (5, 6) AND (workers.FNAME LIKE 'Ma' OR workers.LNAME LIKE 'Ma') 
GROUP BY ID_LEAVE;

但是它什么也没显示。当我通过写短语“ Ma”来过滤LNAME或FNAME时,它将显示类似这样的内容:

+--------+---------+---------+-------------+----------+--------------------------
|ID_LEAVE|ID_WORKER| FNAME   | LNAME | BEGIN_DATE         | END_DATE            | 
+--------+---------+---------+---------+-------------+--------------------+------
| 5      |   10    | MARIO   | NEED  |2019-03-22 07:00:00 |2019-03-25 15:00:00  | 
+--------+---------+---------+-------------+----------+-------------------------- 
| 6      |   10    | MARIO   | NEED  |2019-03-28 07:00:00 |2019-04-09 15:00:00  | 
+--------+---------+---------+-------------+----------+-------------------------- 

有什么想法吗?我应该在该代码中更改什么?我可以指望您的支持吗?谢谢。

2 个答案:

答案 0 :(得分:1)

您在过滤条件中错过了'%'符号。 另外,您还必须在字段和要比较的数据上使用UPPER()函数, 这样过滤将不区分大小写

SELECT leave.ID_LEAVE, leave.ID_WORKER, workers.FNAME, workers.LNAME, leave.BEGIN_DATE, leave.END_DATE,  
FROM 
(SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS date_value 
 FROM 
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0, 
 (SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1, 
 (SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2, 
 (SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3, 
 (SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) calendar 
INNER JOIN leave ON calendar.date_value BETWEEN DATE(leave.BEGIN_DATE) AND DATE(leave.END_DATE) 
INNER JOIN workers ON leave.ID_WORKER = workers.ID_WORKER 
WHERE NOT WEEKDAY(date_value) IN (5, 6) AND (UPPER(workers.FNAME) LIKE '%MA%' OR UPPER(workers.LNAME) LIKE '%MA%') 
GROUP BY ID_LEAVE;

答案 1 :(得分:0)

您可以尝试此查询

select leave.ID_LEAVE, 
Workers.ID_WORKER, 
Workers.FNAME, 
Workers.LNAME, 
leave.BEGIN_DATE, 
leave.END_DATE from Workers 
join leave on Workers.ID_WORKER = leave.ID_WORKER 
where Workers.FNAME Like '%ma%';