I have 3 tables and I want to query them and get results based on
Table 1 Team ID .
I think its simple but I failed to work this out.
All I'm trying to do is get their select their data from other tables and group them by TeamID.
The structure of tables is as follows:
Table 1
This table holds the teams entries by PersonID - referenced by PersonID Table, Each team is in one row
[TeamID] Tournament_id Person_1_ID Person_2_ID Person_3_ID Country_ID
--- --------- -------- ---------- --------- --------
1 77789 123 124 125 90
2 77789 126 127 128 95
3 77789 129 130 131 5
.........
Table 2
This is the person table PersonID = Primary Key
[PersonID] Name Dob Email Country_ID
--------- ------- -------- ---------- ------------
123 John 19/03/1992 John#live.com 90
124 Moe 20/10/1995 Moe#live.com 90
125 Sami 10/05/1989 Sami#example.com 90
126 Kim 30/01/1990 Kim#company.com 95
.......
Table 3
Participation table
[ParticipationID] PersonID tournament_id Country_id
----------------- ---------- ------------- -------------
9999901 123 77789 90
9999902 124 77789 90
9999903 125 77789 90
9999904 126 77789 95
9999905 127 77789 95
.......................
How do I get this output
TeamID Tournament_Id Name Country_ID
------ ----------- ----- ------
1 777789 John 90
1 777789 Moe 90
1 777789 Sami 90
2 777789 Kim 95
Something like this:
SELECT t.TeamID
,t.Tournament_Id
,pr.Name
,pr.Country_Id
FROM Team AS t
INNER JOIN Participation AS p ON p.tournament_id = t.Tournament_id
INNER JOIN Person AS pr ON pr.PersonID = p.PersonID
WHERE Tournament_Id = 777789
Related
I probably haven't explained this very well in the title but I have two tables. Here is a simple version.
channel_data
entry_id channel_id first_name last_name model other_fields
1 4 John Smith
2 4 Jane Doe
3 4 Bill Evans
4 15 235
5 15 765
6 15 543
7 15 723
8 15 354
9 15 976
10 1 xxx
11 2 yyy
12 3 123
channel_titles
entry_id author_id channel_id
1 101 4
2 102 4
3 103 4
4 101 15
5 101 15
6 101 15
7 102 15
8 102 15
9 103 15
10 101 1
11 102 2
12 103 3
I am not able to re-model the data unfortunately.
I need to list all the rows with a channel_id 15 from channel_data and beside them the first_name and last_name which has the same author_id from channel_titles.
What I want to return is this:
Model First Name Last Name
---------------------------------
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
If Model was in one table and Names were in another this would be much simpler but I'm not sure how to go about this when they are in the same table.
========================================
Edited to clarify.
I need to get each model with a channel_id 15 from channel_data
For each model I need to look up the entry_id in channel_titles to find the author_id
I need to find the row with that author_id AND channel_id 4 in channel titles (each row with channel_id 4 has a unique author_id).
I need to take the entry_id of this row back to channel_data and get the first_name and last_name to go with the model.
I am well aware that the data is not structured well but that is what I have to work with. I am trying to accomplish a very small task in a much larger system, remodelling the data is not an option at this point.
I think sub-queries might be what I am looking for but this is not my area at all usually.
Ok, that is convoluted. However, based on your description, this query should give you the results you want. The WHERE and JOIN descriptions follow the logic you have described in your question.
SELECT cd1.model, cd2.first_name, cd2.last_name
FROM channel_data cd1
JOIN channel_titles ct1 ON ct1.entry_id = cd1.entry_id
JOIN channel_titles ct2 ON ct2.channel_id = 4 AND ct2.author_id = ct1.author_id
JOIN channel_data cd2 ON cd2.entry_id = ct2.entry_id
WHERE cd1.channel_id = 15
ORDER BY cd1.entry_id
Output:
model first_name last_name
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
Demo on SQLFiddle
Table 1
*Table shortlisted_details*
---------------------------
id employee_id shortlisted_master_id
---------------------------------------------
1 12117 136
2 13133 136
3 11114 136
4 12117 116
5 12117 203
Table 2
*Table shortlisted_master*
------------------------
id filter_details_id
-------------------------
1 136
2 162
3 169
4 116
5 203
Table 3
*Table employees*
---------------
id first_name
------------------
12117 Faith
13133 David
11114 Noel
This is my query
SELECT COUNT(sd.id) as 'shortlisted_count',
sm.filter_details_id as Filter_ID
FROM shortlisted_details sd
INNER JOIN employees e ON e.id= sd.employee_id
INNER JOIN shortlisted_master sm ON sm.id=sd.shortlisted_master_id
WHERE sd.is_disqualified=0 AND sm.filter_details_id = 136
GROUP BY sm.id
This is the output
*Result*
shortlisted_count Filter_ID
---------------------------------
3 136
but if I change the where into this " WHERE sd.is_disqualified=0 AND sm.filter_details_id = 136 AND e.first_name LIKE '%Faith%' " this is the output of the query
*Result*
shortlisted_count Filter_ID
---------------------------------
1 136
but I want to happen is the same output of the 1st output even if I add the LIKE
Need to fetch data from master table based on child table condition.
Master Table:-
ID Name Address
1 abc xyz
2 abs txt
3 aui tre
4 pop top
5 the tre
6 pot tos
7 pog sop
8 pat top
9 bat cric
10 not kot
Child Table:-
chid shootid imagename IDFK
101 234 123ab.jpg 3
102 234 54abcab.jpg 3
103 235 123abc.jpg 3
104 236 12390acb.jpg Null
105 235 12332aab.jpg 8
106 234 123786ab.jpg 4
107 234 54789abcab.jpg 10
108 235 122343abc.jpg 10
109 235 122123acb.jpg 4
110 234 12123aab.jpg 9
111 234 1223ab.jpg Null
112 233 5432abcab.jpg Null
113 235 1239abc.jpg Null
114 236 1238acb.jpg 2
115 236 12356aab.jpg 2
116 236 1235ab.jpg 2
117 236 545abcab.jpg Null
118 237 1233abc.jpg 1
119 237 1223acb.jpg 1
120 237 1123aab.jpg 1
In Child table IDFK is the foreign key and ID in Master table is the primary key of that.
Now i want to show those name from master table that doesn't exist on child table filter on shootid like where childtable.Shootid=234. I tried but not find the desired output.Every time it just return's the same out for different shootid as well.
Please help me and show me the right query for that.
I don't know if I am understand you well or not but I think this is what you want,
Select * from [master] m
where m.ID not in (Select IDFK from detail where shootid=234)
I think this is what you are looking for.
Select distinct m.name from master m LEFT OUTER JOIN child c
ON m.id = c.id and
c.shootid=234
where
c.id is null
I want to be able to get all the data from table 1 and table 3 below but in addition to this I also want to get the latest application stage from table 2. The latest application stage is determined by getting the max stage_date for each application.
Table 1: applications
id | applicant_id | col_x | col_y | col_z
-----------------------------------------
10 300 a b c
11 310 a b c
12 320 a b c
13 330 a b c
14 340 a b c
Table 2: application_progress
id | application_id | application_stage | stage_date | stage_notes
------------------------------------------------------------------
1 10 DRAFT 2013-01-01 (NULL)
2 10 APPLICATION 2013-01-14 (NULL)
3 10 PHASE1 2013-01-30 (NULL)
4 11 DRAFT 2013-01-01 (NULL)
4 12 DRAFT 2013-01-01 (NULL)
5 13 DRAFT 2013-01-01 (NULL)
6 14 DRAFT 2013-01-01 (NULL)
7 14 APPLICATION 2013-01-14 (NULL)
EDIT: third table
Table 3: applicants
id | applicant_name | applicant_address | programme_id
------------------------------------------------------
300 Applicant 1 abc 1
310 Applicant 2 xyz 2
320 Applicant 3 xyz 2
330 Applicant 4 xyz 2
340 Applicant 5 xyz 2
Returned data set
applicant_id | applicant_name | current_stage
---------------------------------------------------------
300 Applicant 1 PHASE1
310 Applicant 2 DRAFT
320 Applicant 3 DRAFT
330 Applicant 4 DRAFT
340 Applicant 5 APPLICATION
Am struggling with this one and would appreciate any help.
PS. Tried to put an example of sqlfiddle but it's down at the minute. I'll update this with the sqlfiddle when it's back up if haven't had an answer before this.
You can do this with a correlated subquery:
select a.*,
(select application_stage
from application_progress ap
where ap.application_id = a.id
order by stage_date desc
limit 1
) MostRecentStage
from applications a;
EDIT:
You can joining in the applicant data with something like this::
select a.*, aa.*,
(select application_stage
from application_progress ap
where ap.application_id = a.id
order by stage_date desc
limit 1
) MostRecentStage
from applications a join
applicant aa
on a.applicant_id = aa.id;
we have three tables.
tblA
id(PK) doc_id(fk) mr_id(fk) date
-------- ---------- -------- ---------
1 23 22 2012-05-23
2 24 22 2012-05-23
3 25 21 2012-05-24
4 26 22 2012-05-24
tblB
doc_id(PK) d_name d_segId(FK) mr_id(FK)
------------ ------------- ---------- ----------
1 manish 1 12
23 rahul 2 22
24 paul 1 22
25 jacky 1 21
26 amit 2 22
tblC
seg_id(PK) seg_name seg_color
-------- ---------- --------
1 A_corei red
2 Bcorec green
what i want is all the record from tblA where mr_id=22 and date='2012-05-23' and order by seg_name in tblC
doc_id is referenced to tblB and
on the basis of doc_id, Seg_id is referenced to tblC how to use join in this situation.
It should look like
id doc_id d_name seg_color
-------- --------- --------- ----------
1 23 rahul green
2 24 paul red
try this.
SELECT a.id,b.doc_id,b.d_name,c.seg_color FROM tblB b
INNER JOIN tblA a ON b.doc_id=a.doc_id
INNER JOIN tblC c ON b.d_segId=c.seg_id
WHERE a.mr_id=22
AND a.date='2012-05-23'
SELECT
tblA.id,
tblA.doc_id,
tblB.d_name,
tblC.seg_color
FROM
tblA, tblB, tblC
WHERE
tblA.doc_id = tblB.doc_id
AND
tblB.d_segId = tblC.seg_id
AND
tblA.mr_id = 22
AND
tblA.date = '2012-05-23'
ORDER BY
tblC.seg_name