MYSQL: Left join same table twice with same field [duplicate] - mysql

This question already has answers here:
MYSQL: How to JOIN two tables on the same query referencing the same table twice
(3 answers)
Closed 4 years ago.
I have a users table that stores admin and normal customers:
ID Name Role
1 John Admin
2 Sara User
3 Pete User
4 Nick User
Another Table that stores requests:
ID Updatedby Createdby DateCreated DateUpdated
1 1 2 10/12/2017 21/01/2018
2 1 1 11/12/2017 22/01/2018
3 1 3 12/12/2017 23/01/2018
4 2 2 13/12/2017 24/01/2018
I am trying to create a view that looks like this:
ID Updatedby Createdby DateCreated DateUpdated
1 John Sara 10/12/2017 21/01/2018
2 John John 11/12/2017 22/01/2018
3 John Pete 12/12/2017 23/01/2018
4 Sara Sara 13/12/2017 24/01/2018
The problem I am facing when creating the query is, I am trying to left join to the user table twice. How do I specify whether the Users.Name is for Updatedby or Createdby? This is what I mean:
SELECT
request.ID,
users.Name, /*name for Createdby */
users.Name, /*name for Updatedby */
requests.DateCreated,
requests.DateUpdated
FROM
requests
LEFT JOIN
users ON requests.Updatedby = users.ID
I can't get my head around the logic with this.

this is an example:
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
you can use Alias

Related

Left Join and remove duplicates

I have three tables, clients, job_allocations and jobs table. I want to select all clients that are not in a particular job, below are my tables.
Clients table
id
Fullname
1
John Doe
2
Jane Doe
3
King James
4
Jere Gray
Jobs table
id
Title
1
Road Construction
2
Repair of Engines
job_allocations table
id
client_id
job_id
1
2
1
2
2
2
3
1
2
4
3
2
I want to select all clients that are not in job_id=2, but when I ran my query, I am getting client id: 2 - Jane Doe again, please how do I solve this?
I did this:
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id
WHERE job_id <> 2 OR job_id IS NULL```
You can use a NOT IN clause as follows:
SELECT *
FROM clients
WHERE id NOT IN (SELECT client_id
FROM job_allocations
WHERE job_id = 2)
Check the demo here.
So you will fetch all clients, but only jobs related to job_id <> 2
This query should work for you:
SELECT client.*
FROM clients
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id and job_id <> 2
Use DISTINCT keyword for selecting unique values

MYSQL - Select row even if row does not join

I am trying to find one query that returns all people including the company they belong to and companies that do not have any person assigned yet.
Company
cid | cname
--------------
1 Company 1
2 Company 2
Person
pid | pname | fk_company
---------------------------
1 Person 1 1
2 Person 2 1
desired result
pid | pname | fk_company | cid | cname
----------------------------------------------
1 Person 1 1 1 Company 1
2 Person 2 1 1 Company 1
NULL NULL NULL 2 Company 2
Thanks in advance
If you want everything from both tables, regardless of match left AND right, you need a FULL JOIN:
SELECT *
FROM person
FULL JOIN company
ON person.fk_company = company.cid
edit: Apparently mysql doesn't support FULL JOIN. You'll have to do both LEFT JOINS by hand and UNION ALL them.
You should mention something you tried. Anyway, I will explain the method so you can work on it.
SELECT <column_names>FROM <table1_name> LEFT JOIN <table2_name>ON
<table1.column_name> = <table2.column_name>;
For more explanations please refer this link.
SQL Left Join

MySQL IN operator of result from another column

I have 2 tables:
users(uid, name, titles)
titles(uid, name)
users:
uid | name | titles
1 David 2,4
2 John 5
3 Jane 4
titles:
uid | name
2 Owner
4 CEO
5 Manager
The question is how do I select something like this:
SELECT u.* FROM users as u
JOIN titles as t
ON t.uid IN (u.titles)
WHERE t.uid=2
Notice the IN(u.titles)? It's only taking the first title uid in u.titles field. That means when I change condition to WHERE t.uid=4, it shows no records.
Any idea?
SELECT u.*
FROM users as u
JOIN titles as t ON find_in_set(t.uid, u.titles) > 0
WHERE t.uid=2
If you want that each user can have multiple titles I would recommend a reference table which references the users to the titles.

Three table with value join query base value is first ,secound and third table

i need to develop the query with join or any thing i can't make it
my concept is the first table have a usr id and usr type id
the second table have details of usr type and id and usr master details id
the third table have usr master details and id my question is how i select the value of those three table
Sample table is
user_details
usr_id Name us_ty_id
25 john 2
34 sam 3
24 rose 1
user_type
us_ty_id type usr_ma_id
1 dev 2
2 desi 1
3 test 2
user_master
usr_ma_id details
1 team1
2 team2
3 team3
my output like below
usr_id type details
34 test team3
the first table us_type_id find the type and usr_ma_id in second and find the details of selected id of usr_ma_id in second table and find the details
ple help me...
What about the simple:
SELECT *
FROM user_details d
INNER JOIN user_type t ON t.us_ty_id = d.us_ty_id
INNER JOIN user_master m ON m.usr_ma_id = t.usr_ma_id;

Count Memberships of User in Groups

I've a tree structure of an association which is divided in divisions, subdivisions etc. on every level users may have memberships to certain roles.
I want to count the memberships on every "structure type" (association, division, subdivision) as defined in the table
The Table structure looks like:
table intern_structures
Contains the hierarchy (nested set, but that does not matter here)
id | intern_structure_type_id | name | parent_id | lft | rgt
1 1 My Company USA 0 1 6
2 2 Texas 1 2 5
3 3 El Paso 2 3 4
table intern_structure_types
Contains Description to the types like "association", "division", "subdivision"
id | name
1 Association
2 Division
3 Subdivision
table memberships
Contains the memberships
id | user_id | intern_structure_id | role_id
1 1 1 1
2 1 2 2
3 2 3 1
3 2 3 3
....
table roles
Contains role descriptions
id | name
1 Admin
2 Moderator
3 Clerk
I want a grouped list like:
structure_type_name | role_name | count of memberships
Association Admin 1
Association Moderator 10
Association Clerk 0 << !! I miss the zero rows!
Division Admin 7
Divison Moderator 43
Division Clerk 31
Subdivision Admin 234
Subdivision Moderator 942
Subdivision Clerk 456
What I achieved so far is this query:
SELECT
is_types.name,
roles.name,
COUNT(memberships.id)
FROM
roles,
intern_structure_types AS is_types
LEFT JOIN intern_structures AS is_elements ON is_elements.intern_structure_type_id = is_types.id
LEFT JOIN memberships ON memberships.intern_structure_id = is_elements.id
WHERE
roles.id = memberships.role_id
GROUP BY
is_types.id, roles.id
It works fine except that it doesn't list all roles because some roles don't have any memberships yet but I want them listed as well just with 0 as membership count.
I'd be very thankful for any help!
I'm assuming the counts you showed in the OP are contrived. To get the results you want, you should create a derived table of the types and roles in use and then left join that entire query to a cross join of the roles and types.
Select is_types.name
, roles.name
, Count(Z.is_type_name)
From roles
Cross Join intern_structure_types As is_types
Left Join (
Select is_types.name As is_type_name
, roles.name As role_name
From intern_structures As is_elements
Join intern_structure_types As is_types
On is_types.id = is_elements.intern_structure_type_id
Join memberships
On memberships.intern_structure_id = is_elements.id
Join roles
On roles.id = memberships.role_id
) As Z
On Z.is_type_name = is_types.name
And Z.role_name = roles.name
Group By is_types.name, roles.name