Multiple foreign keys on same column - mysql

I have following tables:
Table: user
Columns
- id
- username
- full_name
Table: pet
Columns
- id
- pet_name
Table: results
Columns
- id
- user_id_1
- user_id_2
- user_id_3
- pet_id
- date
- some_text
Can I make an SQL query witch will give me one row with: id, full_name, full_name, full_name, pet_name, date, some_text where the results.id is 3?

select u1.full_name
, u2.full_name
, u3.full_name
, p.pet_name
, r.date
, r.some_text
from Results r
join User u1
on u1.id = r.user_id_1
join User u2
on u2.id = r.user_id_2
join User u3
on u3.id = r.user_id_3
join pet p
on p.id = r.pet_id
where r.id = 3

Try Following query :
SELECT A.id, B.full_name, C.full_name, D.full_name, E.pet_name, A.date, A.some_text
FROM RESULTS AS A
LEFT OUTER JOIN USER AS B ON A.USER_ID1 = B.ID
LEFT OUTER JOIN USER AS C ON A.USER_ID2 = C.ID
LEFT OUTER JOIN USER AS D ON A.USER_ID3 = D.ID
LEFT OUTER JOIN PET AS E ON A.PET_ID = E.ID
WHERE A.id = 3

Related

Join two table where one table has relation with many table

