SQL命令按特定顺序

时间:2019-11-20 11:47:53

标签: sql mariadb

因此,根据我目前的结果集,我得到了:
current

,我想命令它获得类似以下内容: desired result

代码:

Statement stmt = con.createStatement();
String sql = "";
JOptionPane.showMessageDialog(null, "test");
sql = "update users set email='" + email1.getText() + "' WHERE fullname='" + user1 + "'";
stmt.executeUpdate(sql);
stmt.close();
con.close();

3 个答案:

答案 0 :(得分:0)

只需按klant1和klant2的字符串版本进行排序。

select count(*) as aantal_keer, 
 concat(klant1, '-', klant2) as pairname
from (
    select t.klant1, t.klant2, t.datum1, t.datum2,
    count(*) as aantal_overeenkomsten
    from (
        select a1.klant_idklant as klant1, 
        a2.klant_idklant as klant2, a1.datum as datum1, a2.datum as datum2
        from aankoop a1
        join aankoop a2 on a2.klant_idklant < a1.klant_idklant and a2.product_idproduct = a1.product_idproduct
    ) t
    group by datum1, datum2
    having aantal_overeenkomsten > 3
) t2
group by klant1, klant2
order by cast(klant1 as char), cast(klant2 as char);

答案 1 :(得分:0)

分别尝试castingintegerchar order

select count(*) as aantal_keer, concat(cast(t2.klant1 as char), '-', cast(t2.klant2 as char)) as pairname
from (
    select t.klant1, t.klant2, t.datum1, t.datum2, count(*) as aantal_overeenkomsten
    from (
        select a1.klant_idklant as klant1, a2.klant_idklant as klant2, a1.datum as datum1, a2.datum as datum2
        from aankoop a1, aankoop a2 where a2.product_idproduct = a1.product_idproduct and a2.klant_idklant < a1.klant_idklant
    ) t
    group by datum1, datum2
    having aantal_overeenkomsten > 3
) t2
group by pairname
order by cast(T1 AS char), cast(T2 AS char);

FIDDLE DEMO

输出将为

enter image description here

答案 2 :(得分:0)

如果您以两个数字开头:

SELECT concat(T1, '-', T2) FROM tbl
    order by  T1, T2;

如果您从配对的数字开始,请按如下所示拆分它们:

SELECT xx_yy FROM tbl
    ORDER BY SUBSTRING_INDEX(xx_yy, '-', 1),
             SUBSTRING_INDEX(xx_yy, '-', -1)