Join three tables MySQL Where - mysql

First, I realize there are other similar questions, I have read through many of them and I cannot figure this out.
I have three tables like so.
urilist
----------------
rowid | uri
1000 xyz
1001 abc
1002 cde
1003 fgh
wordlist
----------------
rowid | word
1 word1
2 word2
3 word3
4 word4
wordlocation2
----------------
uriid | wordid
1001 1
1001 2
1001 3
1001 4
Table a hold uris. Table b holds words. Table c is a lookup table where you can see every word associated with a uri or vice versa.
I need to return the words for a given uri. so for uri abc I need words word1,word2,word3,word4.
This was my latest attempt.
select word from wordlist join wordlocation2 on wordlist.rowid =
wordlocation2.wordid
left join urilist on wordlocation2.uriid = urilist.rowid
and urilist.uri = "a uri address";
This returned thousands of unrelated answers. I'm not really sure of the best way to do this. Any help would be much appreciated.

Let us look at your query result by selecting *. I have used 'a uri address' as given by you for demonstrating regarding LEFT JOIN
Query:
select * from wordlist join wordlocation2 on wordlist.rowid = wordlocation2.wordid left join urilist on wordlocation2.uriid = urilist.rowid and urilist.uri = "a uri address";
This will fetch the below result
As you have done wordlocation2 LEFT JOIN urilist, even if there is no matching row for urilist, a value will come in the result by making the urilist value as NULL (this is the meaning of left join). In your case, you should not get any value if there is no matching row. So, use JOIN instead of LEFT JOIN.
select * from wordlist join wordlocation2 on wordlist.rowid = wordlocation2.wordid join urilist on wordlocation2.uriid = urilist.rowid where urilist.uri = "abc";
I think the order of tables doesn't matter as everything is inner join here. I am always ready to correct myself if anyone gives some suggestion on this.

This should be your query:
select l.word from urilist u
join wordlocation2 w on w.uriid = u.rowid
join wordlist l on l.rowid = wordid
where u.uri = 'some_uri'

Use:
select u.uri, wl.word
from urilist u
join wordlocation2 wl2 on wl2.uriid = u.id
join wordlist wl on wl2.wordid = wl.rowid

Related

find records in a table which is not associated in to other records in different table

I have 3 tables :
list_routes
pk_route_id route_code route_name route_description
3 J1 IND IND-DPS
4 J4 ADT ADT_DPS
tbl_stu_route
pk_stu_route_id fk_stu_cls_id fk_route_id
2 13 3
tbl_stu_class
pk_stu_cls_id fk_stu_id
13 56
Now what I want to achieve is write query in MYSQL that it will fetch me the records from list_routes which is not associated or attached in tbl_stu_class so for e.g in the above scenario it should give me the output :
pk_route_id route_code route_name route_description
4 J4 ADT ADT_DPS
I wrote the below query using left outer join as
SELECT
a.pk_route_id,
a.route_code,
a.route_name,
a.route_description
FROM
list_routes a
left outer join tbl_stu_route b on a.pk_route_id=b.fk_route_id
left outer join tbl_stu_class c on b.fk_stu_cls_id=c.pk_stu_cls_id
where c.fk_stu_id ='56'
but the output was
pk_route_id route_code route_name route_description
3 J1 IND IND-DPS
I am not sure where I am going wrong. Can you please enlighten me how to acieve this ?
If you want to get the not exists record then just filter out with IS NULL
SELECT a.*
FROM list_routes a
left outer join tbl_stu_route b on a.pk_route_id=b.fk_route_id
left outer join tbl_stu_class c on b.fk_stu_cls_id=c.pk_stu_cls_id
where b.fk_route_id is null;
Given the nature of your question, I would use not exists:
SELECT lr.*
FROM list_routes lr
WHERE NOT EXISTS (SELECT 1
FROM tbl_stu_route sr JOIN
tbl_stu_class c
ON sr.fk_stu_cls_id = c.pk_stu_cls_id
WHERE lr.pk_route_id = sr.fk_route_id AND
c.fk_stu_id = 56
);
Notes:
When using table aliases, use abbreviations for the table names. Abbreviations make the query much easier to follow than random letters.
Do not use single quotes for numeric constants. I assume that your ids are numbers, hence 56 rather than '56'.
I find that NOT EXISTS is a better fit to your problem description than LEFT JOIN, although both work.

