两个表中三列值的差异

时间:2017-08-06 21:07:53

标签: jquery sql sqlite

我有两个文件,每个文件有三列。

data1.txthttps://pastebin.com/ezVr6KPi

# P (Gpa)       G / F. unit  (a.u.)     T (K)
-4.4119598462100 -941.5590019525371 10.0000000000000
-4.4115664473800 -941.5589977004460 30.1000000000000
-4.4084485632200 -941.5589688036446 50.2000000000000
-4.3989188608700 -941.5588816009147 70.3000000000000
...

data2.txthttps://pastebin.com/veqBi8Er

# P (Gpa)       G / F. unit  (a.u.)     T (K)
7.5789845745700 -941.3858364185293 10.0000000000000
7.5936983738200 -941.3856461092793 30.1000000000000
7.6147899594200 -941.3853929461748 50.2000000000000
7.6413267974900 -941.3851062350891 70.3000000000000
...

我创建了2个表,并在tables.sql脚本中命名了列:

.headers on

CREATE TABLE C_I("P1" TEXT, "G1" TEXT, "T1" TEXT);
CREATE TABLE C_II("P2" TEXT, "G2" TEXT, "T2" TEXT);

我导入数据:

.separator " "

.import './data1.txt'  C_I
.import './data2.txt'  C_II

然后我从文件中删除本机头,只是为了更好地引用上面给出的列名:

select T1 from C_I LIMIT 3;
select P1 from C_I LIMIT 3;
select G1 from C_I LIMIT 3;

DELETE FROM C_I WHERE T1 == '(Gpa)';
DELETE FROM C_I WHERE P1 == '#';
DELETE FROM C_I WHERE G1 == 'P';

DELETE FROM C_II WHERE T2 == '(Gpa)';
DELETE FROM C_II WHERE P2 == '#';
DELETE FROM C_II WHERE G2 == 'P';

select * from C_I LIMIT 3;
select * from C_II LIMIT 3;

我正在尝试获取P1G1T1P2G2T2的值,其中: / p>

P1 = P2(在阈值内)AND T1 = T2(在阈值内)AND G1 = G2(在阈值内)。

为此我做:

更新

选项1:

SELECT * FROM   C_I l1,  C_II l2
WHERE
    ABS(l1.P1-l2.P2) < 0.005
    AND
    ABS(l1.T1-l2.T2) < 20.0
    AND
    ABS(l1.G1-l2.G2) < 0.005

这会打印以下错误:

Error: incomplete SQL: SELECT * FROM   C_I l1,  C_II l2 
WHERE 
    ABS(l1.P1-l2.P2) < 0.005
    AND
    ABS(l1.T1-l2.T2) < 20.0
    AND
    ABS(l1.G1-l2.G2) < 0.005

选项2:添加;

SELECT * FROM   C_I l1,  C_II l2;
WHERE
    ABS(l1.P1-l2.P2) < 0.005
    AND
    ABS(l1.T1-l2.T2) < 20.0
    AND
    ABS(l1.G1-l2.G2) < 0.005

在这种情况下,整个两个表通过终端显示, 显然这段代码没有效果。即使我在这三个语句中将阈值设置为0.0,它也会发生相同的情况(如果阈值为0.0,则不会产生任何输出)。

选项3:正如@Myonara建议的那样,在此处添加;

SELECT P, G, T FROM   C_I l1 ,  C_II  l2
WHERE
    ABS(l1.P1-l2.P2) < 0.005;
    AND
    ABS(l1.T1-l2.T2) < 20.0
    AND
    ABS(l1.G-l2.G2) < 0.005

产生此错误:

Error: incomplete SQL: AND
    ABS(l1.T1-l2.T2) < 20.0
    AND
    ABS(l1.G1-l2.G2) < 0.005

因此,我无法理解的东西不起作用

如果你能帮助我,我将不胜感激。

完整的脚本: tables.sql

我的运行方式为:sqlite3 < tables.sql

.headers on

CREATE TABLE C_I("P1" TEXT, "G1" TEXT, "T1" TEXT);
CREATE TABLE C_II("P2" TEXT, "G2" TEXT, "T2" TEXT);

.separator " "

.import './data1.txt'  C_I
.import './data2.txt'  C_II


select T1 from C_I LIMIT 3;
select P1 from C_I LIMIT 3;
select G1 from C_I LIMIT 3;


DELETE FROM C_I WHERE T1 == '(Gpa)';
DELETE FROM C_I WHERE P1 == '#';
DELETE FROM C_I WHERE G1 == 'P';

DELETE FROM C_II WHERE T2 == '(Gpa)';
DELETE FROM C_II WHERE P2 == '#';
DELETE FROM C_II WHERE G2 == 'P';


select * from C_I LIMIT 3;
select * from C_II LIMIT 3;


SELECT * FROM   C_I l1,  C_II l2;
WHERE
    ABS(l1.P1-l2.P2) < 0.005
    AND
    ABS(l1.T1-l2.T2) < 20.0
    AND
    ABS(l1.G1-l2.G2) < 0.005

2 个答案:

答案 0 :(得分:1)

试试这个,因为这只是SELECT的完整sql语法,你需要:

to_json

答案 1 :(得分:1)

根据您的意见,这是适合您的正确脚本:

SELECT l1.p1, l2.p2, l1.t1, l2.t2, l1.g1, l2.g2 FROM C_I l1, C_II l2 WHERE ABS(l1.P1-l2.P2) < 0.005 AND ABS(l1.T1-l2.T2) < 20.0 AND ABS(l1.G1-l2.G2) < 0.005;

我将解释这个脚本做了什么:  1. SELECT l1.p1, l2.p2, l1.t1, l2.t2, l1.g1, l2.g2 FROM C_I l1, C_II l2 - 这里只是根据需要创建笛卡尔积(也称为交叉连接)。这意味着它将提供一个包含680.000行(400行* 1700行= 680.000行)的表格,for each record from table C_I将提供1700 records from table C_II;  2.在WHERE子句中,它将进行计算,并且从脚本中,only one (1) match对所有your 3 conditions进行计算;  3.例如,仅对于此条件ABS(l1.P1-l2.P2) < 0.005您有91 matches,如果您应用第二个,则只有1个匹配;

希望它会对你有所帮助!