如何解决这个MYSQL查询错误?

时间:2017-10-07 13:24:33

标签: mysql sql

我在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命令,任何人都可以在我出错的地方帮助我吗?以及如何解决错误?抱歉我的英语不好。请尝试编辑并改进我的问题。

1 个答案:

答案 0 :(得分: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`,

我还建议你学习如何使用表别名,这样查询就更容易编写和阅读。