How to grab specific result after table has been joined in mysql

i'm able to join 3 tables but i want to get the specific result based on user_id i'm getting null result why?
here is demo:http://sqlfiddle.com/#!9/c58f14/2
here is my query:
SELECT ifc.*,jp.* FROM interview_for_candidate_inbox ifc
LEFT JOIN jobs_applied_by_jobseeker_for_employer jbe on jbe.employee_id = ifc.user_id
LEFT JOIN jobs_posted_by_employer jp on jbe.job_id = jp.id
after the table being constructed
the same constructed table in demo:http://sqlfiddle.com/#!9/c58f14/2 (please run)
when i try this for above result
WHERE user_id = 151 AND job_is_deleted_or_not = 0
i'm getting blank result where BLANK RESULT DEMO:http://sqlfiddle.com/#!9/c58f14/3
please help me thanks in advance
Simply because
WHERE user_id = 152
Didn't match anything.
If you rewrite your query like this:
SELECT DISTINCT user_id FROM interview_for_candidate_inbox ifc
LEFT JOIN jobs_applied_by_jobseeker_for_employer jbe on jbe.employee_id = ifc.user_id
LEFT JOIN jobs_posted_by_employer jp on jbe.job_id = jp.id
WHERE job_is_deleted_or_not = 0;
You will find distinct user_id values, none of them is 152

Get rows that are not logged in a joined table

I got this 3 tables as shown aboved.
My goal is to get all bits that user 1 (jm) did not put a reaction to.
Currently I have this MySQL code:
select * from bit b LEFT JOIN bit_reaction br ON (br.bitId=b.id AND br.userId != 1)
The problem here is that bit_reaction.id = 2 is being returned since br.userId is not equal to 1. The correct behavior is that it would only return bits with id 2 and 3.
Thanks for the tip!
select b.*
from bit b
left join bit_reaction br ON br.bitId = b.id
AND br.userId = 1
WHERE br.bitId is null
I assume that you want every user to react every bit. So the more generic answer will be...
SELECT *
FROM bit AS tBit
LEFT JOIN user AS tUser ON 1 = 1
LEFT JOIN bit_reaction AS tReaction ON tBit.id = tReaction.bitID
AND tUser.id = tReaction.userID
WHERE tReaction.userID IS NULL

join or select in on multiple fields

(The example that follows is hypothetical, but illustrates the concept).
Using MySQL, say I have 2 tables:
userFromID userToId moreInfo
1 2 cat
1 3 dog
4 1 bear
3 4 fish
And...
userId someInfo addlInfo
1 m 32
2 f 33
3 m 25
4 f 28
And I want to query for a user id, and get back joined info from both tables for all users that share a relationship with user1.
assume that the first table has something like alter table thatFirstTable add unique index(userFromId, userToId) so there won't be any duplicates - each relationship between the two ids will be unique.
it doesn't matter who's the "from" or "to"
so the desired result would be something like this, if queried for relationships with user id: 1
userId moreInfo someInfo addlInfo
2 cat f 33
3 dog m 25
4 bear f 28
Thanks.
/EDIT this "works" but I suspect there's a better way?
SELECT * FROM users JOIN friends ON friends.userFrom = users.id OR friends.userTo = users.id WHERE users.id != 1 AND friends.userFrom = 1 OR friends.userTo = 1
/EDIT2 - I updated the sample output to better reflect the goal
try this query::
select tbl2.userid,tbl1.moreinfo,
tbl2.someinfo,tbl2.addinfo
from tbl1 join tbl2
on (tbl1.usertoid = tbl2.userid and tbl1.userfromid = 1)
You should just join the tables with the query below.
select u.userId, f.moreInfo, u.someInfo, u.addlInfo
from users AS u INNER JOIN friends AS f ON u.userId = f.UserToId
where f.userFrom = 1
Try this. Tested and 100% working
select a.userToID, a.moreInfo, b.someInfo, b.addInfo from tbl1 a
left outer join
tbl2 b on a.userToID = b.userId
where a.userFromID = 1;

