2017-10-07 149 views
-1

我在MYSQL查詢中收到錯誤。任何人都可以幫我解決我錯誤的地方,以及如何解決錯誤?如何解決這個MYSQL查詢錯誤?

SQL:

SELECT `sale`.*, 
     `customer`.`english_company_name`  AS `customer_name`, 
     `customer`.`english_address`   AS `english_address`, 
     `customer`.`english_address_two`  AS `english_address_two`, 
     `customer`.`english_address_three` AS `english_address_three`, 
     `area`.`NAME`       AS `customer_area`, 
     `city`.`NAME`       AS `customer_city`, 
     `state`.`NAME`      AS `customer_state`, 
     `customer`.`english_shipping_address` AS `english_shipping_address`, 
     `area`.`NAME`       AS `customer_area`, 
     `city`.`NAME`       AS `customer_city`, 
     `state`.`NAME`      AS `customer_state`, 
     `shipping_area`.`NAME`    AS `shipping_area`, 
     `shipping_city`.`NAME`    AS `shipping_city`, 
     `shipping_state`.`NAME`    AS `shipping_state`, 
     `transport`.`english_company_name` AS `transport_name`, ( 
     CASE 
       WHEN `sale`.`transport_branch` = 'default' THEN `transport`.`english_address` 
       ELSE `transport_brach`.`english_address` 
     END) AS transport_english_address, ( 
     CASE 
       WHEN `sale`.`transport_branch` = 'default' THEN `transport`.`english_address_two` 
       ELSE `transport_brach`.`english_address_two` 
     END) AS `transport_english_address_two`, ( 
     CASE 
       WHEN `sale`.`transport_branch` = 'default' THEN `transport`.`english_address_three` 
       ELSE `transport_brach`.`english_address_three` 
     END) AS transport_english_address_three, ( 
     CASE 
       WHEN `sale`.`transport_branch` = 'default' THEN `transport`.`area` 
       ELSE `transport_brach`.`area` 
     END) AS `transport_area`, ( 
     CASE 
       WHEN `sale`.`transport_branch` = 'default' THEN `transport`.`city` 
       ELSE `transport_brach`.`city` 
     END) AS transport_city, ( 
     CASE 
       WHEN `sale`.`transport_branch` = 'default' THEN `transport`.`state` 
       ELSE `transport_brach`.`state` 
     END) AS `transport_state FROM `sale` LEFT JOIN `customer` ON `customer`.`id` = `sale`.`client` LEFT JOIN `transport` ON `transport`.`id` = `sale`.`transport` LEFT JOIN `transport_branch` ON `transport_branch`.`id` = `sale`.`transport_branch` LEFT JOIN `area` ON `area`.`id` = `customer`.`area` LEFT JOIN `area` AS `shipping_area` ON `shipping_area`.`id` = `customer`.`shipping_area` LEFT JOIN `area` AS `transport_area` ON `transport_area`.`id` = `transport`.`area` LEFT JOIN `area` AS `transport_branch_area` ON `transport_branch_area`.`id` = `transport_branch`.`area` LEFT JOIN `city` ON `city`.`id` = `customer`.`city` LEFT JOIN `city` AS `shipping_city` ON `shipping_city`.`id` = `customer`.`shipping_city` LEFT JOIN `city` AS `transport_city` ON `transport_city`.`id` = `transport`.`city` LEFT JOIN `city` AS `transport_branch_city` ON `transport_city`.`id` = `transport_branch`.`city` LEFT JOIN `state` ON `state`.`id` = `customer`.`state` LEFT JOIN `state` AS `shipping_state` ON `shipping_state`.`id` = `customer`.`shipping_state` LEFT JOIN `state` AS `transport_state` ON `transport_state`.`id` = `transport`.`state` LEFT JOIN `state` AS `transport_branch_state` ON `transport_city`.`id` = `transport_branch`.`state` 

錯誤:

Ending quote ` was expected. (near "" at position 2806) 
An alias was previously found. (near "sale" at position 1612) 
An alias was previously found. (near "` LEFT JOIN `" at position 1616) 
An alias was previously found. (near "customer" at position 1629) 
An alias was previously found. (near "` ON `" at position 1637) 
An alias was previously found. (near "customer" at position 1643) 
An alias was previously found. (near "`.`" at position 1651) 
An alias was previously found. (near "ID" at position 1654) 
An alias was previously found. (near "` = `" at position 1656) 
An alias was previously found. (near "sale" at position 1661) 
An alias was previously found. (near "`.`" at position 1665) 
An alias was previously found. (near "` LEFT JOIN `" at position 1674) 
An alias was previously found. (near "transport" at position 1687) 
An alias was previously found. (near "` ON `" at position 1696) 
An alias was previously found. (near "transport" at position 1702) 
An alias was previously found. (near "`.`" at position 1711) 
An alias was previously found. (near "ID" at position 1714) 
An alias was previously found. (near "` = `" at position 1716) 
An alias was previously found. (near "sale" at position 1721) 
An alias was previously found. (near "`.`" at position 1725) 
An alias was previously found. (near "Transport" at position 1728) 
An alias was previously found. (near "` LEFT JOIN `" at position 1737) 
An alias was previously found. (near "transport_branch" at position 1750) 
An alias was previously found. (near "` ON `" at position 1766) 
An alias was previously found. (near "transport_branch" at position 1772) 
An alias was previously found. (near "`.`" at position 1788) 
An alias was previously found. (near "ID" at position 1791) 
An alias was previously found. (near "` = `" at position 1793) 
An alias was previously found. (near "sale" at position 1798) 
An alias was previously found. (near "`.`" at position 1802) 
An alias was previously found. (near "Transport_branch" at position 1805) 
An alias was previously found. (near "` LEFT JOIN `" at position 1821) 
An alias was previously found. (near "` ON `" at position 1838) 
An alias was previously found. (near "`.`" at position 1848) 
An alias was previously found. (near "ID" at position 1851) 
An alias was previously found. (near "` = `" at position 1853) 
An alias was previously found. (near "customer" at position 1858) 
An alias was previously found. (near "`.`" at position 1866) 
An alias was previously found. (near "` LEFT JOIN `" at position 1873) 
An alias was previously found. (near "` as `" at position 1890) 
An alias was previously found. (near "shipping_area" at position 1896) 
An alias was previously found. (near "` ON `" at position 1909) 
An alias was previously found. (near "shipping_area" at position 1915) 
An alias was previously found. (near "`.`" at position 1928) 
An alias was previously found. (near "ID" at position 1931) 
An alias was previously found. (near "` = `" at position 1933) 
An alias was previously found. (near "customer" at position 1938) 
An alias was previously found. (near "`.`" at position 1946) 
An alias was previously found. (near "Shipping_area" at position 1949) 
An alias was previously found. (near "` LEFT JOIN `" at position 1962) 
An alias was previously found. (near "` as `" at position 1979) 
An alias was previously found. (near "transport_area" at position 1985) 
An alias was previously found. (near "` ON `" at position 1999) 
An alias was previously found. (near "transport_area" at position 2005) 
An alias was previously found. (near "`.`" at position 2019) 
An alias was previously found. (near "ID" at position 2022) 
An alias was previously found. (near "` = `" at position 2024) 
An alias was previously found. (near "transport" at position 2029) 
An alias was previously found. (near "`.`" at position 2038) 
An alias was previously found. (near "` LEFT JOIN `" at position 2045) 
An alias was previously found. (near "` as `" at position 2062) 
An alias was previously found. (near "transport_branch_area" at position 2068) 
An alias was previously found. (near "` ON `" at position 2089) 
An alias was previously found. (near "transport_branch_area" at position 2095) 
An alias was previously found. (near "`.`" at position 2116) 
An alias was previously found. (near "ID" at position 2119) 
An alias was previously found. (near "` = `" at position 2121) 
An alias was previously found. (near "transport_branch" at position 2126) 
An alias was previously found. (near "`.`" at position 2142) 
An alias was previously found. (near "` LEFT JOIN `" at position 2149) 
An alias was previously found. (near "city" at position 2162) 
An alias was previously found. (near "` ON `" at position 2166) 
An alias was previously found. (near "city" at position 2172) 
An alias was previously found. (near "`.`" at position 2176) 
An alias was previously found. (near "ID" at position 2179) 
An alias was previously found. (near "` = `" at position 2181) 
An alias was previously found. (near "customer" at position 2186) 
An alias was previously found. (near "`.`" at position 2194) 
An alias was previously found. (near "City" at position 2197) 
An alias was previously found. (near "` LEFT JOIN `" at position 2201) 
An alias was previously found. (near "city" at position 2214) 
An alias was previously found. (near "` as `" at position 2218) 
An alias was previously found. (near "shipping_city" at position 2224) 
An alias was previously found. (near "` ON `" at position 2237) 
An alias was previously found. (near "shipping_city" at position 2243) 
An alias was previously found. (near "`.`" at position 2256) 
An alias was previously found. (near "ID" at position 2259) 
An alias was previously found. (near "` = `" at position 2261) 
An alias was previously found. (near "customer" at position 2266) 
An alias was previously found. (near "`.`" at position 2274) 
An alias was previously found. (near "Shipping_city" at position 2277) 
An alias was previously found. (near "` LEFT JOIN `" at position 2290) 
An alias was previously found. (near "city" at position 2303) 
An alias was previously found. (near "` as `" at position 2307) 
An alias was previously found. (near "transport_city" at position 2313) 
An alias was previously found. (near "` ON `" at position 2327) 
An alias was previously found. (near "transport_city" at position 2333) 
An alias was previously found. (near "`.`" at position 2347) 
An alias was previously found. (near "ID" at position 2350) 
An alias was previously found. (near "` = `" at position 2352) 
An alias was previously found. (near "transport" at position 2357) 
An alias was previously found. (near "`.`" at position 2366) 
An alias was previously found. (near "City" at position 2369) 
An alias was previously found. (near "` LEFT JOIN `" at position 2373) 
An alias was previously found. (near "city" at position 2386) 
An alias was previously found. (near "` as `" at position 2390) 
An alias was previously found. (near "transport_branch_city" at position 2396) 
An alias was previously found. (near "` ON `" at position 2417) 
An alias was previously found. (near "transport_city" at position 2423) 
An alias was previously found. (near "`.`" at position 2437) 
An alias was previously found. (near "ID" at position 2440) 
An alias was previously found. (near "` = `" at position 2442) 
An alias was previously found. (near "transport_branch" at position 2447) 
An alias was previously found. (near "`.`" at position 2463) 
An alias was previously found. (near "City" at position 2466) 
An alias was previously found. (near "` LEFT JOIN `" at position 2470) 
An alias was previously found. (near "state" at position 2483) 
An alias was previously found. (near "` ON `" at position 2488) 
An alias was previously found. (near "state" at position 2494) 
An alias was previously found. (near "`.`" at position 2499) 
An alias was previously found. (near "ID" at position 2502) 
An alias was previously found. (near "` = `" at position 2504) 
An alias was previously found. (near "customer" at position 2509) 
An alias was previously found. (near "`.`" at position 2517) 
An alias was previously found. (near "State" at position 2520) 
An alias was previously found. (near "` LEFT JOIN `" at position 2525) 
An alias was previously found. (near "state" at position 2538) 
An alias was previously found. (near "` as `" at position 2543) 
An alias was previously found. (near "shipping_state" at position 2549) 
An alias was previously found. (near "` ON `" at position 2563) 
An alias was previously found. (near "shipping_state" at position 2569) 
An alias was previously found. (near "`.`" at position 2583) 
An alias was previously found. (near "ID" at position 2586) 
An alias was previously found. (near "` = `" at position 2588) 
An alias was previously found. (near "customer" at position 2593) 
An alias was previously found. (near "`.`" at position 2601) 
An alias was previously found. (near "Shipping_state" at position 2604) 
An alias was previously found. (near "` LEFT JOIN `" at position 2618) 
An alias was previously found. (near "state" at position 2631) 
An alias was previously found. (near "` as `" at position 2636) 
An alias was previously found. (near "transport_state" at position 2642) 
An alias was previously found. (near "` ON `" at position 2657) 
An alias was previously found. (near "transport_state" at position 2663) 
An alias was previously found. (near "`.`" at position 2678) 
An alias was previously found. (near "ID" at position 2681) 
An alias was previously found. (near "` = `" at position 2683) 
An alias was previously found. (near "transport" at position 2688) 
An alias was previously found. (near "`.`" at position 2697) 
An alias was previously found. (near "State" at position 2700) 
An alias was previously found. (near "` LEFT JOIN `" at position 2705) 
An alias was previously found. (near "state" at position 2718) 
An alias was previously found. (near "` as `" at position 2723) 
An alias was previously found. (near "transport_branch_state" at position 2729) 
An alias was previously found. (near "` ON `" at position 2751) 
An alias was previously found. (near "transport_city" at position 2757) 
An alias was previously found. (near "`.`" at position 2771) 
An alias was previously found. (near "ID" at position 2774) 
An alias was previously found. (near "` = `" at position 2776) 
An alias was previously found. (near "transport_branch" at position 2781) 
An alias was previously found. (near "`.`" at position 2797) 
An alias was previously found. (near "State" at position 2800) 
An alias was previously found. (near "`" at position 2805) 

我在想,我正在使用AS命令case語句裏面的人可以請幫我在哪裏,我錯了?以及如何解決錯誤?對我弱的英語感到抱歉。請嘗試編輯並改進我的問題。

+3

請加錯誤,你的 –

+0

那裏似乎也有丟失的代碼 –

+0

@ Fred-ii-沒有代碼是不會丟失的。 –

回答

0

你缺少一個END

 (CASE WHEN `sale`.`transport_branch` = 'default' 
      THEN `transport`.`english_address` 
     END) AS transport_english_address, 
    `transport`.`english_address_two` AS `transport_english_address_two`, 

如果你想CASE適用於比出結果集列越多,你需要重複它:

 (CASE WHEN `sale`.`transport_branch` = 'default' 
      THEN `transport`.`english_address` 
     END) AS transport_english_address, 
     (CASE WHEN `sale`.`transport_branch` = 'default' 
      THEN `transport`.`english_address_two` 
     END) AS `transport_english_address_two`, 

我也建議您將學習如何使用表別名,以便查詢更易於編寫和閱讀。

+0

謝謝,夥計。我會按照你的建議。 –

+0

先生我更新了我的查詢,你可以請再次看看查詢。 –