使用 IF 语句进行查询
我有一个关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将其添加到
SELECT
列表中即可。CASE
语句 是不过建议更便携。由于您只需要一个布尔值 1 或 0,因此该语句也可以不使用
CASE
或IF
编写为:,也将具有相同的效果。
Just add it into the
SELECT
list. ACASE
statement is recommended as more portable though.Since you just need a boolean 1 or 0, the statement can also be written without
CASE
orIF
as:and it will have the same effect.