What I'm trying to do is to switch up an id with the corresponding name from another table.
Teams:
1 - team_01
2 - team_02
Games:
team_a team_b score_a score_b
1 2 30 40
What I want to get is:
Games:
team_a team_b score_a score_b
team_01 team_02 30 40
I try:
SELECT
games.id
, games.score_team_a
, games.score_team_b
, games.time
, games.category
, games.team_a
, games.team_b
FROM games
LEFT JOIN teams t1 ON t1.id = games.team_a
LEFT JOIN teams t2 ON t2.id = games.team_b
SELECT
games.id
, games.score_team_a
, games.score_team_b
, games.time
, games.category
, t1.<team_name> as team_a -- reference the join tables
, t2.<team_name> as team_b
FROM games
LEFT JOIN teams t1 ON t1.id = games.team_a
LEFT JOIN teams t2 ON t2.id = games.team_b
You don't need a left join, I don't see why the team ids in the games table would not match an id inside the teams table.
Also if you need only 4 columns, why do you select all the other columns?
SELECT
t1.name team_a,
t2.name team_b,
g.score_a,
g.score_b
FROM games g
INNER JOIN teams t1 ON t1.id = g.team_a
INNER JOIN teams t2 ON t2.id = g.team_b
Related
First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know how is called the teacher who makes more money with the classes:
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 1
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
My main problem is that I don't know how to deal with the repeated values from the first table when I'm doing the triple join.
So far "I've made" this:
select IDTeacher, PricePerClass
from Table1 as T1
inner join Table2 as T2 on t1.LessonName = t2.Lesson
inner join Table3 as T3 on t1.TeacherId = t3.idTeacher
...
And I don't know how to keep going. I will have to group the t1.LessonName but every time I try to do it I get syntax errors. As you can see I'm pretty lost.
EDIT: My expected result would be something like:
IDTeacher TotalRevenue
1 250
Thanks a lot.
join the tables, group by teacher to aggregate and get the top row after you sort by the total descending:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
order by TotalRevenue desc limit 1
Note that this query does not return ties, if any.
SELECT t3.IDTeacher, t3.TeacherName, sum(t2.PricePerClass) from table1 t1
inner join table3 t3 on t1.TeacherID = t3.IDTeacher
inner join table2 t2 on t1.Lessonname = t2.Lesson
group by t3.TeacherName
order by sum(t2.PricePerClass) desc limit 1;
I have a problem similar to that answered in:
SQL: Get Products from a category but also must be in another set of categories
but I need to include 3 tables (that deals only with 2)
I have three tables: Image, Category and CategoryImage.
Image:
id name
1 Fred
2 Joan
CategoryImage
imageId categoryId
1 10
1 20
1 30
2 15
2 20
2 30
3 10
Category
id title
10 Hiking
15 Walking
20 Family
30 Older
The only value being passed into the query is the category title. I'm stuck with this situation since the data is coming via an existing website app.
I want to return the image id for images which are in ALL the categories I'm interested in - i.e., if the user selected 'Hiking', 'Family' and 'Older' then I want to see only image 1. If the user selected 'Walking' and 'Older' I'd see only image 2.
The sql from the linked question works if I manually plug in the category ID.
select t0.id, t0.name
from image t0
inner join CategoryImage t1
on (t0.Id = t1.imageId AND t1.categoryId = 10)
inner join CategoryImage t2
on (t0.Id = t2.imageId AND t2.categoryID = 20)
inner join CategoryImage t3
on (t0.Id = t3.imageId AND t3.categoryID = 30)
How do I expand this correctly to add in the fact that actually all I know is the Category table title value.
Thanks for any help.
liz
simply
select t0.*
from
image t0
join CategoryImage t1 on t0.id = t1.imageId
join Category t2 on t1. categoryId = t2.id
where
t2.id in ( $list_of_id )
which
$list_of_id = implode(",", $ids);
or
$list_of_id = '10,20,30';
image is a datatype. plz,change the table name image and try this query...
select * from table1 inner join table2 on table1.id=table2.id inner join table3 on table2.id=table3.id where (condition)
This can be possible if you have the count of your category names you are using in query like for 'Hiking', 'Family','Older' count is 3 so you can use below query, SUM with the IN() expression will give you the count for each image group and in HAVING clause you can filter over the results with your condition
SELECT t0.id, t0.name ,
SUM(t2.title IN('Hiking', 'Family','Older')) `image_exist`
FROM image t0
INNER JOIN CategoryImage t1
ON (t0.Id = t1.imageId)
INNER JOIN Category t2
ON (t2.id = t1.categoryId)
GROUP BY t0.id
HAVING image_exist >= 3 ;
Fiddle Demo
Same for the second scenario 'Walking' and 'Older' count is 2 you can do so
SELECT t0.id, t0.name ,
SUM(t2.title IN('Walking','Older')) `image_exist`
FROM image t0
INNER JOIN CategoryImage t1
ON (t0.Id = t1.imageId)
INNER JOIN Category t2
ON (t2.id = t1.categoryId)
GROUP BY t0.id
HAVING image_exist >= 2 ;
Solution is
select t0.* from images t0 inner join categoryImage t1
on t0.id=t1.ImageID
where t1.CategoryID in (select id from Category where name in ('Hiking','Family','Older'))
group by t0.id,t0.name
having count(categoryid)=(select count(id) from Category where name in ('Hiking','Family','Older'))
select t0.* from images t0 inner join categoryImage t1
on t0.id=t1.ImageID
where t1.CategoryID in (select id from Category where name in ('Walking','Older'))
group by t0.id,t0.name
having count(categoryid)=(select count(id) from Category where name in ('Walking','Older'))
Here is my tables:
T1 record:
id referencePerson1ID referencePerson2ID referencePerson3ID
1 1 2 3
T2 referencePerson:
id name
1 Peter
2 John
3 Mary
I want to get the following result when I choose T1 id =1:
id referencePerson1 referencePerson2 referencePerson3
1 Peter John Mary
How can I do that?
Thanks
This is one basic way to do things:
SELECT T1.id, P1.name, P2.name, p3.name
FROM record T1 LEFT JOIN referencePerson P1
ON T1.referencePerson1ID=P1.id
LEFT JOIN referencePerson P2
ON T1.referencePerson2ID=P2.id
LEFT JOIN referencePerson P3
ON T1.referencePerson3ID=P3.id
Another way when number of persons is unknown can be done using a PIVOT
You can do that with three times JOIN :
SELECT t1.id, t21.name, t22.name, t23.name
FROM T1
INNER JOIN T2 t21 ON t21.id = T1.referencePerson1ID
INNER JOIN T2 t22 ON t22.id = T1.referencePerson2ID
INNER JOIN T2 t23 ON t23.id = T1.referencePerson3ID
WHERE T1.id = 1
I have two tables:
teams
----------------
|uid|name |rank|
----------------
| 1 |Team1| 1 |
| 2 |Team2| 2 |
----------------
games
-----------------------------------------------------------------------
|uid|team_one_uid|team_one_score|team_two_uid|team_two_score|game_date|
-----------------------------------------------------------------------
|1|1|70|2|50|2012-12-12|
The teams table has a list of teams and other data like a rank.
The games table has a list of games and references each team by it's unique id (uid).
What query can I run in order to see a result that contains a row with the following columns:
game_uid, team_one_name, team_one_rank, team_one_score, team_two_name, team_two_rank, team_two_score, game_date
select g.uid as game_uid,
t1.name as team_one_name,
t1.rank as team_one_rank,
team_one_score,
t2.name as team_two_name,
t2.rank as team_two_rank,
team_two_score,
game_date
from games g
inner join teams t1 on t1.uid = team_one_uid
inner join teams t2 on t2.uid = team_two_uid
I think this is what you're looking for:
SELECT g.uid AS game_uid, t1.name AS team_one_name, t1.rank AS team_one_rank,
g.team_one_score, t2.name AS team_two_name, t2.rank AS team_two_rank,
g.team_two_score, g.game_date
FROM `games` g, `teams` t1, `teams` t2
WHERE t1.id = g.team_one_uid
AND t2.id = g.team_two_uid
This can also be done with INNER JOIN but it comes up to the same thing.
I have tables
table 1
id text
1 A
1 B
2 C
table 2
id text
1 x
1 f
2 y
2 z
I want to join them this way
1 A x
1 B f
2 C y
2 z
In other words i want to see all texts from table1 and table2 grouped by id, with no repeats.
Any ideas?
Update: as they say in comments, the logic is not clear, I'll try to explain.
I have current values in table_1 and deleted values in table_2.
Customer wants to see current values and deleted values in one table grouped by some id.
Simple solution to get something close to what you're looking for
SELECT t1.id, t1.text, t2.text
FROM tbl_1 t1
INNER JOIN tbl_2 t2
ON t1.id = t2.id
this will create output
1 A x
1 B x
2 C y
2 C z
Only different is now that the duplicated texts x and C should somehow removed.
Update
precondition: duplicates per id are either in tbl_1 or tbl_2 not both !
Joining a grouped select in addition to above simple solution will allow to create kind of "CASE-Filters" to get your desired output.
SELECT
t1.id,
CASE
WHEN t2.text = txt_i2 THEN t1.text
END AS txt_t1,
CASE
WHEN t1.text = txt_i1 THEN t2.text
END AS txt_t2
FROM (
SELECT
i1.id,
i1.text AS txt_i1,
i2.text AS txt_i2
FROM tbl_1 i1
INNER JOIN tbl_2 i2
ON i1.id = i2.id
GROUP BY id
) i
INNER JOIN tbl_1 t1
ON i.id = t1.id
INNER JOIN tbl_2 t2
ON t1.id = t2.id
You should create a view of the tbl_1-tbl_2-join to get more readable SQL:
CREATE OR REPLACE VIEW V_tbl_1_2 AS (
SELECT
t1.id,
t1.text AS txt_1,
t2.text AS txt_2
FROM tbl_1 t1
INNER JOIN tbl_2 t2
ON t1.id = t2.id
)
;
SELECT
t.id,
CASE
WHEN t.txt_2 = i.txt_2 THEN t.txt_1
END AS txt_t1,
CASE
WHEN t.txt_1 = i.txt_1 THEN t.txt_2
END AS txt_t2
FROM V_tbl_1_2 t
INNER JOIN (
SELECT *
FROM V_tbl_1_2
GROUP BY id
) i ON t.id = i.id
;
USE MYSQL VIEW OR JOIN
This works if you can have no more than two items per id in either table and if neither one has complete duplicates. (And I must also add that this can only work if MySQL is able to swallow this monster and not choke with it.)
SELECT
COALESCE (t1.id, t2.id) AS id,
t1.text AS text1,
t2.text AS text2
FROM (
SELECT
t.id,
t.text,
CASE t.text WHEN m.text THEN 1 ELSE 2 END AS rowid
FROM table_1 t
INNER JOIN (
SELECT id, MIN(text) AS text
FROM table_1
GROUP BY id
) m ON t.id = m.id
) t1
FULL JOIN (
SELECT
t.id,
t.text,
CASE t.text WHEN m.text THEN 1 ELSE 2 END AS rowid
FROM table_2 t
INNER JOIN (
SELECT id, MIN(text) AS text
FROM table_2
GROUP BY id
) m ON t.id = m.id
) t2
ON t1.id = t2.id AND t1.rowid = t2.rowid
ORDER BY COALESCE (t1.id, t2.id), COALESCE (t1.rowid, t2.rowid)