Triple or even quardruple? join - Mysql - mysql

Now this question got me so bad i am even not sure about the question headline.
I have 4 tables (topic t, user u, entry e, user_fav u_f), respectively
topic_id topic_name
1 abba
2 queen
3 rolling stones
user_id user_name
1 a
2 b
3 c
4 d
entry_id entry topic_fk(FK to t.topic_id) user_fk(FK to u.user_id)
1 ..... 1 1
2 ..... 1 2
3 ..... 2 2
4 ..... 2 3
5 ..... 3 4
user_fav_id user_name entry_fk(FK to e.entry_id)
1 a 1
2 a 2
3 a 3
4 c 4
5 d 5
and my question is how i can reach to t.topic_name using u_f.user_name?
An example to be more precise: if i want to use user a,
i need to get u_f.entry_fk (which are 1, 2 & 3)
then i need to get e.topic_fk(which are 1, 1 & 2)
and finally, i need to get t.topic_name (which are abba, abba & queen) (pay attention i need 2 abba's with different entry_ids with them)

You can use sql joins. Try this:
SELECT t.topic_name, e.entry_id
FROM `user` u
LEFT JOIN entry e ON u.user_id=e.user_id
INNER JOIN topic t ON e.topic_id=t.topic_id
WHERE u.user_name='a';

Related

Filtering by comparing 2 rows in SQL

I have a table like this:
item consumerID userID
A 1 1
B 1 1
C 1 2
D 2 2
E 2 2
F 2 3
G 4 4
H 5 6
I want to get all items where consumerID is not like userID grouped by userID
I am currently doing it programatically in PHP but I wonder if it'd be possible to do this with SQL directly. I am using MariaDB.
Desired output:
item consumerID userID
C 1 2
F 2 3
H 5 6
Are you simply looking for "not equals"?
select t.*
from t
where consumerId <> userId;

MySQL query to extract email neighborhood

I have a MySQL table called personxmessage, extracted from emails. It has three fields, id (primary key), MessageID, and PersonID. For each message referenced by MessageID, there are two or more records, each with a PersonID for a person who was included in the email (i.e. in sender, sent to, or cc).
I want to query this table to get a link list showing all the people who were linked to a given PersonID=XXXX, and the links between them defined by being included on the emails that include XXXX, preferably including a weight on the links showing the number of occurrences. Another way of saying this, in graph terminology, is I'm trying to get the neighborhood of XXXX. For example, for entries like
MID PID
1 1
1 2
1 3
2 1
2 2
3 1
3 3
3 4
For PersonID 1 I would like to get link list
P1 P2 Count
1 1 3
1 2 2
1 3 2
1 4 1
2 3 1
3 4 1
Is it possible to do this with some kind of self-join? If so what query would I use? I have done some simpler joins (for example to get the star-graph of XXXX and other PersonIDs that are with XXXX on emails) but this one is over my head.
You can use GROUP_CONCAT do to something sort of like that. It's not a table but a result set of the related pids per mid.
select mid, group_concat(distinct pid order by pid separator ', ') as related_ppl
from personxmessage
group by mid;
result
mid related_ppl
1 1, 2, 3
2 1, 2
3 1, 3, 4
I think this is what you're looking for:
select p.pid as pid1, pp.pid as pid2, count(*) as cnt
from personxmessagep
left join personxmessagepp
on p.mid = pp.mid
and p.pid < pp.pid
group by p.pid, pp.pid;
result
pid pid2 cnt
1 2 2
1 3 2
1 4 1
2 1
2 3 1
3 1
3 4 1
4 1

How to join two tables with two conditions?

I have two tables:
LLOAN
LOANID SOURCEID LOAN_COMPANY ETC
1 1 3
2 1 3
3 1 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
COMPANY
CompanyID CountryID CompanyIDLLAS
1 1 1
2 1 2
3 1 3
4 2 1
5 3 1
6 4 1
And I want to join them. The SourceID refers to the CountryID and the LOAN_COMPANY refers to the CompanyID. Only country '1' has multiple companies, all the others just have one.
How can I join these two tables correctly? I've tried many different things, of which this came the closest:
SELECT Count(c.CompanyID) FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON c.CountryID = l.SourceID AND c.CompanyID = l.LOAN_COMPANY
But it leaves many rows blank. What is the correct way to join two tables with two conditions?
Try below Query:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As LL
LEFT JOIN dbo.Company As C
ON (C.CountryID = LL.SourceID)
AND (C.CompanyID = LL.LOAN_COMPANY)
You can group the condition using paranthesis like this:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON (c.CountryID = l.SourceID) AND (c.CompanyID = l.LOAN_COMPANY)

Mysql returns duplicate entries in simple join

I have 2 tables: course and section as shown in sqlfiddle
I need to select the 'starttime' (or null) for each course's section.
Some courses don't have a 'section' entry.
The query:
select section.id, section.course, section.section, IFNULL(schedule.starttime, "null")
from section
join schedule on schedule.courseid = section.course
where course = 3 and section.section > 0
returns has many duplicate entries as there are in the joined table.
id course section IFNULL(schedule.starttime, "null")
7 3 1 1440021600
7 3 1 1440021620
7 3 1 1440021640
8 3 2 1440021600
8 3 2 1440021620
8 3 2 1440021640
9 3 3 1440021600
9 3 3 1440021620
9 3 3 1440021640
When I expect:
id course section IFNULL(schedule.starttime, "null")
7 3 1 1440021600
8 3 2 1440021620
9 3 3 1440021640
10 3 4 null
11 3 5 null
I tried different combinations of DISTINCT and GROUP BY without success.
Try:
SELECT section.id, section.course, section.section, IFNULL(schedule.starttime, "null")
FROM section
LEFT JOIN schedule ON schedule.courseid = section.course AND schedule.id = section.section
WHERE course = 3 AND section.section > 0
you can use left join then use the schedule.sectionid and section.id
select section.id, section.course, section.section, IFNULL(schedule.starttime, "")
from section
LEFT JOIN schedule ON schedule.sectionid = section.id
where course = 3 and section.section > 0;
SEE DEMO

Take element that are only in one table from a join in MySQL

I have two tables, network_permissions and device_permissions like this:
NETWORK_PERMISSIONS
user_id network_id perm
1 1 3
1 2 4
2 3 3
2 2 1
3 2 1
4 2 3
DEVICES_PERMISSIONS
user_id network_id device_id perm
1 2 2 1
2 2 2 1
Now, I would take only user_id that for the net=2 are in the network_permissions table (within users with perm=3), but not in the device_permissions.
I would in this case the result is: 3 because is the user that have perm!=3 in the network_permission table for the net=2, but is not present in the device_permissions table.
Sorry if it's confused... Thank you for the help.
SELECT n.user_id
FROM network_permissions AS n
Left JOIN devices_permissions AS d ON n.user_id = d.user_id
WHERE d.user_id is null and n.network_id=2 and n.perm=3
Select NETWORK_PERMISSIONS.network_id FROM NETWORK_PERMISSIONS LEFT JOIN DEVICES_PERMISSIONS on NETWORK_PERMISSIONS.network_id = DEVICES_PERMISSIONS.network_id AND NETWORK_PERMISSIONS.user_id = DEVICES_PERMISSIONS.user_id