mysql - 使用 IF 语句进行查询

标签 mysql select if-statement

我有一个关于 MYSQL If 语句的问题。 我的要求:

SELECT c.status, c.thumb, j.id, j.name, j.username, s.session_id, a.time, a.isactive, a.country, a.countrycode, a.city
FROM j16_users AS j 
JOIN j16_community_users AS c ON c.userid = j.id 
LEFT JOIN j16_session AS s ON s.userid = j.id 
LEFT JOIN j16_session AS s ON s.userid = j.id 
LEFT JOIN j16_x5_fastchat_users AS a ON a.userid = j.id 
WHERE j.id IN (62,66,2692)

如何添加新列:带有条件的 isonline 到上述请求:

IF(a.time > DATE_SUB(NOW(), INTERVAL 1 MINUTE), "1", "0") AS isonline

谢谢!

最佳答案

SELECT c.status, c.thumb, 
       j.id, j.name, 
       j.username, s.session_id, 
       a.time, a.isactive, 
       a.country, a.countrycode, a.city,
       IF(a.time > DATE_SUB(NOW(), INTERVAL 1 MINUTE), 1, 0) AS isOnline
FROM j16_users AS j 
     JOIN j16_community_users AS c ON c.userid = j.id 
     LEFT JOIN j16_session AS s ON s.userid = j.id 
     LEFT JOIN j16_session AS s ON s.userid = j.id 
     LEFT JOIN j16_x5_fastchat_users AS a ON a.userid = j.id 
WHERE j.id IN (62,66,2692)

关于mysql - 使用 IF 语句进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8743578/

相关文章:

Java 用 Enter 按钮中断 while 循环

javascript - 我怎样才能最小化我的javascript函数?

mysql - 在查询中使用 MySQL 关键字?

php - WordPress的Rest API在哪里执行对数据库的查询?

php - 如何从 'table' 中选择 WHERE 列 = *通配符*

sql - 消息 102,级别 15,状态 1,第 6 行 'Reference' 附近的语法不正确

sql - 分布式连接两个表

mysql - 如何做 SUM 而不是 UNION

mySQL 按今天排序,然后正常排序

php - PHP中的if语句结构