Mybatisplus Error querying database.Cause: java.sql.SQLException: Operand should contain 1 column(s)

Mybatis-plus Error querying database. Cause: java.sql.SQLException: Operand should contain 1 column(s)

/**
 * QueryWrapperSelect 实现子查询
 */
@Test
public void QueryWrapperSelectSon(){
    
    
    //查询id小于等于100的用户信息
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.inSql("id", "select id from user where id <= 100");
    queryWrapper.inSql("username", "select * from user where username like '%金%' AND is_deleted=0");
    List<User> list = userMapper.selectList(queryWrapper);
    list.forEach(System.out::println);
}

In the Mybatis-Plus actual combat project, when the user information is sub-queried through the QueryWrapper constructor, the following error is reported
Error querying database. Cause: java.sql.SQLException: Operand should contain 1 column(s)

org.springframework.jdbc.BadSqlGrammarException: 
### Error querying database.  Cause: java.sql.SQLException: Operand should contain 1 column(s)
### The error may exist in com/example/mybatisplus/mapper/UserMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT  id AS uid,username AS name,age,email,create_time,update_time,is_deleted  FROM user   WHERE  is_deleted=0  AND (id IN (select * from user where id <= 100))
### Cause: java.sql.SQLException: Operand should contain 1 column(s)
; bad SQL grammar []; nested exception is java.sql.SQLException: Operand should contain 1 column(s)

Reason for error:
select * from user where username like '%金%' AND is_deleted=0 cannot be *, but must be a field in the database table Solution:

insert image description here
Replace * with the specific field name in the table, for example: Username or id, etc., can solve the problem of error reporting.
insert image description here
insert image description here
As above, all user information can be queried

Guess you like

Origin blog.csdn.net/weixin_42694422/article/details/128121837