MySQL join & search

I have a problem with joining some tables, heres my structure:
tbl_imdb:
fldID fldTitle fldImdbID
1 Moviename 0000001
tbl_genres:
fldID fldGenre
1 Action
2 Drama
tbl_genres_rel:
fldID fldMovieID fldGenreID
1 1 1
2 1 2
What I’m trying to do is a query that will find all movies that is both an action movie and drama, is this possible to do without a subquery, if so, how?
What I'm trying right now is:
SELECT tbl_imdb.*
FROM tbl_imdb
LEFT JOIN tbl_imdb_genres_rel ON ( tbl_imdb.fldID = tbl_imdb_genres_rel.fldMovieID )
LEFT JOIN tbl_imdb_genres ON ( tbl_imdb_genres_rel.fldGenreID = tbl_imdb_genres.fldID )
WHERE tbl_imdb_genres.fldGenre = 'Drama'
AND tbl_imdb_genres.fldGenre = 'Action';
But this dosnt work, however it does work if I only keep one of the two WHERE's, but thats not what I want.
Two ways to do it:
1
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel rel_action
ON tbl_imdb.fldID = rel_action.fldMovieID
INNER JOIN tbl_genres genre_action
ON rel_action.fldGenreId = genre_action.fldID
AND 'Action' = genre_action.fldGenre
INNER JOIN tbl_genres_rel rel_drama
ON tbl_imdb.fldID = rel_drama.fldMovieID
INNER JOIN tbl_genres genre_drama
ON rel_drama.fldGenreId = genre_drama.fldID
AND 'Drama' = genre_drama.fldGenre
This method is on the same path as your original solution. 2 differences:
The join should be inner, not left because you're trying to get movies that certainly have the corresponding genre entry
Since you want to find 2 different generes, you'll have to do the join with tbl_genres_rel and tbl_genres twice, once for each particular genre you're interested in.
2
SELECT tbl_imdb.*
FROM tbl_imdb
INNER JOIN tbl_genres_rel
ON tbl_imdb.fldID = tbl_genres_rel.fldMovieID
INNER JOIN tbl_genres
ON tbl_genres_rel.fldGenreId = tbl_genres.fldID
AND tbl_genres.fldGenre IN ('Action', 'Drama')
GROUP BY tbl_imdb.fldID
HAVING COUNT(*) = 2
Again, the basic join plan is the same. Difference here is that we join to the tbl_genres_rel and tbl_genres path just once. This on itself fetches all genres for one film, and then filters for the one's you're interested in. The ones that qualify will now have 2 rows for each distinct value of tbl_imdb.fldId. The GROUP BY aggregates on that, flattening that into one row. By asserting in the HAVING clause that we have exactly 2 rows, we ensure that we keep only those rows that have both the genres.
(Note that this assumes that there is a unique constraint on tbl_genres_rel over {fldMovieID, fldGenreID}. If such a constraint is not present, you should consider adding it.)
LEFT JOIN is not applicable in your case because records should exist on both tables. And you need to count the instances of the movie
SELECT *
FROM tbl_imdb a
INNER JOIN tbl_genres_rel b
on a.fldID = fldMovieID
INNER JOIN tbl_genres c
on c.fldGenreID = b.fldID
WHERE c.fldGenre IN ('Drama', 'Action')
GROUP BY a.Moviename
HAVING COUNT(*) > 1