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
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
i have 3 tables
1) users
2) user_likes
3) user_pictures
4) user_pucture_likes
users(id, name)
user_likes(id, user_id, like)
user_pictures(id, user_id, filename)
user_picture_likes(id, user_picture_id, like)
what i am trying to do is to sort the user on the total like on user_like + user_picture_like
I am not good with joining more than 2 tables.
i got upto here
SELECT t2.user_id, sum(t1.total_likes) as image_likes from user_pictures as t2 JOIN (
SELECT sum(like_status) as total_likes, user_picture_id FROM `user_picture_likes` GROUP BY user_picture_id ORDER BY total_likes DESC
)as t1
ON t1.user_picture_id = t2.id GROUP BY t2.user_id ORDER BY image_likes DESC
and how do i proceed from here?
Try this
SELECT T1.Id AS UserID, SUM(T2.like), Tmp.filename AS Image, Tmp.Imageslikes
FROM users T1
LEFT JOIN user_likes T2 ON T1.Id = T2.user_id
LEFT JOIN (
SELECT T3.user_id, T3.filename, SUM(T4.likes) Imageslikes
FROM user_pictures LEFT JOIN user_picture_likes T4 ON T3.Id = T4.user_picture_id
GROUP BY T3.user_id, T3.filename
)Tmp ON T1.Id = Tmp.user_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)
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.
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