MySQL查询与多个AND条件

时间:2018-10-13 11:39:03

标签: mysql

我不知道为什么我的SQL查询不起作用

TABLE用户

public class ComponentImpl {

public List<Employee> catchAllEmployess () {

    List<Manager> managers = DbOperation.collectManagers(2);
    List<Employee> employees = new ArrayList<>();

    for (Manager manager : managers) {
        Employee employee = new Employee();
        employee.setName(manager.getName());
        employee.setDesignation(manager.getDesignation());
        employee.setActive(manager.getActive());
        employees.add(employee);
    }
    return employees;
}

TABLE msg

userid   firstname    lastname
1         JOHN         DEO
2         JANE         DEO

我的查询:

msg_id  msg_from   msg_to     received  age  city  country    product  weight  price     timestamp            
1       userid(1)  userid(2)  null      26   any   any         rice     25     $100      2018-10-04 14:37:12

我看不到任何问题,但无法获得结果

2 个答案:

答案 0 :(得分:0)

假设userid / msg_to / msg_from是整数,而msg表中的price是一个数字,我将WHERE子句更改为

WHERE M.msg_to = 1 AND product = 'rice' AND price >= 100.0

如果它们是字符串,那么您可以

WHERE M.msg_to = '1' AND product = 'rice' AND CAST(price AS DECIMAL(10,2)) >= 100.0

假设您放下美元符号。

答案 1 :(得分:0)

如果要获取结果,请执行以下查询

SELECT SND.userid, SND.firstname, SND.lastname, M.age, M.city, M.country
FROM msg as M
JOIN users as SND 
ON SND.userid = M.msg_from
WHERE M.msg_to = 'userid 2' AND product = 'rice' AND price >='$100'

在代码结果中未显示,因为根据您的表视图userid与msg_to列不匹配