Conditional Inner join in sqlite python
I have three tables a
, b
and c
.
Table a
is related with table b
through column key
.
table b
is related with table c
through columns word
, sense
and speech
. In addition table c
holds column id
.
Now some rows in a.word have no matching value with b.word, based on that
I want to inner join
tables on condition if a.word = b.word then join, otherwise compare only a.end_key = b.key.
As a result I want to have table in form of a
with extra columns of start_id
and end_id
from c
matching with key_start
and key_end
.
I tried following sql command with python:
CREATE TABLE relations
AS
SELECT * FROM
c
INNER JOIN
a
INNER JOIN
b
ON
a.end_key = b.key
AND
a.start_key = b.key
AND
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
OR
a.word = b.word
a:
+-----------+---------+------+-----------+
| key_start | key_end | word | relation |
+-----------+---------+------+-----------+
| k5 | k1 | tree | h |
| k7 | k2 | car | m |
| k200 | k3 | bad | ho |
+-----------+---------+------+-----------+
b:
+-----+------+--------+-------+
| key | word | speech | sense |
+-----+------+--------+-------+
| k5 | sky | a | 1 |
| k2 | car | a | 1 |
| k3 | bad | n | 2 |
+-----+------+--------+-------+
c:
+----+---------+--------+-------+
| id | word | speech | sense |
+----+---------+--------+-------+
| 0 | light | a | 1 |
| 0 | dark | b | 3 |
| 1 | neutral | a | 2 |
+----+---------+--------+-------+
Edit for clarification:
The values of tables a
, b
and c
hold hundreds thousands lines, so there are matching values in the tables. Table a
is related to table b
with end_key
~ key
and start_key
~key
relation. Table b
is related to c
through word
sense
and speech
, there are values which match in each of these columns.
The desired table is in form
start_id
|key_start
|key_end
|end_id
|relation
Where start_id
matches key_start
and key_end
matches end_id
.
python-3.x sqlite3
add a comment |
I have three tables a
, b
and c
.
Table a
is related with table b
through column key
.
table b
is related with table c
through columns word
, sense
and speech
. In addition table c
holds column id
.
Now some rows in a.word have no matching value with b.word, based on that
I want to inner join
tables on condition if a.word = b.word then join, otherwise compare only a.end_key = b.key.
As a result I want to have table in form of a
with extra columns of start_id
and end_id
from c
matching with key_start
and key_end
.
I tried following sql command with python:
CREATE TABLE relations
AS
SELECT * FROM
c
INNER JOIN
a
INNER JOIN
b
ON
a.end_key = b.key
AND
a.start_key = b.key
AND
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
OR
a.word = b.word
a:
+-----------+---------+------+-----------+
| key_start | key_end | word | relation |
+-----------+---------+------+-----------+
| k5 | k1 | tree | h |
| k7 | k2 | car | m |
| k200 | k3 | bad | ho |
+-----------+---------+------+-----------+
b:
+-----+------+--------+-------+
| key | word | speech | sense |
+-----+------+--------+-------+
| k5 | sky | a | 1 |
| k2 | car | a | 1 |
| k3 | bad | n | 2 |
+-----+------+--------+-------+
c:
+----+---------+--------+-------+
| id | word | speech | sense |
+----+---------+--------+-------+
| 0 | light | a | 1 |
| 0 | dark | b | 3 |
| 1 | neutral | a | 2 |
+----+---------+--------+-------+
Edit for clarification:
The values of tables a
, b
and c
hold hundreds thousands lines, so there are matching values in the tables. Table a
is related to table b
with end_key
~ key
and start_key
~key
relation. Table b
is related to c
through word
sense
and speech
, there are values which match in each of these columns.
The desired table is in form
start_id
|key_start
|key_end
|end_id
|relation
Where start_id
matches key_start
and key_end
matches end_id
.
python-3.x sqlite3
add a comment |
I have three tables a
, b
and c
.
Table a
is related with table b
through column key
.
table b
is related with table c
through columns word
, sense
and speech
. In addition table c
holds column id
.
Now some rows in a.word have no matching value with b.word, based on that
I want to inner join
tables on condition if a.word = b.word then join, otherwise compare only a.end_key = b.key.
As a result I want to have table in form of a
with extra columns of start_id
and end_id
from c
matching with key_start
and key_end
.
I tried following sql command with python:
CREATE TABLE relations
AS
SELECT * FROM
c
INNER JOIN
a
INNER JOIN
b
ON
a.end_key = b.key
AND
a.start_key = b.key
AND
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
OR
a.word = b.word
a:
+-----------+---------+------+-----------+
| key_start | key_end | word | relation |
+-----------+---------+------+-----------+
| k5 | k1 | tree | h |
| k7 | k2 | car | m |
| k200 | k3 | bad | ho |
+-----------+---------+------+-----------+
b:
+-----+------+--------+-------+
| key | word | speech | sense |
+-----+------+--------+-------+
| k5 | sky | a | 1 |
| k2 | car | a | 1 |
| k3 | bad | n | 2 |
+-----+------+--------+-------+
c:
+----+---------+--------+-------+
| id | word | speech | sense |
+----+---------+--------+-------+
| 0 | light | a | 1 |
| 0 | dark | b | 3 |
| 1 | neutral | a | 2 |
+----+---------+--------+-------+
Edit for clarification:
The values of tables a
, b
and c
hold hundreds thousands lines, so there are matching values in the tables. Table a
is related to table b
with end_key
~ key
and start_key
~key
relation. Table b
is related to c
through word
sense
and speech
, there are values which match in each of these columns.
The desired table is in form
start_id
|key_start
|key_end
|end_id
|relation
Where start_id
matches key_start
and key_end
matches end_id
.
python-3.x sqlite3
I have three tables a
, b
and c
.
Table a
is related with table b
through column key
.
table b
is related with table c
through columns word
, sense
and speech
. In addition table c
holds column id
.
Now some rows in a.word have no matching value with b.word, based on that
I want to inner join
tables on condition if a.word = b.word then join, otherwise compare only a.end_key = b.key.
As a result I want to have table in form of a
with extra columns of start_id
and end_id
from c
matching with key_start
and key_end
.
I tried following sql command with python:
CREATE TABLE relations
AS
SELECT * FROM
c
INNER JOIN
a
INNER JOIN
b
ON
a.end_key = b.key
AND
a.start_key = b.key
AND
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
OR
a.word = b.word
a:
+-----------+---------+------+-----------+
| key_start | key_end | word | relation |
+-----------+---------+------+-----------+
| k5 | k1 | tree | h |
| k7 | k2 | car | m |
| k200 | k3 | bad | ho |
+-----------+---------+------+-----------+
b:
+-----+------+--------+-------+
| key | word | speech | sense |
+-----+------+--------+-------+
| k5 | sky | a | 1 |
| k2 | car | a | 1 |
| k3 | bad | n | 2 |
+-----+------+--------+-------+
c:
+----+---------+--------+-------+
| id | word | speech | sense |
+----+---------+--------+-------+
| 0 | light | a | 1 |
| 0 | dark | b | 3 |
| 1 | neutral | a | 2 |
+----+---------+--------+-------+
Edit for clarification:
The values of tables a
, b
and c
hold hundreds thousands lines, so there are matching values in the tables. Table a
is related to table b
with end_key
~ key
and start_key
~key
relation. Table b
is related to c
through word
sense
and speech
, there are values which match in each of these columns.
The desired table is in form
start_id
|key_start
|key_end
|end_id
|relation
Where start_id
matches key_start
and key_end
matches end_id
.
python-3.x sqlite3
python-3.x sqlite3
edited Nov 26 '18 at 15:55
flowian
asked Nov 25 '18 at 9:12
flowianflowian
36
36
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
EDIT new answer
The problem with the proposed query lies in the use of AND's and OR's (and likely missing (...)
). This statement
a.word = b.word then join, otherwise compare only a.end_key = b.key.
would translate to:
AND (a.word= b.word OR a.end_key = b.key)
.
Maybe try it like this:
ON
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
AND
(a.word = b.word OR a.end_key = b.key)
It would be a good idea to test in a sqlite manager (eg command line sqlite3, DB Browser for sqlite) before you try it in python; troubleshooting is much easier. And of course test the SELECT
before you implement it in a CREATE TABLE
.
You could clarify your question by showing the desired columns and result in relations
table that this sample data would create (there is nothing between b
and c
that would match on word, speech, sense
). Also the description of the relationship between a
and b
is confusing. In the first paragraph it says Table a is related with table b through column key
. Should key
be word
?
I edited the OP
– flowian
Nov 26 '18 at 8:12
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466094%2fconditional-inner-join-in-sqlite-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
EDIT new answer
The problem with the proposed query lies in the use of AND's and OR's (and likely missing (...)
). This statement
a.word = b.word then join, otherwise compare only a.end_key = b.key.
would translate to:
AND (a.word= b.word OR a.end_key = b.key)
.
Maybe try it like this:
ON
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
AND
(a.word = b.word OR a.end_key = b.key)
It would be a good idea to test in a sqlite manager (eg command line sqlite3, DB Browser for sqlite) before you try it in python; troubleshooting is much easier. And of course test the SELECT
before you implement it in a CREATE TABLE
.
You could clarify your question by showing the desired columns and result in relations
table that this sample data would create (there is nothing between b
and c
that would match on word, speech, sense
). Also the description of the relationship between a
and b
is confusing. In the first paragraph it says Table a is related with table b through column key
. Should key
be word
?
I edited the OP
– flowian
Nov 26 '18 at 8:12
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
add a comment |
EDIT new answer
The problem with the proposed query lies in the use of AND's and OR's (and likely missing (...)
). This statement
a.word = b.word then join, otherwise compare only a.end_key = b.key.
would translate to:
AND (a.word= b.word OR a.end_key = b.key)
.
Maybe try it like this:
ON
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
AND
(a.word = b.word OR a.end_key = b.key)
It would be a good idea to test in a sqlite manager (eg command line sqlite3, DB Browser for sqlite) before you try it in python; troubleshooting is much easier. And of course test the SELECT
before you implement it in a CREATE TABLE
.
You could clarify your question by showing the desired columns and result in relations
table that this sample data would create (there is nothing between b
and c
that would match on word, speech, sense
). Also the description of the relationship between a
and b
is confusing. In the first paragraph it says Table a is related with table b through column key
. Should key
be word
?
I edited the OP
– flowian
Nov 26 '18 at 8:12
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
add a comment |
EDIT new answer
The problem with the proposed query lies in the use of AND's and OR's (and likely missing (...)
). This statement
a.word = b.word then join, otherwise compare only a.end_key = b.key.
would translate to:
AND (a.word= b.word OR a.end_key = b.key)
.
Maybe try it like this:
ON
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
AND
(a.word = b.word OR a.end_key = b.key)
It would be a good idea to test in a sqlite manager (eg command line sqlite3, DB Browser for sqlite) before you try it in python; troubleshooting is much easier. And of course test the SELECT
before you implement it in a CREATE TABLE
.
You could clarify your question by showing the desired columns and result in relations
table that this sample data would create (there is nothing between b
and c
that would match on word, speech, sense
). Also the description of the relationship between a
and b
is confusing. In the first paragraph it says Table a is related with table b through column key
. Should key
be word
?
EDIT new answer
The problem with the proposed query lies in the use of AND's and OR's (and likely missing (...)
). This statement
a.word = b.word then join, otherwise compare only a.end_key = b.key.
would translate to:
AND (a.word= b.word OR a.end_key = b.key)
.
Maybe try it like this:
ON
b.word = c.word
AND
b.speech = c.speech
AND
b.sense = c.sense
AND
(a.word = b.word OR a.end_key = b.key)
It would be a good idea to test in a sqlite manager (eg command line sqlite3, DB Browser for sqlite) before you try it in python; troubleshooting is much easier. And of course test the SELECT
before you implement it in a CREATE TABLE
.
You could clarify your question by showing the desired columns and result in relations
table that this sample data would create (there is nothing between b
and c
that would match on word, speech, sense
). Also the description of the relationship between a
and b
is confusing. In the first paragraph it says Table a is related with table b through column key
. Should key
be word
?
edited Nov 26 '18 at 15:10
answered Nov 25 '18 at 18:41
DinoCoderSaurusDinoCoderSaurus
75958
75958
I edited the OP
– flowian
Nov 26 '18 at 8:12
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
add a comment |
I edited the OP
– flowian
Nov 26 '18 at 8:12
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
I edited the OP
– flowian
Nov 26 '18 at 8:12
I edited the OP
– flowian
Nov 26 '18 at 8:12
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
answer has been edited.
– DinoCoderSaurus
Nov 26 '18 at 15:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466094%2fconditional-inner-join-in-sqlite-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown