简体   繁体   English

返回 IN 值的默认结果,不管

[英]Return default result for IN value regardless

I have a query that gets all user that have been online between a certain date range.我有一个查询,可以获取在某个日期范围内在线的所有用户。 This is done via an IN query due to how I want to display the data.由于我想要显示数据的方式,这是通过 IN 查询完成的。 However, I would like to return a default value if no records were found for an ID that was parsed into the IN condition.但是,如果没有找到解析为 IN 条件的 ID 的记录,我想返回一个默认值。

Simplified Query:简化查询:

SELECT users.Name, users.ID, SUM(users.Minutes) AS MinutesOnline
FROM UserTable
     LEFT JOIN OnlineUseage ON OnlineUseage.ID = UserTable.ID
WHERE OnlineUseage.Date >= '2016-01-01 00:00:00' AND OnlineUseage.Date <= '2016-12-31 23:59:59'
AND UserTable.ID IN(332,554,5764,11,556,.........)
GROUP BY users.ID
ORDER BY FIELD(UserTable.ID, 332,554,5764,11,556,.........)

Now the above query will only pull in those row that meet the condition, as expected.现在上面的查询只会拉入那些符合条件的行,正如预期的那样。 I would also like the query to pull in a default value for the ID's within the IN condition that don't meet the condition.我还希望查询为不满足条件的 IN 条件中的 ID 提取默认值。

Using IFNULL in this instance will not work as the record is never returned在这种情况下使用IFNULL将不起作用,因为永远不会返回记录

SELECT users.Name, users.ID, IFNULL(SUM(users.Minutes), 0) AS MinutesOnline
FROM UserTable
    LEFT JOIN OnlineUseage ON OnlineUseage.ID = UserTable.ID
WHERE OnlineUseage.Date >= '2016-01-01 00:00:00' AND OnlineUseage.Date <= '2016-12-31 23:59:59'
AND UserTable.ID IN(332,554,5764,11,556,.........)
GROUP BY users.ID
ORDER BY FIELD(UserTable.ID, 332,554,5764,11,556,.........)

FYI - i'm parsing this query into a custom PDO function.仅供参考 - 我正在将此查询解析为自定义 PDO function。 I'm not using deprecated mysql functions我没有使用过时的 mysql 函数

You have a condition on OnlineUseage the left join become like a inner join.您在OnlineUseage上有一个条件,左连接变得像内连接。

move your condition to the from clause will be better :将您的条件移至from子句会更好:

SELECT
    users.Name,
    users.ID,
    IFNULL(SUM(users.Minutes), 0) AS MinutesOnline
FROM
    users
    LEFT JOIN OnlineUseage ON
        OnlineUseage.ID = users.ID and
        OnlineUseage.Date >= '2016-01-01 00:00:00' AND
        OnlineUseage.Date <= '2016-12-31 23:59:59'
WHERE
    users.ID IN (332,554,5764,11,556,.........)
GROUP BY
    users.ID,users.Name
ORDER BY
    users.ID

When you use LEFT JOIN you have to put the conditions on the second table into the ON clause.当您使用LEFT JOIN您必须将第二个表上的条件放入ON子句中。 Putting them in the WHERE clause filters out all the rows that don't have matches, because those columns will be NULL and the conditions will never succeed.将它们放在WHERE子句中会过滤掉所有没有匹配项的行,因为这些列将为NULL并且条件永远不会成功。

SELECT users.Name, users.ID, IFNULL(SUM(OnlineUseage.Minutes), 0) AS MinutesOnline
FROM UserTable
LEFT JOIN OnlineUseage ON OnlineUseage.ID = UserTable.ID
    AND OnlineUseage.Date >= '2016-01-01 00:00:00' AND OnlineUseage.Date <= '2016-12-31 23:59:59'
WHERE UserTable.ID IN(332,554,5764,11,556,.........)
GROUP BY UserTable.ID
ORDER BY FIELD(UserTable.ID, 332,554,5764,11,556,.........)

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

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