How to get the columns from three different tables? - mysql

I have three different table as mention below
mRNA_GO
MSU7_LOC
GO_ID
Gene Ontology
GO_ID (Primary Key)
Category
Term
Evidence
miRNA_mRNA
miRNA_ID
MSU7_LOC
Table 1 is connected with table 2 by GO_ID and with table 3 by MSU7_LOC.
I want following columns in my output.
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidence
tabke3.miRNA_ID
I have written two diff query
Query 1
select gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC
from gene_ontology inner join mrna_go on mrna_go.go_id = gene_ontology.go_id
where mrna_go.go_id in ('GO:0009058') ;
Which will give me following columns
table1.MSU7_LOC
table2.GO_ID
table2.Category
table2.term
table2.Evidenc
Query 2
SELECT mrna_go.go_id, mirna_mrna.mirna
from mirna_mrna inner join mrna_go on mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
where mrna_go.go_id in ('GO:0009058') ;
which will give me
table2.GO_ID
tabke3.miRNA_ID
Can any one tell me how can I get the output using only one query not two different query..

Just join third table
SELECT
mg.go_id,
mm.mirna ,
g.go_id ,
g.category,
g.evidence,
g.term ,
mg.MSU7_LOC
FROM mirna_mrna mm
inner join mrna_go mg on mg.MSU7_LOC = mm.MSU7_LOC
inner join gene_ontology g on mg.go_id = g.go_id
where mg.go_id in ('GO:0009058') ;

just add a 2nd join in like;
SELECT gene_ontology.go_id , gene_ontology.category, gene_ontology.evidence, gene_ontology.term , mrna_go.MSU7_LOC, mrna_go.go_id, mirna_mrna.mirna
FROM gene_ontology
INNER JOIN mrna_go ON mrna_go.go_id = gene_ontology.go_id
INNER JOIN mirna_mrna ON mrna_go.MSU7_LOC = mirna_mrna.MSU7_LOC
WHERE mrna_go.go_id IN ('GO:0009058') ;

Related

MYSQL - CONCATENATE a lookup tables columns into one row

I have the following table structure:
tbl_catalogue_state
In tbl_catalogue there is a part number 58674 that has three states in the tbl_catalogue_state_lk table. Here is the result when I run a query inner joining the three tables.
As expected there are multiple rows returned.
Is there a way to only return one row having the values for each catalgue_state_id on the same row?
I would also like the ability to ignore a row for example:
select tbl_catalogue.catalogue_part, tbl_catalogue_state.catalogue_state_id from tbl_catalogue
inner join tbl_catalogue_state_lk on tbl_catalogue.catalogue_id = tbl_catalogue_state_lk.catalogue_id
inner join tbl_catalogue_state on tbl_catalogue_state_lk.catalogue_state_id = tbl_catalogue_state.catalogue_state_id
where tbl_catalogue_state_lk.catalogue_state_id <> 1;
The above select still returns two rows.
UPDATE
I was able to use GROUP_CONCAT:
select tbl_catalogue.catalogue_part, GROUP_CONCAT(tbl_catalogue_state.catalogue_state_id) as cat_state from tbl_catalogue
inner join tbl_catalogue_state_lk on tbl_catalogue.catalogue_id = tbl_catalogue_state_lk.catalogue_id
inner join tbl_catalogue_state on tbl_catalogue_state_lk.catalogue_state_id = tbl_catalogue_state.catalogue_state_id
where tbl_catalogue_state_lk.catalogue_state_id <> 1
group by tbl_catalogue.catalogue_id;
My issue is the above statement still returns a row. I need it to return nothing.
I was able to use not exists:
select tc.catalogue_part, GROUP_CONCAT(tcs.catalogue_state_id) as cat_state from tbl_catalogue as tc
inner join tbl_catalogue_state_lk as tcsl on tc.catalogue_id = tcsl.catalogue_id
inner join tbl_catalogue_state as tcs on tcsl.catalogue_state_id = tcs.catalogue_state_id
where
not exists
(
select tcsl2.catalogue_state_id from tbl_catalogue_state_lk as tcsl2
where tcsl2.catalogue_state_id = 6 and tcsl2.catalogue_id = tc.catalogue_id
)
and
not exists
(
select tcsl3.catalogue_state_id from tbl_catalogue_state_lk as tcsl3
where tcsl3.catalogue_state_id = 1 and tcsl3.catalogue_id = tc.catalogue_id
)
and
not exists
(
select tcsl3.catalogue_state_id from tbl_catalogue_state_lk as tcsl3
where tcsl3.catalogue_state_id = 2 and tcsl3.catalogue_id = tc.catalogue_id
)
group by tc.catalogue_id;

