1.IFNULL语句判断一个name为空,则返回username

  1. SELECT (CASE WHEN IFNULL(trim(name),'') = '' THEN username ELSE name END) AS name from ta_user where id=209;
  2. //或者:
  3. SELECT (IF(IFNULL(trim(name),'') = '' , username , name )) AS name from ta_user where id=209;

2.Mybatis获取多条数据,根据id获取设备列表

  1. <select id="getBatchAdressById" resultType="string">
  2. select adress from ta_user where id in
  3. <foreach collection="list" open="(" close=")" separator="," item="item">#{item}</foreach>
  4. </select>

3.获取生日

  1. SELECT TIMESTAMPDIFF(YEAR, birthday, CURDATE()) from ta_user where id=#{id}

4.Mybatis获取前十条数据

  1. <select id="getUsersByDepartment_id" resultType="com.jfsystem.manage.dwpc.model.PushUser">
  2. select id,name,height,department_id,weight,birthday,TIMESTAMPDIFF(YEAR, birthday, CURDATE()) as age,phone,sex FROM ta_user where department_id in
  3. <foreach collection="list" open="(" close=")" separator="," item="item">#{item}</foreach>
  4. ORDER BY RAND() LIMIT 10;
  5. </select>

5.查询语句

  1. SELECT
  2. ta_user.id,
  3. ta_user.username,
  4. ta_user.`password`,
  5. ta_user.num,
  6. ta_user.`name`,
  7. ta_user.height,
  8. ta_user.weight,
  9. ta_user.phone,
  10. ta_user.birthday,
  11. ta_user.sex,
  12. ta_user.department_alias,
  13. ta_user.title,
  14. ta_user.photo_path,
  15. ta_user.mac,
  16. ta_user.department_id,
  17. ta_user.authority,
  18. ta_user.rfid_code,
  19. ta_user.rfid_qr,
  20. ta_user.flag,
  21. ta_user.adress,
  22. ta_user.watch_code,
  23. ta_user.roleid,
  24. ta_user.role,
  25. ta_user.version
  26. FROM
  27. ta_user
  28. WHERE
  29. ta_user.watch_code LIKE '%0-15%'
  30. GROUP BY
  31. ta_user.id
  32. HAVING
  33. ta_user.sex = 1
  34. ORDER BY
  35. ta_user.id ASC
  36. LIMIT 1,1;