简体   繁体   English

COALESCE和IFNULL不能按预期工作

[英]COALESCE and IFNULL not working as expected

I'm trying to output a default row when there's no row found in the query. 当查询中没有找到行时,我正在尝试输出默认行。 This is a sample of my query: 这是我的查询的示例:

SELECT 
COALESCE(site, 'STE') as site, 
instrument,
field

FROM Table1
WHERE site IN ('East', 'West')
AND DATE(tstamp) = "2016-09-07"
ORDER BY id desc

The output is 输出是

+------+------------+-------+
| site | instrument | field |
+------+------------+-------+
| West | 0          | 0     |
+------+------------+-------+

For the tsamp 2016-09-07 we have a row for the site "West" and there's no row for "East". 对于ts​​amp 2016-09-07,网​​站“ West”有一行,“ East”没有一行。 I tried to search and found that I can use COALESCE and also tried IFNULL but I'm only getting the output above. 我尝试搜索,发现可以使用COALESCE,还尝试了IFNULL,但是我只得到上面的输出。 I also tried if(count(site) = 0, "STE", site) but i can't get it to work. 我也尝试过if(count(site)= 0,“ STE”,site)但我无法使它正常工作。

My expected result is 我的预期结果是

+------+------------+-------+
| site | instrument | field |
+------+------------+-------+
| West | 0          | 0     |
| STE  | NULL       | NULL  |
+------+------------+-------+   

I hope you guys can help me. 我希望你们能帮助我。 Thanks in advance 提前致谢

Both coalesce() and ifnull() work on a row basis, meaning they can replace a null value if that null value exists in a record. coalesce()和ifnull()都基于行工作,这意味着如果记录中存在空值,则它们可以替换空值。 However, they cannot create a record that does not exist - and you do not have any records matching East (or STE). 但是,它们无法创建不存在的记录-并且您没有与East(或STE)匹配的记录。

A possible solution is to create a table that has all possible values for the site field and you can left join on this table: 一个可能的解决方案是创建一个表,该表具有site字段的所有可能值,您可以在该表上保留联接:

SELECT 
COALESCE(Table1.site, "STE") as site, 
Table1.instrument,
Table1.field

FROM LookupTable lt
LEFT JOIN Table1 ON lt.site=Table1.site
WHERE lt.site IN ('East', 'West')
AND DATE(Table1.tstamp) = "2016-09-07"
ORDER BY id desc

If 'STE' isn't in table1.site then it won't come back in the results. 如果“ STE”不在table1.site中,则它将不会返回结果中。

You could do a union instead: 您可以改为工会:

SELECT 
site, 
instrument,
field

FROM (SELECT 'West' [Site], 0 [instrument], 0 [field]) table1
WHERE site IN ('East', 'West')
UNION 
SELECT 'STE', NULL, NULL

Note that by using "STE" you're looking for the column name "STE", not the value 'STE' (single quotes) 请注意,通过使用“ STE”,您正在查找列名“ STE”,而不是值“ STE”(单引号)

EDIT 编辑

You need a control table to dictate which values to look for, or you have to hard code your rules. 您需要一个控制表来指示要查找的值,或者您必须对规则进行硬编码。 You can't look for something that's missing without first specifying the things that should be there. 您必须先指定应该存在的内容,才能找到缺少的内容。

Here's an option: 这是一个选择:

--Create and populate reference table "sites"
create table sites
    ([site] char (4))

INSERT INTO sites VALUES ('East')
INSERT INTO sites VALUES ('West')

-- Query against reference table
SELECT 
ISNULL(table1.site, 'STE'), 
instrument,
field
FROM (SELECT 'West' [Site], 0 [instrument], 0 [field]) table1
RIGHT JOIN sites on table1.[Site] = sites.[Site]

--or

-- Query against reference table
SELECT 
ISNULL(table1.site, 'STE'), 
instrument,
field
FROM sites
LEFT JOIN (SELECT 'West' [Site], 0 [instrument], 0 [field]) table1  on table1.[Site] = sites.[Site]

Let me know if you have questions on how this works. 如果您对此有疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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