MySQL从最大值中选择最小值

时间:2017-07-14 14:41:50

标签: mysql

我想选择从所有表中返回的最小值的最小值。这是我的sql命令,用于搜索所有最大值:

SELECT MAX(value) FROM temperatureTable 
UNION 
SELECT MAX(value) FROM resistanceTable 
UNION 
SELECT MAX(value) FROM pressureTable;

结果如下:

    {
        "MAX(value)": "8.113130E-1"
    },
    {
        "MAX(value)": "6.445700E+0"
    },
    {
        "MAX(value)": "6.526210E-8"
    }

如何使用sql命令选择最小值6.445700E+0

另外,如何从返回的Rawdatapackage中访问MAX(值)/ MIN(值)?

result[0].Max(value) // will generate an error because of the '()'.

2 个答案:

答案 0 :(得分:2)

使用子查询技术:

public class ObservableValueProperty<T> extends SimpleObjectProperty<T> {

    private InvalidationListener listener = null;

    private final Callback<T, Observable[]> extractor;

    public ObservableValueProperty() {
    this(null);
    }

    public ObservableValueProperty(final Callback<T, Observable[]> extractor) {
    this.extractor = extractor;
    }

    @Override
    protected void fireValueChangedEvent() {
    super.fireValueChangedEvent();
    }

    @Override
    public void setValue(final T v) {
    if (extractor != null) {
        final T oldValue = super.get();
        if (oldValue != null) {
        for (final Observable o : extractor.call(oldValue)) {
            o.removeListener(listener);
        }
        }
        listener = o -> fireValueChangedEvent();
        for (final Observable o : extractor.call(v)) {
        o.addListener(listener);
        }
    }
    super.setValue(v);
    }
}

public class ObservableValuePropertyTest4 implements ChangeListener<Object> {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    static class NestedBean {

    StringProperty nestedProperty = new SimpleStringProperty("hans");

    public static <T extends NestedBean> Callback<T, Observable[]> extractor() {

        return (final T o) -> new Observable[] { o.nestedProperty };
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof NestedBean) {
        System.err.println(this.nestedProperty.get() + " " + ((NestedBean) obj).nestedProperty.get());
        return Objects.equal(this.nestedProperty.get(), ((NestedBean) obj).nestedProperty.get());
        }
        return false;
    }

    }

    private ObservableValueProperty<NestedBean> p;

    private NestedBean nestedBean;

    private String newNestedValue = null;

    @Test
    public void test01() {
    p = new ObservableValueProperty<>(NestedBean.extractor());
    nestedBean = new NestedBean();
    p.setValue(nestedBean);
    p.addListener(this);
    nestedBean.nestedProperty.set("peter");
    assertEquals("peter", newNestedValue);
    }

    @Override
    public void changed(final ObservableValue<? extends Object> observable, final Object oldValue,
        final Object newValue) {
    System.err.println("Changed");
    newNestedValue = nestedBean.nestedProperty.get();

    }

}

答案 1 :(得分:1)

您可以使用

SELECT MAX(value) as maxValue FROM temperatureTable 
UNION 
SELECT MAX(value) FROM resistanceTable 
UNION 
SELECT MAX(value) FROM pressureTable;

并访问结果如:

result[0].maxValue  
// using "as someAlias" names that field result "someAlias"

如果您想要最小的最大值,您可以:

Select min(maxValue) as minValue FROM (
    SELECT MAX(value) as maxValue FROM temperatureTable 
    UNION 
    SELECT MAX(value) FROM resistanceTable 
    UNION 
    SELECT MAX(value) FROM pressureTable;
) x

我不确定这个是否合理,因为这些表包含不同的类型的测量值。