sql 查询时有空值返回0怎么写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 查询时有空值返回0怎么写相关的知识,希望对你有一定的参考价值。
参考技术A根据数据库的不同,采用如下不同的方法:
oracle
将空值返回0用如下语句:
select nvl(字段名,0) from 表名;
sqlserver
将空值返回0用如下语句:
方法一:select isnull(字段名,0) from 表名;
字符型:select isnull(mycol,'0') as newid from mytable
整型:select isnull(mycol,0) as newid from mytable
方法二:case ??end
case when columnName is null then 0 else columnName end
将空值返回0用如下语句:
select ifnull(字段名,0) from 表名;
拓展资料:
SQL SELECT 语句
SELECT 语句用于从表中选取数据。
结果被存储在一个结果表中(称为结果集)。
SQL SELECT 语法
SELECT 列名称 FROM 表名称。
SQL 查询返回空值
【中文标题】SQL 查询返回空值【英文标题】:SQL Query returns Null Values 【发布时间】:2022-01-01 09:52:51 【问题描述】:我有一个角色类型为营销人员、常务董事和总经理的用户实体。 当用户角色董事总经理登录时,我希望用户角色董事总经理仅查看分配给与董事总经理具有相同分支机构 ID 的用户类型营销人员的客户。
我在 客户存储库 中有一个自定义查询,它返回空结果。
@Query("SELECT customer from Customer customer join customer.marketer marketer "
+ "where marketer.branch = :director")
List<Customer> findByUserBranch(User director);
这是用户实体
@Entity
@JsonIgnoreProperties("hibernateLazyInitializer","handler")
public class User
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String firstName ;
private String lastName;
@Column(name="user_name", unique=true)
private String userName;
private String password;
private String Gender;
private String phoneNumber;
private String email;
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
@ManyToOne(targetEntity = Branch.class,
fetch = FetchType.LAZY )
@JoinColumn(name="branch_id")
private Branch branch;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createdDate;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private Set<UserRole> userRole = new HashSet<>();
@Enumerated(EnumType.STRING)
private UserStatus status;
@JsonBackReference
@OneToMany(mappedBy="marketer",cascade = CascadeType.ALL, targetEntity=Customer.class)
private List <Customer> customer;
这是控制器类
@GetMapping(value="branch/customers")
public List<Customer> getListByBranch()
Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
User loggedInUser = userRepo.findByUserName(authentication.getName()); return customerRepo.findByBranch(loggedInUser);
更新:
这是客户类
@Entity
@JsonIgnoreProperties("hibernateLazyInitializer","handler")
public class Customer implements Serializable
/**
*
*/
private static final long serialVersionUID = 8348682056500740593L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String userName;
private String password;
private String firstName ;
private String lastName;
private String gender;
private String Address;
private String maritalStatus;
private String category;
private String motherMaidenName;
private String idType;
private String idNumber;
private String phoneNumber;
private String email;
@Column(nullable = true, length = 64)
private String photos;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateOfBirth;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date registrationDate;
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
@ManyToOne(targetEntity = User.class,
fetch = FetchType.LAZY )
@JoinColumn(name="marketer_id")
private User marketer ;
@JsonBackReference
@OneToMany(mappedBy="customer_id",cascade = CascadeType.ALL, targetEntity=Investment.class)
private List<Investment> investment;
【问题讨论】:
【参考方案1】:我无法发表评论,所以我会要求也给我们 customer.class。
【讨论】:
我刚刚用客户类更新了问题 你试过数据库中的查询吗?如果是,请与我分享。 不,我没有在数据库中尝试。 试试这个 => List解决了。我将用户对象更改为分支对象。
@Query("SELECT customer from Customer customer join "
+ "customer.marketer marketer "
+ "where marketer.branch = :branch")
List<Customer> findByUserBranch(Branch branch);
然后重构控制器类
@GetMapping(value="branch/customers")
public List<Customer> getListByBranch(Principal principal)
User loggedInUser = userRepo.findByUserName(principal.getName());
Branch branchId = loggedInUser.getBranch();
return customerRepo.findByBranch(branchId);
【讨论】:
以上是关于sql 查询时有空值返回0怎么写的主要内容,如果未能解决你的问题,请参考以下文章