SQL query to get Freinds of Freinds of Freinds (depth 3) - mysql

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

Related

JOIN OR SELECT IN SELECT [MYSQL]

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

Select data if no match found in second table

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)

Selecting from multiple tables with LEFT JOIN

I have 3 tables
t1 (select these records)
-------------
id
offer_id
business_id
t2 (offer details)
-------------
id
offer_details
business_id
t3 (business details)
-------------
id
business_name
I need to select all records from t1 and add information from t2 and t3. Seems basic but I can't seem to be able to get it right -- must be the heat.
SELECT t2.offer_details, t3.business_name
FROM t2
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
should be
SELECT t2.offer_details, t3.business_name
FROM t1
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
Your lead table is t1 and the join should be based on this table
How about this
Select t2.offer_details, t3.business_name
From t1
Left Join t2 ON (t1.offer_id = t2.id)
Left Join t3 ON (t1.business_id = t3.id)
If you want all records from t1, add t1.* on your select part. Assuming that all IDs in t1 exists in the other 2 tables
SELECT
t1.*, t2.offer_details, t3.business_name
FROM
t1
JOIN t2 ON t2.id = t1.offer_id
JOIN t3 ON t3.id = t1.business_id
Modify to LEFT JOIN if the IDs in t1 may be missing in t2 or t3.

What kind of MySQL join do I need?

Simple MySQL tables with matching ID values across tables:
table 1;
pid, firstname
table 2;
id, pid, property, value
Lets say there is one person entry in table 1:
pid: 1
firstname: fred
For each person there are multiple table 2 entries:
pid: 1
property: likes cats?
value: no
pid: 1
property: eye colour
value: orange
pid: 1
property: favourite food
value: sox
I want to select just two of the many table two entries, say the eye colour and favour food entries for a given person ID. What kind of outer join can achieve this?
SELECT `t1`.name
FROM `table1` AS t1
LEFT JOIN `table2` t2 ON `t1`.pid = `t2`.pid
WHERE `t1`.pid = 1
AND `t2`.property = 'eye colour'
I'm stuck here, how to get two rows from table2 and include favour food also? (Before anyone says it, no I can't change the structure of this database).
Thanks for reading.
You need to left join twice:
SELECT
t1.name
,t2a.value as eye_color
,t2b.value as fav_food
FROM table1 t1
LEFT JOIN table2 t2a ON (t1.pid = t2a.pid AND t2a.property = 'eye colour')
LEFT JOIN table2 t2b ON (t1.pid = t2b.pid AND t2b.property = 'fav food')
WHERE t1.pid = 1
How about something like:
SELECT t1.name, t2.property, t2.value
FROM table2 t2
INNER JOIN table1 t1 ON t1.pid = t2.pid
WHERE t2.pid = 1
AND t2.property IN ('eye colour','favourite food')
Or you just wanted the first two however MySQL indexed it:
SELECT t1.name, t2.property, t2.value
FROM table2 t2
INNER JOIN table1 t1 ON t1.pid = t2.pid
WHERE t2.pid = 1
LIMIT 2
There's no real reason to use a LEFT JOIN here, since there should always be an associated pid defined in table1.
How about this?
SELECT t.name, t.value, t3.value
FROM
(SELECT t1.name, t2.value, t1.pid
FROM table1 t1
LEFT JOIN table2 t2 ON t1.pid = t2.pid
WHERE t1.pid = 1
AND t2.property = 'eye colour') t
LEFT JOIN table2 t3 ON t.pid = t3.pid
WHERE t.pid = 1
AND t3.property = 'favourite food'
This will also work for people who don't have an 'eye colour' or 'favourite food' entry
If you want to do this with one query then you should use INNER JOIN but i'm not recommending this. If i were you i would select first the person then i would create another query to recieve the necessary table2 entries. Try both of my solutions you could choose the right one for you.

MySQL sum + inner join query

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