联盟没有工作空结果

时间:2016-11-24 16:48:55

标签: mysql sql sqlite

我尝试使用内连接和联合不工作

sql="SELECT * FROM " +
        "(SELECT order_id, order_date, order_status, order_value, order_sapId, order_from, order_clientAcct, order_error, order_visitId, order_cancelled, distributor_name " +
        " FROM `orders` " +
        " JOIN visits  on  visit_id = order_visitId  " +
        " JOIN Name ON user_id = visit_userId  " +
        " JOIN distributor ON distributor_id = visit_outlet_id  " +
        " WHERE order_from = 'CSFA' AND user_id = '"+ App.getUserData("user_id")+"' " +
        ")" +
        " JOIN " +
        "(SELECT   order_id, order_date, order_status, order_value, order_sapId, order_from, order_clientAcct, order_error, order_visitId, order_cancelled,  distributor_name " +
        " FROM `orders` " +
        " JOIN distributor ON order_clientAcct = distributor_accountName " +
        " JOIN user ON salesEmpNo = user_sapId " +
        " WHERE order_from = 'u' AND user_id = '"+ App.getUserData("user_id")+"' " +
        ") " +
        "" +
        "ORDER BY order_id DESC LIMIT 100" +
        "";

我需要一种方法来加入这两个结果 1.尝试内连接 2.tried union

  

E / SQLiteLog:(1)near"(":语法错误   11-24 19:53:01.017 13531-13531 / E / AndroidRuntime:致命异常:主要                                                                         过程:,PID:13531                                                                         android.database.sqlite.SQLiteException:near"(&#34 ;:语法错误(代码1):,同时编译:SELECT * FROM(SELECT order_id,order_date,order_status,order_value,order_sapId,order_from,order_clientAcct,order_error ,order_visitId,order_cancelled,distributor_name FROM orders JOIN访问visit_id = order_visitId JOIN名称ON user_id = visit_userId JOIN分配器ON distributor_id = visit_outlet_id WHERE order_from =' CSFA' AND user_id =' 75&# 39;)UNION(SELECT order_id,order_date,order_status,order_value,order_sapId,order_from,order_clientAcct,order_error,order_visitId,order_cancelled,distributor_name FROM orders JOIN distributor ON order_clientAcct = distributor_accountName JOIN user ON salesEmpNo = user_sapId WHERE order_from =&# 39; u' AND user_id =' 75')ORDER BY order_id DESC LIMIT 100

这是错误日志

1 个答案:

答案 0 :(得分:0)

经过几天的研究后,我们终于得到了答案。使用LEFT JOIN和OR来从表中获取数据,只有它们匹配。


sql="" +
        "SELECT DISTINCT order_id, order_date, order_status, order_value, order_sapId, order_from, order_clientAcct, order_error, order_visitId, order_cancelled, distributor_name " +
        " FROM `orders` " +
        " LEFT JOIN visits,user,distributor  ON  (visit_id = order_visitId AND user_id = visit_userId AND  distributor_id = visit_outlet_id AND order_from = 'CSFA' AND user_id = '"+ App.getUserData("user_id")+"') OR " +
        " " +
        " ( order_clientAcct = distributor_accountName AND salesEmpNo = user_sapId AND order_from = 'SAP' AND user_id = '"+ App.getUserData("user_id")+"' )" +
        " " +
        "ORDER BY order_id DESC LIMIT 100" +
        "";