让列名称变得模糊

时间:2016-10-26 10:49:35

标签: mysql sql

我有查询,其中我正在获取专栏' month'暧昧的。我正在尝试以下查询。请检查和帮助我。

SELECT SC.name as sc,
       B.name as brand,
       COUNT(T.id) as tc,
       SUM(IF(is_category_present = 1, 1, 0)) as BASE
FROM outlet_categories OC 
     INNER JOIN transactions T ON OC.outlet_id = T.outlet_id 
     inner join outlets O on O.id = T.outlet_id 
     inner join brands B on T.brand_id = B.id 
     inner join sale_channels SC on SC.id =  O.sale_channel_id
WHERE T.month = month

3 个答案:

答案 0 :(得分:1)

限定所有列名称。目前尚不清楚正确的名称是什么,但这些需要表别名:

grep { $count{$_} <= 3 }

关于MySQL的三个注释:

  • 您的FilterDef filterDef = new FilterDef(); filterDef.setFilterName("springSecurityFilterChain"); filterDef.setFilterClass("org.springframework.web.filter.DelegatingFilterProxy"); container.addFilterDef(filterDef); FilterMap filterMapping = new FilterMap(); filterMapping.setFilterName("springSecurityFilterChain"); filterMapping.addURLPattern("/*"); container.addFilterMap(filterMapping); 不是必需的。
  • 我猜SELECT SC.name as sc, B.name as brand, COUNT(T.id) as tc, -------^ Looks like an aggregation query, but there is no `GROUP BY`. SUM(IF(is_category_present = 1, 1, 0)) as BASE --------------^ FROM outlet_categories OC INNER JOIN transactions T ON OC.outlet_id = T.outlet_id INNER JOIN outlets O ON O.id = T.outlet_id INNER JOIN brands B ON T.brand_id = B.id INNER JOIN sale_channels SC ON SC.id = O.sale_channel_id WHERE T.month = month; ----------------^ 是一个变量。如果是这样,请将其命名为其他内容。
  • 据推测,您需要一个IF()

更好的查询版本:

month

变量GROUP BY已重命名为SELECT SC.name as sc, B.name as brand, COUNT(T.id) as tc, SUM(??.is_category_present) as BASE FROM outlet_categories OC INNER JOIN transactions T ON OC.outlet_id = T.outlet_id INNER JOIN outlets O ON O.id = T.outlet_id INNER JOIN brands B ON T.brand_id = B.id INNER JOIN sale_channels SC ON SC.id = O.sale_channel_id WHERE T.month = v_month GROUP BY SC.name, B.name; ,因此不会与列名混淆。 MySQL会将month解释为v_month - 假设T.month = month仅在T.month = T.month表中。并且,我猜测month不是你的意图。

答案 1 :(得分:0)

如果在多个表中有一个名为month的列,则必须指定它来自的表,否则数据库不知道您引用的名为month的列。

答案 2 :(得分:0)

SELECT SC.name as sc,
       B.name as brand,
       COUNT(T.id) as tc,
       SUM(IF(is_category_present = 1, 1, 0)) as BASE /* <-- from with table this column? add alias*/
FROM outlet_categories OC 
     INNER JOIN transactions T ON OC.outlet_id = T.outlet_id 
     inner join outlets O on O.id = T.outlet_id 
     inner join brands B on T.brand_id = B.id 
     inner join sale_channels SC on SC.id =  O.sale_channel_id
WHERE T.month = month /* <-- from with table this column month? add alias*/

这个错误意味着你有几个表中的列test,例如,SQL并不知道从哪一栏开始 - 例如,来自table1或table2。