使用 IF 语句进行查询

发布于 2024-12-25 11:01:49 字数 586 浏览 2 评论 0原文

我有一个关于 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)

如何添加一个新列:isoline with condition到上述请求:

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

谢谢!

I have an question about MYSQL If statement.
My request:

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)

How can I add a new column: isonline with condition to above request:

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2025-01-01 11:01:50
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)
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)
若有似无的小暗淡 2025-01-01 11:01:50

只需将其添加到SELECT 列表中即可。 CASE 语句 是不过建议更便携。

SELECT 
  c.status, 
  c.thumb, 
  j.id, 
  j.name, 
  j.username, 
  s.session_id, 
  a.time, 
  a.isactive, 
  a.country, 
  a.countrycode, 
  a.city,
  CASE WHEN a.time > DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN 1 ELSE 0 END 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)

由于您只需要一个布尔值 1 或 0,因此该语句也可以不使用 CASEIF 编写为:

 ... 
 a.countrycode, 
 a.city,
 (a.time > DATE_SUB(NOW(), INTERVAL 1 MINUTE)) AS isonline
 ...

,也将具有相同的效果。

Just add it into the SELECT list. A CASE statement is recommended as more portable though.

SELECT 
  c.status, 
  c.thumb, 
  j.id, 
  j.name, 
  j.username, 
  s.session_id, 
  a.time, 
  a.isactive, 
  a.country, 
  a.countrycode, 
  a.city,
  CASE WHEN a.time > DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN 1 ELSE 0 END 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)

Since you just need a boolean 1 or 0, the statement can also be written without CASE or IF as:

 ... 
 a.countrycode, 
 a.city,
 (a.time > DATE_SUB(NOW(), INTERVAL 1 MINUTE)) AS isonline
 ...

and it will have the same effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文