简体   繁体   中英

how to use if else condition in mysql query if attribute is a boolean datatype

 var sql = 'SELECT user_groups.*, groups.grp_display_name, user_groups.uug_active ' +' FROM user_groups, groups' +' WHERE user_groups.uug_is_deleted = 0 AND user_groups.group_id = groups.id  AND user_id ='+ userId + '\n';

在我的数据库中uug_active为1或0.我想使用if_else条件,以便如果其为1则它将在我的seachbox中搜索yes

Try the below query

SELECT user_groups.*, groups.grp_display_name, user_groups.uug_active 
FROM user_groups, groups 
WHERE user_groups.uug_is_deleted = 0 AND user_groups.group_id = groups.id AND user_id ='+ userId+'
AND IF("'+ active_mode+'" = "yes", (user_groups.uug_active = 1), (user_groups.uug_active = 0))

Please learn proper explicit join syntax and the use of table aliases:

SELECT ug.*, g.grp_display_name, g.uug_active 
FROM user_groups ug JOIN
     groups g
     ON ug.group_id = g.group_id
WHERE ug.uug_is_deleted = 0 AND user_id = $userId AND
      (uug_active <> 1 OR $searchbox = 'yes');

Note: you are embedding string data directly in the query. You should be using parameterized queries, but this answer only addresses the logic in the database, not the application logic for calling the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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