说我有表1:
Information:Gradle tasks [:app:generateDebugSources,:app:generateDebugAndroidTestSources]:app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72300Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42300Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl :app:compileDebugRenderscript :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources :app:mergeDebugResources :app:processDebugManifest UP-TO-DATE :app:processDebugResources C:\Users\hp\AndroidStudioProjects\MyApplication\app\src\main\res\layout\activity_main.xml Error:(7) Error parsing XML: not well-formed (invalid token) Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\hp\AppData\Local\Android\sdk\build-tools\23.0.0\aapt.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
Information:Total time: 37.338 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console`
console :
Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources] Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72300Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42300Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources
AGPBI: {"kind":"error","text":"Error parsing XML: not well-formed (invalid token)","sources":[{"file":"C:\\Users\\hp\\AndroidStudioProjects\\MyApplication\\app\\src\\main\\res\\layout\\activity_main.xml","position":{"startLine":6}}],"original":""}
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:processDebugResources'. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\hp\AppData\Local\Android\sdk\build-tools\23.0.0\aapt.exe'' finished with non-zero exit value 1
BUILD FAILED
Total time: 37.338 secs`
build.gradle file :
com.android.tools.build:gradle:1.3.0
我有表2:
stuff | sender_id | recipient_id
我想回来:
id | name
或
stuff | sender_name | recipient_name
现在我正在寻找类似的东西:
stuff | sender_id | sender_name | recipient_id | recipient_name
但是这会返回重复的东西,因为一行填充了两个条件。
有没有办法在1个查询中获取我想要的信息?
答案 0 :(得分:1)
一种方法是使用内部选择,如下所示:
SELECT stuff, sender_id
, (SELECT name FROM table2 WHERE id = sender_id) AS sender_name
, recipient_id
, (SELECT name FROM table2 WHERE id = recipient_id) AS recipient_name
FROM table1;
答案 1 :(得分:1)
一个对table2的引用的解决方案:
select t1.stuff
, max(case when t1.sender_id = t2.id then t2.name end) as sender_name
, max(case when t1.recipient_id = t2.id then t2.name end) as recipint_name
from t1
join t2
on t2.id in (t1.sender_id, t1.recipient_id)
group by t1.stuff;
这有点乱,但有些情况会很方便。
我创建了表并用10000行填充它们(db2 express-c,10.5 fixpak 1):
db2 "create table t1 (stuff int not null primary key, sender_id int not null, recipient_id int not null)"
db2 "create table t2 (id int not null primary key, name varchar(10) not null);
db2 "insert into t1 with t (n) as ( values 0 union all select n+1 from t where n+1 < 10000) select n, 2*n, 2*n+1 from t"
db2 "insert into t2 with t (n) as ( values 0 union all select n+1 from t where n+1 < 10000) select 2*n, 'C' || rtrim(cast(2*n as char(10))) from t"
db2 runstats on table t1 with distribution and sampled detailed indexes all
db2 runstats on table t2 with distribution and sampled detailed indexes all
并检查了不同查询的计划。我添加了一个where子句
两个子选择:
db2 "explain plan for SELECT stuff , (SELECT name FROM t2 WHERE id = sender_id) AS sender_name, (SELECT name FROM t2 WHERE id = recipient_id) AS recipient_name FROM t1 where t1.id between 500 and 600"
db2exfmt -d sample -g -1 -o sub.exfmt
两个加入:
db2 "explain plan for SELECT t1.stuff, tA.name as sender_name, tB.name as recipient_name from t1 join t2 as tA on t1.sender_id = tA.id join t2 as tB on t1.sender_id = tB.id where t1.stuff between 500 and 600"
db2exfmt -d sample -g -1 -o dualjoin.exfmt
最后是带有聚合和案例的变体:
db2 "explain plan for SELECT t1.stuff, max(case when t1.sender_id = t2.id then t2.name end) as sender_name, max(case when t1.recipient_id = t2.id then t2.name end) as recipint_name from t1 join t2 on t2.id in (t1.sender_id, t1.recipient_id) group by t1.stuff"
db2exfmt -d sample -g -1 -o singlejoin.exfmt
根据这个相当不科学的测试,@Juan Carlos Oropeza的解决方案是最便宜的:
Access Plan:
-----------
Total Cost: 132.657
Query Degree: 1
Rows
RETURN
( 1)
Cost
I/O
|
101.808
^NLJOIN
( 2)
132.657
53
/-------+--------\
101.808 1
TBSCAN FETCH
( 3) ( 7)
13.6735 13.6215
2 2
| /---+----\
101.808 1 10000
SORT IXSCAN TABLE: LELLE
( 4) ( 8) T2
13.6733 6.81423 Q1
2 1
| |
101.808 10000
FETCH INDEX: SYSIBM
( 5) SQL150906110744470
13.6625 Q1
2
/---+----\
101.808 10000
IXSCAN TABLE: LELLE
( 6) T1
6.84113 Q2
1
|
10000
INDEX: SYSIBM
SQL150906110646160
Q2
在@ shA.t中使用两个子选项有点贵:
Access Plan:
-----------
Total Cost: 251.679
Query Degree: 1
Rows
RETURN
( 1)
Cost
I/O
|
101.808
>^NLJOIN
( 2)
251.679
103.99
/-------+--------\
101.808 1
TBSCAN FETCH
( 3) ( 12)
132.695 13.6215
52.9898 2
| /---+----\
101.808 1 10000
SORT IXSCAN TABLE: LELLE
( 4) ( 13) T2
132.691 6.81423 Q1
52.9898 1
| |
101.808 10000
>^NLJOIN INDEX: SYSIBM
( 5) SQL150906110744470
132.67 Q1
52.9898
/-------+--------\
101.808 1
TBSCAN FETCH
( 6) ( 10)
13.6881 13.6215
2 2
| /---+----\
101.808 1 10000
SORT IXSCAN TABLE: LELLE
( 7) ( 11) T2
13.6839 6.81423 Q2
2 1
| |
101.808 10000
FETCH INDEX: SYSIBM
( 8) SQL150906110744470
13.6625 Q2
2
/---+----\
101.808 10000
IXSCAN TABLE: LELLE
( 9) T1
6.84113 Q3
1
|
10000
INDEX: SYSIBM
SQL150906110646160
Q3
我的解决方案是最昂贵的解决方案:
Access Plan:
-----------
Total Cost: 758.822
Query Degree: 1
Rows
RETURN
( 1)
Cost
I/O
|
10000
GRPBY
( 2)
758.139
124.996
|
20000
NLJOIN
( 3)
756.923
124.996
/----------+----------\
10000 2
FETCH FETCH
( 4) ( 6)
122.351 27.0171
49 3.96667
/---+----\ /---+----\
10000 10000 2 10000
IXSCAN TABLE: LELLE RIDSCN TABLE: LELLE
( 5) T1 ( 7) T2
58.1551 Q2 13.6291 Q1
21 2
| /-------+-------\
10000 1.0016 1.0016
INDEX: SYSIBM SORT SORT
SQL150906110646160 ( 8) ( 10)
Q2 6.81465 6.81465
1 1
| |
1.0016 1.0016
IXSCAN IXSCAN
( 9) ( 11)
6.81423 6.81423
1 1
| |
10000 10000
INDEX: SYSIBM INDEX: SYSIBM
SQL150906110744470 SQL150906110744470
Q1 Q1
答案 2 :(得分:0)
另一种方式是使用双连接。
SELECT
t1.stuff,
tA.id sender_id,
tA.name sender_name,
tB.id recipient_id,
tB.name recipient_name
FROM
table1 t1
inner join table2 tA
on t1.sender_id = tA.id
inner join table2 tB
on t1.sender_id = tB.id
使用explain
或analyze