I have 5 table, and the structure is below --
user - id
- name
- dob
- phone
desi - id
- desgname
- dept_id
dept - id
- deptname
- org_id
org - id
- orgname
junction_table - id
- userid
- desg_id
- dept_id
- org_id
How could I make a joint table?
update
After trying sometime, I got a solution and i posted it as answer
Try this
$this->db->select('your-selection');
$this->db->from('user');
$this->db->join('designation','user.designation_id=designation.designation_id');
$this->db->join('department','user.department_id=department.department_id');
$this->db->join('org','user.org_id=org.org_id');
$this->db->join('designation','user.designation_id=designation.designation_id');
$this->db->where('your-condition`);
$result_set = $this->db->get();
return $result_set->result();
your-selection can be your fields that you want to select.
your-condition can be your conditions in an array.
If I understood well I think you need something like this, join every table to get the info you need.
SELECT
U.name
,D.desgname
,DPT.deptname
,O.orgname
FROM junction_table JT
INNER JOIN [user] U
ON JT.userid = U.id
INNER JOIN [desi] D
ON JT.desg_id = D.id
INNER JOIN [dept] DPT
ON JT.dept_id = DPT.id
INNER JOIN org O
ON JT.org_id = O.id
The Solution is to left join the junction table to user table and then again left join to junction table to other table
SELECT
U.name,
O.orgname,
DPT.dpt,
D.desgname
FROM user U
LEFT JOIN junction_table JT
ON U.id = JT.user_id
LEFT JOIN organization O
ON JT.org_id = O.id
LEFT JOIN tbl_suborg DPT
ON JT.dpt_id = DPT.id
LEFT JOIN designation D
ON JT.desg_id = D.id
Thanks brother for trying to help me.

Inner join 3 tables, retrieve if conditions

I have three tables
Person
- id
- other_fields
PersonTypes
- id
- person_id
- type_id
PersonEmails
- id
- person_id
- email_address
How to get only these email addresses where PersonTypes.type_id is different than value ?
I'm using MariaDB v.10, I tried to use left outer joins with in all tables and then return only these rows where type_id is different. But I'm not sure about the results.
SELECT pe.email_address
FROM Person p
LEFT OUTER JOIN PersonTypes pt ON p.id = pt.person_id
LEFT OUTER JOIN PersonEmails pe ON p.id = pe.person_id
WHERE pt.type_id != 14;
select P1.id,
P2.type_id, -- just for the sake of this output
P2.email_address
from Person P1
inner join
(
select PT.person_id, PT.type_id, PE.email_address
from PersonTypes PT
inner join PersonEmails PE
on PE.person_id = PT.person_id
where PT.type_id <> 14
) P2
on P2.person_id = P1.id
or
select pe.email_address
from PersonEmails pe
where person_id not in
(
select person_id
from PersonTypes
where Type_id = 14
)
The query selects all persons that don't have type_id 14 and joins Person and PersonsEmails tables data
SELECT pe.email_address
FROM PersonTypes pt
INNER JOIN Person p ON pt.person_id = p.id
INNER JOIN PersonEmails pe ON pt.person_id = pe.person_id
WHERE pt.type_id != 14;

Field list in ambiguous in join with 4 tables

Very confused why this is happening since I'm naming the specific tables as es and sub that it's complaining about (and I think that's how you fix that particular issue).
I've got:
users submissions_comments email_settings submissions tables
This query gives me what I want, but I now want to join this on my email_settings and submissions table which both have a user_id.
SELECT user_id,
submission_id,
comment,
users.*,
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
Output:
user_id: 35
submission_id: 12
comment: fdasadfsdadfs
id: 35
email: bobcobb#gmail.com
username: bobcobb
name: Robert Cobb
about:
created: 2014-03-21 20:24:57
last_login: 2014-07-06 23:21:43
public_profile: 1
queued_photo: 0
But if I try to join on another table (which has a reference to a user_id) I get Column 'user_id' in field list is ambiguous
SELECT user_id,
submission_id,
comment,
users.*,
es.*,
sub.*
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
LEFT JOIN submissions sub ON x.parent_id = sub.user_id
LEFT JOIN email_settings es ON x.parent_id = es.user_id;
As you can tell I'm joining all of these on the user_id returned from the inner select (e.g. x.parent_id).
You have to add the alias to the column name:
SELECT sub.user_id,...
SELECT submissions_comments.user_id,
submissions_comments.submission_id,
submissions_comments.comment,
users.*,
es.*,
sub.*
FROM submissions_comments
INNER JOIN
(SELECT submissions_comments.parent_id
FROM submissions_comments
WHERE id = 224) x ON submissions_comments.id = x.parent_id
LEFT JOIN users ON submissions_comments.user_id = users.id
LEFT JOIN submissions sub ON x.parent_id = sub.user_id
LEFT JOIN email_settings es ON x.parent_id = es.user_id;
you need to define which user_id you are referring on the select part as well.

Joining three tables where the term is an exclusion

I have a query that involves 3 tables. The table "project" contains the records I want to retrieve.
3 tables:
project
participants:
id
project_id (refers to id of table 1)
usertable_id (refers to is of table 3)
usertable:
id
lastname
user_type (either "active" or "inactive")
I want to retrieve all the projects (table 1) where the participants (table 2) are NOT of user_type = 'inactive' (table 3)
What is the correct query to join these three tables so that only these projects are retrieved?
select * from projects p
left join participants ps on ps.project_id = p.id
left join usertable u on ps.usertable_id = u.id
where u.user_type <> 'inactive'
Do a simple join query with your where filter
SELECT p.*
FROM project p
LEFT JOIN participants pr ON(p.id = pr.project_id)
LEFT JOIN usertable u ON(u.id=pr.usertable_id )
WHERE u.user_type <> 'inactive'
Edit from comments
SELECT p.*
FROM project p
WHERE NOT EXISTS
(
SELECT 1
FROM participants pr
JOIN usertable u ON(u.id=pr.usertable_id )
WHERE u.user_type = 'inactive'
AND pr.project_id= p.id
)

Friend Table with mysql

I am working the friend table with mysql. I want to get friend userid and username form current username.
friend
=======
- id
- uid
- fid
Sample Data
===========
id uid fid
1 1 2
2 3 1
user
====
- id
- username
Sample Data
===========
id username
1 saturngod
2 snow
3 John
My current code is
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.uid
WHERE friend.fid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
UNION
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.fid
WHERE friend.uid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
It's working. I got the friend list. However, I feel , the sql is so long.
Can I reduce this sql and can we write without UNION in sql ?
You have already joined the tables, so the subselect is not needed.
SELECT u.id,u.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
WHERE u.username='saturngod'
UNION ALL
SELECT u2.id,u2.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
INNER JOIN user u2 ON (u2.id = f.fid)
WHERE u.username='saturngod'
Or you can do:
SELECT
u.id as user_id
,u.username
,u2.id as friend_id
,u2.username as friendname
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username
If you want one row per user you can do:
SELECT
u.id as user_id
,u.username
,GROUP_CONCAT(u2.id) AS friend_ids
,GROUP_CONCAT(u2.username) as friendnames
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username <<-- optional
GROUP BY u.id
SELECT CASE WHEN user_id="$id" THEN friend_id ELSE user_id END AS friendID
FROM user_friend WHERE user_id="$id" OR friend_id="$id" order by friendID ASC