SQL - 按值列在2列之间

时间:2016-01-06 12:04:57

标签: mysql sql

假设我们有下表:

Use GSON liabrary

Best way and very fast parsing of JSON is GSON liabrary 

dependacy for android studio compile 'com.google.code.gson:gson:2.3.1' OR you can download jar.

Make DTO names of all strings exactly same json of resonse.


Class YourDTO{
    String Title;
    String Value;

    take gettters & setters

}

Just include this lines in your code.
List<YourDTO> list = new ArrayList<YourDTO>();
String json = "[{"Title":"name","Value":"Sam"}, {"Title":"mobile","Value":"(606) 87-0238"}]";

JSONArray array=new JSONArray(json);
if (array.length() > 0) {
    Gson gson = new Gson();
    int i = 0;
    while (i < array.length()) {
        list.add(gson.fromJson(array.getJSONObject(i).toString(), YourDTO.class));
        i++;
    }    
} else {
    Toast.makeText(this, "No Objects", Toast.LENGTH_LONG).show();
}

我需要编写一个返回的查询:

CREATE TABLE a (
    id int primary key,
    from int, 
    to int
);

INSERT INTO a (id, from, to) VALUES (1, 1, 3);
INSERT INTO a (id, from, to) VALUES (2, 1, 2);
INSERT INTO a (id, from, to) VALUES (3, 2, 4);

第一列本身是+------------+---------+ | id | value | +------------+---------+ | 1 | 1 | | 1 | 2 | | 1 | 3 | | | | | 2 | 1 | | 2 | 2 | | | | | 3 | 2 | | 3 | 3 | | 3 | 4 | +------------+---------+ 。第二列应包含idfrom之间的所有值,值之间为第1步。

1 个答案:

答案 0 :(得分:2)

你需要一张数字表。如何生成这样的表取决于您使用的数据库。但是,对于小数字,您可以手动执行:

select t.id, (t.from + n.n) as value
from t join
     (select 0 as n union all select 1 union all select 2 union all select 3
     ) n
     on t.from + n.n <= t.to;