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.
Related
I have 2 tables namely t_users, and t_user_freinds. The Schema is quite simple:
-------- --------
|t_users| | t_users_freinds|
-------- -----------------
| id | | id |
-------- ------------------
| name | |user_1 |
-------- ----------------
| user_2 |
----------------
My question is how do I get friends of friends of friends (depth level 3) for a given person with id = 1?
An id is just a number from 1 to x. The user_1 is a friend of user_2. Both user_1 and user_2 exist in the t_user table.
I'm fairly new to SQL.
Thanks.
UPDATE:
I tried this
select t1.id, t1.name,
t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t2.user_1 = t2.user_2
WHERE t1.id = "1"
But it did not work and gives a result of null (no errors though.)
For finding only the friends of a user:
select t1.id, t1.name,
t2.user_2 as friends
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
WHERE t1.id = "1"
This worked as expected.
Finding Freinds of Freinds of Freinds
select t1.id, t1.name, t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t3.user_1 = t2.user_2
WHERE t1.id = "5"
This SQL query seems to work as expected. Thanks everyone.
This SQL query seems work as expected and answers my question:
select t1.id, t1.name, t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t3.user_1 = t2.user_2
WHERE t1.id = "5"
Thanks, everyone.
If i understand your datamodel correctly try below,
SELECT users.id,
users.name,
frnds.user_2 as friends,
frndsoffriends.user_2 as friendsoffriends
FROM t_user_friends frnds,
t_user_friends frndsoffriends,
t_users users
WHERE users.id = 1
AND frnds.user_1 = users.id
AND frnds.user_2 = frndsoffriends.user_1
EDIT: With explicit JOIN in the statement if that is your preferred coding style.
select t1.id, t1.name,
t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t2.user_2 = t3.user_1
WHERE t1.id = 1
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
please help me to join these two selects:
SELECT IFNULL(sum(estimated_hours * t2.man_hour),0) as
estimated from project_has_tasks t1 left join users t2
on t1.user_id = t2.id group by project_id
SELECT IFNULL(sum(TIME_FORMAT(SEC_TO_TIME
(time_spent),'%k.%i' )* t2.man_hour),0) as time_spent_cost FROM project_has_tasks t1
left join users t2 on t1.user_id = t2.id group
by project_id
Wanna to get:
| estimated | time_spent_cost |
_______________________________
| 000000000 | 00000000 |
Simply put them in one query:
SELECT IFNULL(sum(estimated_hours * t2.man_hour),0) as
estimated, IFNULL(sum(TIME_FORMAT(SEC_TO_TIME
(time_spent),'%k.%i' )* t2.man_hour),0) as time_spent_cost
from project_has_tasks t1 left join users t2
on t1.user_id = t2.id group by project_id
I have two tables. t1 contains business info:
t1
id | busName | busPhone
t2 contains business hours
t2
id | busId | open | close
Where t1.id = t2.busId
I need to create a query and loop through it and if t2 does not have corresponding records, select those in order to add data. Clearly the query below is incorrect...
SELECT t1.id
FROM t1
LEFT JOIN t2 ON (t1.id = t2.busId)
WHERE t2.id != ''
This should be your query:
SELECT DISTINCT(t1.id)
t1 LEFT JOIN t2 ON (t1.id = t2.busId)
WHERE t2.busId IS NULL
select id from t1 where id not in (select busId from t2)
Question:
Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
Table 3: id. X | number.+1 123 555 9999 | calls.55
I need a query that join the 3 tables and I only have the id (userid) from the table 1.
So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.
Answer:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
New Question?
Any chance you know how can I print the sum of calls on the result? after all these join I get about 10 lines of the same user and their respective calls based on the phone, what is correct for what I asked, now I need return all the calls in 1 line with the values:
sum | user ID | user Name
Maybe this will be useful:
SELECT SUM(t3.calls) FROM table1 t1 INNER JOIN table2 t2 ON t2.useridfromtable=t1.id INNER JOIN table3 t3 ON t3.number = t2.number
Try this query:
SELECT sum(t3.calls), t1.id, t1.name
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
GROUP BY t1.id