如何使用IFNULL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用IFNULL相关的知识,希望对你有一定的参考价值。
我有mysql查询,我需要在point_of_contact上放置IFNULL
条件。 point_of_contact
列中的行包含许多空值。我被困在哪里可以放置IFNULL
条件。这是我的MYSQL查询。
SELECT
*
FROM
(SELECT
funchead, entity_id
FROM
(SELECT
*
FROM
(SELECT
user_info.emp_id, allocation.entity_id,IFNULL('allocation.point_of_contact','empty')
FROM
allocation, entity, user_department_mapping, user_info, location_floor_mapping
WHERE
allocation.entity_id = entity.id
AND entity.status = 1
AND entity.location_floor_id = location_floor_mapping.id
AND allocation.user_department_id = user_department_mapping.id
AND user_department_mapping.user_id = user_info.id
**AND allocation.point_of_contact=user_info.emp_id**
AND allocation.status = 1
AND allocation.allocation_type in ('USER','RESERVED')
AND entity.location_floor_id IN (SELECT
id
FROM
location_floor_mapping
WHERE
location_id IN (SELECT
id
FROM
location
WHERE
id IN (1) AND status = 1)
AND status = 1)
GROUP BY user_info.emp_id , allocation.entity_id) AS allocationQuery
INNER JOIN (SELECT
c1emp_id,
IFNULL(C8derivedFunctionHead, IFNULL(C7derivedFunctionHead, IFNULL(C6derivedFunctionHead, IFNULL(C5derivedFunctionHead, IFNULL(C4derivedFunctionHead, IFNULL(C3derivedFunctionHead, IFNULL(C2derivedFunctionHead, 'NA'))))))) AS funchead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
c7emp_id,
C7band,
C7derivedFunctionHead,
c8emp_id,
C8band,
C8derivedFunctionHead,
C9.emp_id AS c9emp_id,
C9.band AS C9band,
CASE
WHEN C9.band NOT IN ('CX') THEN C9.supervisor_id
ELSE NULL
END AS C9derivedId,
CASE
WHEN C9.band IN ('CX') THEN C9.emp_id
ELSE NULL
END AS C9derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
c7emp_id,
C7band,
C7derivedFunctionHead,
C8.emp_id AS c8emp_id,
C8.band AS C8band,
CASE
WHEN C8.band NOT IN ('CX') THEN C8.supervisor_id
ELSE NULL
END AS C8derivedId,
CASE
WHEN C8.band IN ('CX') THEN C8.emp_id
ELSE NULL
END AS C8derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
C7.emp_id AS c7emp_id,
C7.band AS C7band,
CASE
WHEN C7.band NOT IN ('CX') THEN C7.supervisor_id
ELSE NULL
END AS C7derivedId,
CASE
WHEN C7.band IN ('CX') THEN C7.emp_id
ELSE NULL
END AS C7derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
C6.emp_id AS c6emp_id,
C6.band AS C6band,
CASE
WHEN C6.band NOT IN ('CX') THEN C6.supervisor_id
ELSE NULL
END AS C6derivedId,
CASE
WHEN C6.band IN ('CX') THEN C6.emp_id
ELSE NULL
END AS C6derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
C5.emp_id AS c5emp_id,
C5.band AS C5band,
CASE
WHEN C5.band NOT IN ('CX') THEN C5.supervisor_id
ELSE NULL
END AS C5derivedId,
CASE
WHEN C5.band IN ('CX') THEN C5.emp_id
ELSE NULL
END AS C5derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
C4.emp_id AS c4emp_id,
C4.band AS C4band,
CASE
WHEN C4.band NOT IN ('CX') THEN C4.supervisor_id
ELSE NULL
END AS C4derivedId,
CASE
WHEN C4.band IN ('CX') THEN C4.emp_id
ELSE NULL
END AS C4derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
C3.emp_id AS c3emp_id,
C3.band AS C3band,
CASE
WHEN C3.band NOT IN ('CX') THEN C3.supervisor_id
ELSE NULL
END AS C3derivedId,
CASE
WHEN C3.band IN ('CX') THEN C3.emp_id
ELSE NULL
END AS C3derivedFunctionHead
FROM
(SELECT
C1.emp_id AS c1emp_id,
C1.band AS C1band,
C2.emp_id AS c2emp_id,
C2.band AS C2band,
CASE
WHEN C2.band NOT IN ('CX') THEN C2.supervisor_id
ELSE NULL
END AS C2derivedId,
CASE
WHEN C2.band IN ('CX') THEN C2.emp_id
ELSE NULL
END AS C2derivedFunctionHead
FROM
(SELECT
*
FROM
user_info) AS C1
LEFT JOIN (SELECT
*
FROM
user_info) AS C2 ON C1.supervisor_id = C2.emp_id) AS CDERV1
LEFT JOIN (SELECT
*
FROM
user_info) AS C3 ON C2derivedId = C3.emp_id) AS CDERV2
LEFT JOIN (SELECT
*
FROM
user_info) AS C4 ON C3derivedId = C4.emp_id) AS CDERV3
LEFT JOIN (SELECT
*
FROM
user_info) AS C5 ON C4derivedId = C5.emp_id) AS CDERV4
LEFT JOIN (SELECT
*
FROM
user_info) AS C6 ON C5derivedId = C6.emp_id) AS CDERV5
LEFT JOIN (SELECT
*
FROM
user_info) AS C7 ON C6derivedId = C7.emp_id) AS CDERV6
LEFT JOIN (SELECT
*
FROM
user_info) AS C8 ON C7derivedId = C8.emp_id) AS CDERV7
LEFT JOIN (SELECT
*
FROM
user_info) AS C9 ON C8derivedId = C9.emp_id) AS CDERV8) AS functionheadmapping ON functionheadmapping.c1emp_id = allocationQuery.emp_id) AS total
GROUP BY funchead , entity_id) AS enno
WHERE
entity_id IN ((SELECT
entity_id
FROM
(SELECT
SUM(1) AS encount, entity_id
FROM
(SELECT
funchead, entity_id,IFNULL('allocation.point_of_contact',0)
FROM
(SELECT
*
FROM
(SELECT
user_info.emp_id, allocation.entity_id
FROM
allocation, entity, user_department_mapping, user_info, location_floor_mapping
WHERE
allocation.entity_id = entity.id
AND entity.status = 1
AND entity.location_floor_id = location_floor_mapping.id
AND allocation.user_department_id = user_department_mapping.id
AND user_department_mapping.user_id = user_info.id
**AND allocation.point_of_contact=user_info.emp_id**
AND allocation.status = 1
AND allocation.allocation_type in ('USER','RESERVED')
AND entity.location_floor_id IN (SELECT
id
FROM
location_floor_mapping
WHERE
location_id IN (SELECT
id
FROM
location
WHERE
id IN (1) AND status = 1)
AND status = 1)
GROUP BY user_info.emp_id , allocation.entity_id) AS allocationQuery
INNER JOIN (SELECT
c1emp_id,
IFNULL(C8derivedFunctionHead, IFNULL(C7derivedFunctionHead, IFNULL(C6derivedFunctionHead, IFNULL(C5derivedFunctionHead, IFNULL(C4derivedFunctionHead, IFNULL(C3derivedFunctionHead, IFNULL(C2derivedFunctionHead, 'NA'))))))) AS funchead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
c7emp_id,
C7band,
C7derivedFunctionHead,
c8emp_id,
C8band,
C8derivedFunctionHead,
C9.emp_id AS c9emp_id,
C9.band AS C9band,
CASE
WHEN C9.band NOT IN ('CX') THEN C9.supervisor_id
ELSE NULL
END AS C9derivedId,
CASE
WHEN C9.band IN ('CX') THEN C9.emp_id
ELSE NULL
END AS C9derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
c7emp_id,
C7band,
C7derivedFunctionHead,
C8.emp_id AS c8emp_id,
C8.band AS C8band,
CASE
WHEN C8.band NOT IN ('CX') THEN C8.supervisor_id
ELSE NULL
END AS C8derivedId,
CASE
WHEN C8.band IN ('CX') THEN C8.emp_id
ELSE NULL
END AS C8derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
C7.emp_id AS c7emp_id,
C7.band AS C7band,
CASE
WHEN C7.band NOT IN ('CX') THEN C7.supervisor_id
ELSE NULL
END AS C7derivedId,
CASE
WHEN C7.band IN ('CX') THEN C7.emp_id
ELSE NULL
END AS C7derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
C6.emp_id AS c6emp_id,
C6.band AS C6band,
CASE
WHEN C6.band NOT IN ('CX') THEN C6.supervisor_id
ELSE NULL
END AS C6derivedId,
CASE
WHEN C6.band IN ('CX') THEN C6.emp_id
ELSE NULL
END AS C6derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
C5.emp_id AS c5emp_id,
C5.band AS C5band,
CASE
WHEN C5.band NOT IN ('CX') THEN C5.supervisor_id
ELSE NULL
END AS C5derivedId,
CASE
WHEN C5.band IN ('CX') THEN C5.emp_id
ELSE NULL
END AS C5derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
C4.emp_id AS c4emp_id,
C4.band AS C4band,
CASE
WHEN C4.band NOT IN ('CX') THEN C4.supervisor_id
ELSE NULL
END AS C4derivedId,
CASE
WHEN C4.band IN ('CX') THEN C4.emp_id
ELSE NULL
END AS C4derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
C3.emp_id AS c3emp_id,
C3.band AS C3band,
CASE
WHEN C3.band NOT IN ('CX') THEN C3.supervisor_id
ELSE NULL
END AS C3derivedId,
CASE
WHEN C3.band IN ('CX') THEN C3.emp_id
ELSE NULL
END AS C3derivedFunctionHead
FROM
(SELECT
C1.emp_id AS c1emp_id,
C1.band AS C1band,
C2.emp_id AS c2emp_id,
C2.band AS C2band,
CASE
WHEN C2.band NOT IN ('CX') THEN C2.supervisor_id
ELSE NULL
END AS C2derivedId,
CASE
WHEN C2.band IN ('CX') THEN C2.emp_id
ELSE NULL
END AS C2derivedFunctionHead
FROM
(SELECT
*
FROM
user_info) AS C1
LEFT JOIN (SELECT
*
FROM
user_info) AS C2 ON C1.supervisor_id = C2.emp_id) AS CDERV1
LEFT JOIN (SELECT
*
FROM
user_info) AS C3 ON C2derivedId = C3.emp_id) AS CDERV2
LEFT JOIN (SELECT
*
FROM
user_info) AS C4 ON C3derivedId = C4.emp_id) AS CDERV3
LEFT JOIN (SELECT
*
FROM
user_info) AS C5 ON C4derivedId = C5.emp_id) AS CDERV4
LEFT JOIN (SELECT
*
FROM
user_info) AS C6 ON C5derivedId = C6.emp_id) AS CDERV5
LEFT JOIN (SELECT
*
FROM
user_info) AS C7 ON C6derivedId = C7.emp_id) AS CDERV6
LEFT JOIN (SELECT
*
FROM
user_info) AS C8 ON C7derivedId = C8.emp_id) AS CDERV7
LEFT JOIN (SELECT
*
FROM
user_info) AS C9 ON C8derivedId = C9.emp_id) AS CDERV8) AS functionheadmapping ON functionheadmapping.c1emp_id = allocationQuery.emp_id) AS total
GROUP BY funchead , entity_id) AS enn
GROUP BY entity_id) AS enn1
WHERE
encount <= 1))
UNION SELECT
'OTHERS', entity_id
FROM
(SELECT
SUM(1) AS encount, entity_id
FROM
(SELECT
funchead, entity_id
FROM
(SELECT
*
FROM
(SELECT
user_info.emp_id, allocation.entity_id
FROM
allocation, entity, user_department_mapping, user_info, location_floor_mapping
WHERE
allocation.entity_id = entity.id
AND entity.status = 1
AND entity.location_floor_id = location_floor_mapping.id
AND allocation.user_department_id = user_department_mapping.id
AND user_department_mapping.user_id = user_info.id
**AND allocation.point_of_contact=user_info.emp_id**
AND allocation.status = 1
AND allocation.allocation_type in ('USER','RESERVED')
AND entity.location_floor_id IN (SELECT
id
FROM
location_floor_mapping
WHERE
location_id IN (SELECT
id
FROM
location
WHERE
id IN (1) AND status = 1)
AND status = 1)
GROUP BY user_info.emp_id , allocation.entity_id) AS allocationQuery
INNER JOIN (SELECT
c1emp_id,
IFNULL(C8derivedFunctionHead, IFNULL(C7derivedFunctionHead, IFNULL(C6derivedFunctionHead, IFNULL(C5derivedFunctionHead, IFNULL(C4derivedFunctionHead, IFNULL(C3derivedFunctionHead, IFNULL(C2derivedFunctionHead, 'NA'))))))) AS funchead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
c7emp_id,
C7band,
C7derivedFunctionHead,
c8emp_id,
C8band,
C8derivedFunctionHead,
C9.emp_id AS c9emp_id,
C9.band AS C9band,
CASE
WHEN C9.band NOT IN ('CX') THEN C9.supervisor_id
ELSE NULL
END AS C9derivedId,
CASE
WHEN C9.band IN ('CX') THEN C9.emp_id
ELSE NULL
END AS C9derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
c7emp_id,
C7band,
C7derivedFunctionHead,
C8.emp_id AS c8emp_id,
C8.band AS C8band,
CASE
WHEN C8.band NOT IN ('CX') THEN C8.supervisor_id
ELSE NULL
END AS C8derivedId,
CASE
WHEN C8.band IN ('CX') THEN C8.emp_id
ELSE NULL
END AS C8derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
c6emp_id,
C6band,
C6derivedFunctionHead,
C7.emp_id AS c7emp_id,
C7.band AS C7band,
CASE
WHEN C7.band NOT IN ('CX') THEN C7.supervisor_id
ELSE NULL
END AS C7derivedId,
CASE
WHEN C7.band IN ('CX') THEN C7.emp_id
ELSE NULL
END AS C7derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
c5emp_id,
C5band,
C5derivedFunctionHead,
C6.emp_id AS c6emp_id,
C6.band AS C6band,
CASE
WHEN C6.band NOT IN ('CX') THEN C6.supervisor_id
ELSE NULL
END AS C6derivedId,
CASE
WHEN C6.band IN ('CX') THEN C6.emp_id
ELSE NULL
END AS C6derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
c4emp_id,
C4band,
C4derivedFunctionHead,
C5.emp_id AS c5emp_id,
C5.band AS C5band,
CASE
WHEN C5.band NOT IN ('CX') THEN C5.supervisor_id
ELSE NULL
END AS C5derivedId,
CASE
WHEN C5.band IN ('CX') THEN C5.emp_id
ELSE NULL
END AS C5derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
c3emp_id,
C3band,
C3derivedFunctionHead,
C4.emp_id AS c4emp_id,
C4.band AS C4band,
CASE
WHEN C4.band NOT IN ('CX') THEN C4.supervisor_id
ELSE NULL
END AS C4derivedId,
CASE
WHEN C4.band IN ('CX') THEN C4.emp_id
ELSE NULL
END AS C4derivedFunctionHead
FROM
(SELECT
c1emp_id,
C1band,
c2emp_id,
C2band,
C2derivedFunctionHead,
C3.emp_id AS c3emp_id,
C3.band AS C3band,
CASE
WHEN C3.band NOT IN ('CX') THEN C3.supervisor_id
ELSE NULL
END AS C3derivedId,
CASE
WHEN C3.band IN ('CX') THEN C3.emp_id
ELSE NULL
END AS C3derivedFunctionHead
FROM
(SELECT
C1.emp_id AS c1emp_id,
C1.band AS C1band,
C2.emp_id AS c2emp_id,
C2.band AS C2band,
CASE
WHEN C2.band NOT IN ('CX') THEN C2.supervisor_id
ELSE NULL
END AS C2derivedId,
CASE
WHEN C2.band IN ('CX') THEN C2.emp_id
ELSE NULL
END AS C2derivedFunctionHead
FROM
(SELECT
*
FROM
user_info) AS C1
LEFT JOIN (SELECT
*
FROM
user_info) AS C2 ON C1.supervisor_id = C2.emp_id) AS CDERV1
LEFT JOIN (SELECT
*
FROM
user_info) AS C3 ON C2derivedId = C3.emp_id) AS CDERV2
LEFT JOIN (SELECT
*
FROM
user_info) AS C4 ON C3derivedId = C4.emp_id) AS CDERV3
LEFT JOIN (SELECT
*
FROM
user_info) AS C5 ON C4derivedId = C5.emp_id) AS CDERV4
LEFT JOIN (SELECT
*
FROM
user_info) AS C6 ON C5derivedId = C6.emp_id) AS CDERV5
LEFT JOIN (SELECT
*
FROM
user_info) AS C7 ON C6derivedId = C7.emp_id) AS CDERV6
LEFT JOIN (SELECT
*
FROM
user_info) AS C8 ON C7derivedId = C8.emp_id) AS CDERV7
LEFT JOIN (SELECT
*
FROM
user_info) AS C9 ON C8derivedId = C9.emp_id) AS CDERV8) AS functionheadmapping ON functionheadmapping.c1emp_id = allocationQuery.emp_id) AS total
GROUP BY funchead , entity_id) AS enn
GROUP BY entity_id) AS enn1
WHERE
encount > 1
我已经尝试放入IFNULL('allocation.point_of_contact','empty')
。
但是在执行查询后,它会显示空白列。
我的point_of_contact列中有这样的行
point_of_contact
null
EMP004
null
null
null
所以我希望在分配时考虑Emp004.point_of_contact = user_info.emp_id
答案
您可以尝试这个吗:
IF(allocation.point_of_contact IS NULL ,"",allocation.point_of_contact) AS point_of_contact
以上是关于如何使用IFNULL的主要内容,如果未能解决你的问题,请参考以下文章
mysql ifnull() 返回的仍然为空值null,怎么办?
SQLite 等效于 ISNULL()、NVL()、IFNULL() 或 COALESCE()