Multiple table join query

I have one master table as "master_tbl" which has following fields :
m_id(PK)
m_name
Two slave tables which can be :
Slave-1 :
---------
sl1_id PK
sl1_name
sl_m_id FK
Slave-2 :
---------
sl2_id PK
sl2_name
sl2_m_id FK
I need output as in one query like the matching records details should be displayed like :
m_id m_name sl1_name(or sl2_name)
last displaying field should be take name of matching records from slave1 or Slave2 table.
Inner Join query Use
SELECT mt.name, s1.sl1_name, s2.sl2_name FROM master_tbl as mt
INNER JOIN Slave-1 as s1 ON s1.sl_m_id = mt.m_id
INNER JOIN Slave-2 as s2 ON s2.sl2_m_id = mt.m_id
SELECT m_id, m_name, sl1_name as `sl1_name(or sl2_name)` FROM slave_1 S1
INNER JOIN master_tbl M ON S1.sl1_m_id = M.m_id
UNION
SELECT m_id, m_name, sl2_name as `sl1_name(or sl2_name)` FROM slave_2 S2
INNER JOIN master_tbl M ON S2.sl2_m_id = M.m_id

SQL Join for retrieving record for a single user

I have user related info split into multiple tables . I am trying to write a join to retrieve data for a single user , a lot of the info is optional , so the entry for many columns may be null , which is okay . I have written the following query , it is working except that it returns all users when I want the user with id '69'
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN (
SELECT *
FROM cur_website
WHERE Userid =69
) AS w ON cur_doctor_names.UserId = w.Userid
I want the following result :
First_Name | Last Name | Website
ABC XYZ Null
where ABC is the name for user with id 69 .
You are only filtering the websites because you are using a left join. You should apply your filter to the doctors table
SELECT
cur_doctor_names.First_Name,
cur_doctor_Names.Last_Name,
w.Website
FROM
cur_doctor_names
LEFT JOIN cur_website AS w
ON cur_doctor_names.UserId = w.Userid
WHERE
cur_doctor_names.UserId = 69
Try writing it like this - your where clause was in the wrong place (also your left join does not need to be written like that):
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website as w
ON cur_doctor_names.UserId = w.Userid
WHERE cur_doctor_names.userId = 69
I think you can just write
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website w ON cur_doctor_names.UserId = w.Userid
WHERE Userid =69

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

MySQL - Using column value for joining in the same query

I have three tables that looks something like this:
Table joins
|ID|JOIN_NAME|
1 persons
2 companies
Table information
|ID|JOIN_ID|
1 1
2 2
Table information_extra_persons
|ID|INFORMATION_ID|NAME|
1 1 John
Table information_extra_companies
|ID|INFORMATION_ID|NAME|
1 2 IBM
How can i join together these tables in one SQL? I've tried something like:
SELECT * FROM `information`
INNER JOIN `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`
ON `information_extra_(SELECT `name` FROM `joins` WHERE `id` = `join_id`)`.`information_id` = `information`.`id`
but I can't get it to work. Of course this isn't my actual table setup, but it's the same principle. Does anyone know how to get all the info in just one SQL?
That's actually four tables, not three. This isn't just a nitpick - it looks as though the substance of your question is "how can I use the name of the table as part of the join criteria?" (ie. how can the information_extra_ tables be treated as a single table?)
To which the answer is: you can't. (Outside of dynamic SQL.)
In this specific case, the following should return what I think you are looking for:
select j.join_name joined_entity,
case when j.join_name = 'persons' then p.name
else c.name
end joined_entity_name
from information i
inner join joins j on i.join_id = j.id
left join information_extra_persons p on i.id = p.information_id
left join information_extra_companies c on i.id = c.information_id
Alternatively, a less efficient (but more general) approach might be:
select j.join_name joined_entity,
v.name joined_entity_name
from information i
inner join joins j on i.join_id = j.id
inner join (select 'persons' entity, information_id, name from information_extra_persons
union all
select 'companies' entity, information_id, name from information_extra_companies) v
on i.id = v.information_id and j.join_name = v.entity