(JDBC)使用if语句和多个条件的Mysql查询

时间:2016-05-28 15:41:21

标签: java mysql sql

mysql-java语句的正确语法是什么,例如这个?

PreparedStatement st =connection.prepareStatement("SELECT Name, Value, Quantity FROM sales if (type=='Purchase' AND state=='confirmed') OR (type=='Sale' AND state=='not confirmed'))  ; 

2 个答案:

答案 0 :(得分:1)

SQL看起来像:

SELECT Name, Value, Quantity
FROM sales 
WHERE (type = 'Purchase' AND state = 'confirmed') OR
      (type = 'Sale' AND state = 'not confirmed');

答案 1 :(得分:0)

如果where clause, not an = clause, and the equality check operator in SQL is ==`,条件应为, not。所以:

SELECT Name, Value, Quantity 
FROM   sales 
WHERe  (type = 'Purchase' AND state = 'confirmed') OR 
       (type = 'Sale' AND state = 'not confirmed')