MariaDB子查询

时间:2018-08-25 07:43:42

标签: mysql laravel mariadb

美好的一天!

我有此SQL,但无法在托管中工作。在局域网上,一切都很好。我找不到问题所在。 Laravel项目SQL:

$data = DB::select('
            SELECT 
                DATE(o.created_at) as day,
                count(id) as count_all,
                count((SELECT id WHERE status=3)) as success,
                count((SELECT id WHERE status=5)) as return_order,
                count((SELECT id WHERE status=0 or status=4 or status=8)) as call_order,

                count((SELECT id WHERE status=7 or status=6 or status=1)) as otkaz,
                count((SELECT id WHERE status=2)) as nado_dostavit,

                SUM((SELECT offer_price)) as all_price,
                SUM((SELECT offer_price WHERE status=3)) as success_price,
                SUM((SELECT offer_price WHERE status=5)) as return_order_price,
                SUM((SELECT offer_price WHERE status=0 or status=4 or status=8)) as call_order_price,
                SUM((SELECT offer_price WHERE status=7 or status=6 or status=1)) as otkaz_price,
                SUM((SELECT offer_price WHERE status=2 )) as nado_dostavit_price,
                (SELECT sum(s.visitors) FROM statistics as s WHERE s.offer_id='.$user_id.') as visitors
            FROM 
                orders as o
            WHERE 
                  offer_id='.$user_id.'
                  AND  created_at between "'.$mother_later.'" AND "'.$today.'"
            GROUP BY day
            ORDER BY day desc
            limit 30
    ');
  

错误:

     

(2/2)QueryException

     

SQLSTATE [42000]:语法错误或访问冲突:1064您有一个       您的SQL语法错误;检查与您的手册相对应的手册       MariaDB服务器版本可在'WHERE附近使用正确的语法       status = 3))成功,

1 个答案:

答案 0 :(得分:0)

您的子查询都没有FROM子句,这就是为什么您遇到语法错误的原因。但是,您实际上并不需要这些位置的子查询。对于COUNT,您应将其替换为例如

SUM(CASE WHEN status=3 THEN 1 ELSE 0 END)

,对于SUM,您应该使用例如

SUM(CASE WHEN status=3 THEN offer_price ELSE 0 END)

因此,您查询的总数应该是:

$data = DB::select('
        SELECT 
            DATE(o.created_at) as day,
            count(id) as count_all,
            SUM(CASE WHEN status=3 THEN 1 ELSE 0 END) as success,
            SUM(CASE WHEN status=5 THEN 1 ELSE 0 END) as return_order,
            SUM(CASE WHEN status=0 OR status=4 OR status=8 THEN 1 ELSE 0 END) as call_order,
            SUM(CASE WHEN status=7 OR status=6 OR status=1 THEN 1 ELSE 0 END) as otkaz,
            SUM(CASE WHEN status=2 THEN 1 ELSE 0 END) as nado_dostavit,

            SUM(offer_price) as all_price,
            SUM(CASE WHEN status=3 THEN offer_price ELSE 0 END) as success_price,
            SUM(CASE WHEN status=5 THEN offer_price ELSE 0 END) as return_order_price,
            SUM(CASE WHEN status=0 OR status=4 OR status=8 THEN offer_price ELSE 0 END)  as call_order_price,
            SUM(CASE WHEN status=7 OR status=6 OR status=1 THEN offer_price ELSE 0 END) as otkaz_price,
            SUM(CASE WHEN status=2 THEN offer_price ELSE 0 END) as nado_dostavit_price,
            (SELECT sum(s.visitors) FROM statistics as s WHERE s.offer_id='.$user_id.') as visitors
        FROM 
            orders as o
        WHERE 
              offer_id='.$user_id.'
              AND  created_at between "'.$mother_later.'" AND "'.$today.'"
        GROUP BY day
        ORDER BY day desc
        limit 30
');