表中每个数据值的行

时间:2017-10-18 11:26:16

标签: mysql sql

我有3张桌子

  • 啤酒花(ID,啤酒专线1至12)
  • pludata(customerID,pluID,pluName,pluDepartment,pluPrice)
  • masterdata(masterNo,customerID)

    SELECT 
    MasterID,beerline1,beerline2,beerline3,beerline4,
    beerline5,beerline6,beerline7,beerline8,beerline9,
    beerline10,beerline11,beerline12 
    FROM `masterdata` 
    JOIN beerlines ON masterdata.masterNo = beerlines.MasterID 
    WHERE masterdata.customerNo = '23'
    

这样我就可以将所有值放在1行,这样就很难输出到我的数据表中。

我希望能够得到这样的东西

  • MasterID:beerline1:pluName:pluDepartment
  • MasterID:beerline2:pluName:pluDepartment
  • MasterID:beerline3:pluName:pluDepartment
  • MasterID:beerline4:pluName:pluDepartment

我如何才能为我的beerlines表中的每个值获取一行?

3 个答案:

答案 0 :(得分:2)

您可以使用union all

SELECT . . .
FROM masterdata m JOIN
     pludata pd
     ON pd.CustomerId = md.CustomerId JOIN
     ((SELECT bl.MasterID, 'beerline1' as which, beerline1 as beerline
       FROM beerlines bl
      ) UNION ALL
      (SELECT bl.MasterID, 'beerline2' as which, beerline2 as beerline
       FROM beerlines bl
      ) UNION ALL
      . . . 
     ) bl 
     ON bl.MasterId = m.masterNo
WHERE m.customerNo = 23;

但是,您应该真正修复数据模型。通过数字区分多个列通常不是一个好主意。相反,你应该有一个联结表,每个主ID一行,每个啤酒行一行。

答案 1 :(得分:1)

您需要将列拆分为行,但mysql没有像sql server这样的非活动表操作符。但您可以像这样使用UNION ALL

SELECT b.Id, b.beerline, pludata.pluDepartment
FROM masterdata
INNER JOIN
(

    SELECT ID, beerline1 AS beerline
    FROM beerlines 
    UNION ALL
    SELECT ID, beerline2 AS beerline
    FROM beerlines 
    ...
    ...
    UNION ALL
    SELECT ID, beerline12 AS beerline
    FROM beerlines 

) AS b ON masterdata.masterNo = b.MasterID 
JOIN pludata ON masterdata.customerID = pludata.CustomerID
WHERE masterdata.customerNo = '23'

答案 2 :(得分:0)

根据David关于使我的数据表更简单的建议,解决方案更容易理解。

public static class Splitter implements FlatMapFunction<String, 
    Tuple3<String, String, List<String>>{
    private static final long serialVersionUID = 1L;

    @Override
    public void flatMap(String value, Collector<Tuple3<String, String, 
        List<String>>out) throws Exception {
        String[] vals = value.split(":");

        if(vals.length 1 && Integer.parseInt(vals[1]) % 2 == 0){
            out.collect(new Tuple3<String, String, List<String>>
             ("test","even", Arrays.asList(vals[1])));
        }else{
            out.collect(new Tuple3<String, String, List<String>>
            ("test","odd", Arrays.asList(vals[1])));
        }
    }
}


    DataStream<Map<String, List<String>>streamValue = 
    kafkaStream.flatMap(new Splitter()).keyBy(0)
    .window(TumblingEventTimeWindows.of(Time.milliseconds(3000))).
    trigger(CustomizedCountTrigger.of(5L))//.trigger(CountTrigger.of(2))
    .fold(new HashMap<String, List<String>>(), new 
    FoldFunction<Tuple3<String, String, List<String>>, Map<String, 
    List<String>>>() {
        private static final long serialVersionUID = 1L;

        @Override
        public Map<String, List<String>fold(Map<String, 
        List<String>accumulator,
        Tuple3<String, String, List<String>value) throws 
        Exception {
            if(accumulator.get(value.f1) != null){
                List<Stringlist = new ArrayList<>();
                list.addAll(accumulator.get(value.f1));
                list.addAll(value.f2);
                accumulator.put(value.f1, list);
            }else{
                accumulator.put(value.f1, value.f2);
            }
            return accumulator;
        }
    });

    streamValue.print();
    env.execute("window test");
}


public class CustomizedCountTrigger<W extends Windowextends 
Trigger<Object, W{

    private static final long serialVersionUID = 1L;
    private final long maxCount;

    private final ReducingStateDescriptor<LongstateDesc =
    new ReducingStateDescriptor<>("count", new Sum(), 
    LongSerializer.INSTANCE);

    private CustomizedCountTrigger(long maxCount) {
        this.maxCount = maxCount;
    }

    @Override
    public TriggerResult onElement(Object element, long timestamp, W window,
    TriggerContext ctx) throws Exception {
        ReducingState<Longcount = ctx.getPartitionedState(stateDesc);
        count.add(1L);
        if (count.get() >= maxCount) {
            count.clear();
            return TriggerResult.FIRE_AND_PURGE;
        }
        return TriggerResult.CONTINUE;
    }

    @Override
    public TriggerResult onProcessingTime(long time, W window,

    org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext

    ctx) throws Exception {
        return TriggerResult.CONTINUE;
    }

    @Override
    public TriggerResult onEventTime(long time, W window,

    org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext

    ctx) throws Exception {
        return TriggerResult.CONTINUE;
    }

    @Override
    public void clear(W window, 
    org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext

    ctx)
    throws Exception {
        ctx.getPartitionedState(stateDesc).clear();
    }

    @Override
    public String toString() {
        return "CountTrigger(" +  maxCount + ")";
    }

    public static <W extends WindowCustomizedCountTrigger<Wof(long 
    maxCount) {
        return new CustomizedCountTrigger<>(maxCount);
    }

    private static class Sum implements ReduceFunction<Long{
        private static final long serialVersionUID = 1L;

        @Override
        public Long reduce(Long value1, Long value2) throws Exception {
            return value1 + value2;
        }

    }
}

这给了我想要的东西,谢谢你能在几秒钟内如此快速地完成任务!