SELECT JOIN WHERE a record does not exist
up vote
0
down vote
favorite
I have 2 tables
id name type
1 aa driver
2 bb cyclist
3 cc runner
parent_id key value
1 mobile 00299029
2 mobile 008772
2 active 1
3 mobile 09887
3 active 0
I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND NOT EXISTS (
SELECT * FROM table2
WHERE key = 'active'
)
mysql sql join
add a comment |
up vote
0
down vote
favorite
I have 2 tables
id name type
1 aa driver
2 bb cyclist
3 cc runner
parent_id key value
1 mobile 00299029
2 mobile 008772
2 active 1
3 mobile 09887
3 active 0
I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND NOT EXISTS (
SELECT * FROM table2
WHERE key = 'active'
)
mysql sql join
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have 2 tables
id name type
1 aa driver
2 bb cyclist
3 cc runner
parent_id key value
1 mobile 00299029
2 mobile 008772
2 active 1
3 mobile 09887
3 active 0
I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND NOT EXISTS (
SELECT * FROM table2
WHERE key = 'active'
)
mysql sql join
I have 2 tables
id name type
1 aa driver
2 bb cyclist
3 cc runner
parent_id key value
1 mobile 00299029
2 mobile 008772
2 active 1
3 mobile 09887
3 active 0
I need to get the record 1,aa,driver, the one that has not a record with value 'active' in the second table.
My last try was something like this, but I'n not sure to be even a bit close to what I need, my result is always 0 records
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND NOT EXISTS (
SELECT * FROM table2
WHERE key = 'active'
)
mysql sql join
mysql sql join
edited yesterday
Rahul Neekhra
607627
607627
asked yesterday
ssstofff
451111
451111
add a comment |
add a comment |
7 Answers
7
active
oldest
votes
up vote
0
down vote
accepted
Just a left join would do
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2
ON t1.id = t2.parent_id
AND t2.key = 'active'
WHERE t2.key IS NULL
add a comment |
up vote
0
down vote
Try this (I think, not entirely sure I understand the linkage):
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null
A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.
add a comment |
up vote
0
down vote
This will work:
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND t2.key not like '%active%'
add a comment |
up vote
0
down vote
You can use NOT EXISTS as follows:
select t1.name from table1 t1
where not exists
(
select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
)
add a comment |
up vote
0
down vote
Select id,name,type from table1
where id Not in
(Select
parent_id from table2
group by parent_id
having key=
'active')
Better avoid join in this case I would say as subquery
would do good in this case.
add a comment |
up vote
0
down vote
I think Your query is not working in this case, please try
this
SELECT name FROM table1
JOIN table2 ON table1.id =
table2.parent_id where id not IN ( SELECT parent_id
FROM
table2 WHERE `key` = 'active' )
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
add a comment |
up vote
-1
down vote
SELECT t1.id,t1.name,t1.type,t2.key,t2.value
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.parent_id
where t2.key <>'active'
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Just a left join would do
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2
ON t1.id = t2.parent_id
AND t2.key = 'active'
WHERE t2.key IS NULL
add a comment |
up vote
0
down vote
accepted
Just a left join would do
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2
ON t1.id = t2.parent_id
AND t2.key = 'active'
WHERE t2.key IS NULL
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Just a left join would do
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2
ON t1.id = t2.parent_id
AND t2.key = 'active'
WHERE t2.key IS NULL
Just a left join would do
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2
ON t1.id = t2.parent_id
AND t2.key = 'active'
WHERE t2.key IS NULL
answered yesterday
George Joseph
45726
45726
add a comment |
add a comment |
up vote
0
down vote
Try this (I think, not entirely sure I understand the linkage):
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null
A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.
add a comment |
up vote
0
down vote
Try this (I think, not entirely sure I understand the linkage):
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null
A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this (I think, not entirely sure I understand the linkage):
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null
A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.
Try this (I think, not entirely sure I understand the linkage):
SELECT t1.name as name
FROM table1 as t1
LEFT JOIN table2 as t2 ON t1.id = t2.parent_id and t2.key = 'active' where t2.key is null
A left join returns all elements of the first table regardless of whether they have a corresponding record in the joined table. By then including t2.key is null in the where clause, you limit it to records that only exist in the first table.
answered yesterday
Chris Thompson
28.7k96896
28.7k96896
add a comment |
add a comment |
up vote
0
down vote
This will work:
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND t2.key not like '%active%'
add a comment |
up vote
0
down vote
This will work:
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND t2.key not like '%active%'
add a comment |
up vote
0
down vote
up vote
0
down vote
This will work:
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND t2.key not like '%active%'
This will work:
SELECT t1.name as name
FROM table1 as t1
JOIN table2 as t2 ON t1.id = t2.parent_id
AND t2.key not like '%active%'
answered yesterday
Vijesh
12519
12519
add a comment |
add a comment |
up vote
0
down vote
You can use NOT EXISTS as follows:
select t1.name from table1 t1
where not exists
(
select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
)
add a comment |
up vote
0
down vote
You can use NOT EXISTS as follows:
select t1.name from table1 t1
where not exists
(
select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
)
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use NOT EXISTS as follows:
select t1.name from table1 t1
where not exists
(
select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
)
You can use NOT EXISTS as follows:
select t1.name from table1 t1
where not exists
(
select 1 from table2 t2 where t1.id = t2.parent_id AND t2.key = 'active'
)
answered yesterday
isaace
2,5981415
2,5981415
add a comment |
add a comment |
up vote
0
down vote
Select id,name,type from table1
where id Not in
(Select
parent_id from table2
group by parent_id
having key=
'active')
Better avoid join in this case I would say as subquery
would do good in this case.
add a comment |
up vote
0
down vote
Select id,name,type from table1
where id Not in
(Select
parent_id from table2
group by parent_id
having key=
'active')
Better avoid join in this case I would say as subquery
would do good in this case.
add a comment |
up vote
0
down vote
up vote
0
down vote
Select id,name,type from table1
where id Not in
(Select
parent_id from table2
group by parent_id
having key=
'active')
Better avoid join in this case I would say as subquery
would do good in this case.
Select id,name,type from table1
where id Not in
(Select
parent_id from table2
group by parent_id
having key=
'active')
Better avoid join in this case I would say as subquery
would do good in this case.
edited yesterday
answered yesterday
Himanshu Ahuja
22812
22812
add a comment |
add a comment |
up vote
0
down vote
I think Your query is not working in this case, please try
this
SELECT name FROM table1
JOIN table2 ON table1.id =
table2.parent_id where id not IN ( SELECT parent_id
FROM
table2 WHERE `key` = 'active' )
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
add a comment |
up vote
0
down vote
I think Your query is not working in this case, please try
this
SELECT name FROM table1
JOIN table2 ON table1.id =
table2.parent_id where id not IN ( SELECT parent_id
FROM
table2 WHERE `key` = 'active' )
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I think Your query is not working in this case, please try
this
SELECT name FROM table1
JOIN table2 ON table1.id =
table2.parent_id where id not IN ( SELECT parent_id
FROM
table2 WHERE `key` = 'active' )
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I think Your query is not working in this case, please try
this
SELECT name FROM table1
JOIN table2 ON table1.id =
table2.parent_id where id not IN ( SELECT parent_id
FROM
table2 WHERE `key` = 'active' )
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
Himanshu Ahuja
22812
22812
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered yesterday
user9639650
572
572
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user9639650 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
add a comment |
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
Why you used join and subquery together I dont understand ?
– Himanshu Ahuja
yesterday
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
I changed the query according to the requirement , without changing what the user was trying to do. may be he/she needs join for the further operation.
– user9639650
21 hours ago
add a comment |
up vote
-1
down vote
SELECT t1.id,t1.name,t1.type,t2.key,t2.value
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.parent_id
where t2.key <>'active'
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
-1
down vote
SELECT t1.id,t1.name,t1.type,t2.key,t2.value
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.parent_id
where t2.key <>'active'
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
-1
down vote
up vote
-1
down vote
SELECT t1.id,t1.name,t1.type,t2.key,t2.value
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.parent_id
where t2.key <>'active'
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
SELECT t1.id,t1.name,t1.type,t2.key,t2.value
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.parent_id
where t2.key <>'active'
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered yesterday
Md Abdul Mannan
11
11
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Md Abdul Mannan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
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%2f53416996%2fselect-join-where-a-record-does-not-exist%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