I have 2 tables table name is users and projects.the structure of table is:
user table
id | name | role
1 | samjad | user
2 | saneer | constructor
projects table
id | name | user_id | constructor_id |
1 | school | 1 | 2 |
How can i get all details from both table in a single row based on project table id.
i want to select
projectname username, constroctorname, user_id, constroctor_id
in a single row
You can join the user table twice - Once as users and then as constructors.
select p.name as projectname,
u.name as username,
c.name as contructorname,
p.user_id,
p.contructor_id
from projects p
left join user u on p.user_id = u.id
left join user c on p.contructor_id = c.id
where u.role = 'user' -- check if the said user has role "user"
and c.role = 'constructor'; -- check if the said constructor has role "constructor"
Do the fact you have two relation between project table and user (one for user and one for constroctor) You can use user joined for two time
select p.name, u1.username, u2.username, p.user_id, p.constroctor_id
from projects as p
inner join user as u1 on p.user_id = u1.id
inner join user as u2 on p.constroctor_id = u2.id
You can use concat() function:
SELECT CONCAT(field1, field2, field3);
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
or CONCAT_WS(separator,str1,str2,...)
Assuming there is a table called Constructor with columns name and constructor_id your query would be
select
p.name as projectname,
u.name as username,
c.name as constructorname,
u.id as userid,
c.id as constructorid
from
projects p
inner join user u on p.user_id=u.id
inner join constructor c on p.constructor_id=c.id
Use a join. You probably want an INNER JOIN:
SELECT * -- actually include only the fields you need
FROM Projects p
INNER JOIN Users u ON u.id = p.user_id
INNER JOIN Users uc ON uc.id = p.constructor_id
You'll want to join the tables on their keys. In this case something like the below:
select p.name as projectname
, u.name as username
, if(u.role='constructor',u.name,null) as constructorname
, p.user_id, p.constructor_id
from users u
join projects p
on p.user_id = u.id;
Related
i have a tables like below
tableName: users
tableName: projects
tableName: user_project
So i want to get users who are not a part of project "1".
i tried like this but no luck
SELECT u.id, u.username
FROM testdb.user_project up
LEFT JOIN testdb.users u
ON up.userId = u.id
WHERE up.projectId=1 AND u.id IS NULL
My expected output
id | username
--------------
3 | u3
4 | u4
can you please help me out of this
Thank you in advance
The below query should work
select u.*
from users u
where u.id not in (select userId from user_project where projectId = 1)
use not exists
select u.* from users u
where not exists ( select 1 from user_project up
where up.userid= u.id and up.projectid=1)
Basically you want the users that are not associated with Project 1 in the relation table. You can do that using "where not exists".
The solution is better, cause exists removes the need to take care of duplicate values, cause one single occurrence is enough to match (or not match) the definition.
select
u.id
, u.username
from users u
where not exists (
select
1
from user_project up
where 1=1
and up.projectID = 1
and up.userId = u.id
)
SELECT u.id, u.username
FROM testdb.user_project up
LEFT JOIN testdb.users u
ON up.userId = u.id
WHERE up.projectId <> 1;
My title isn't great, but let me explain my situation. I have a jobs table. The jobs table has 2 foreign keys to the users table: sales_rep_id and account_manager_id.
Then I have another table called contact_info with a one to one relationship to the users table.
jobs
-----
sales_rep_id
account_manager_id
...
users
-----
first_name
last_name
contact_info
-----
user_id
home_phone
If I want to do a query where I get the phone number for both people on every job I would do the following:
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home FROM jobs
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) reps
ON reps.id = jobs.sales_rep_id
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) account_managers
ON account_managers.id = jobs.account_manager_id
Is there anything I can do to create a temporary table with the joined data? What is the most efficient way to do this join? For example, what if I had 10 foreign keys in the jobs table to the users table, and I needed the phone_number for all 10?
This should be obtain all the info you need
SELECT j.*
, u1.first_name as sales_rep_first_name
, u1.last_name as sales_reps_last_name
, u2.first_name as manager_first_name
, u2.u1.last_name as manager_last_name
, c1.home_phone as sales_rep_home_phone
, c2.home_phone as manager_home_phone
FROM jobs as j
INNER JOIN contact_info as c1 ON j.sales_rep_id = c1.user_id
INNER JOIN user u1 ON u1.id= c1.user_id
INNER JOIN contact_info as c2 ON j.saccount_manager_id = c2.user_id
INNER JOIN user u2 ON u2.id= c2.user_id;
Define a view that joins user and contact_info.
CREATE VIEW user_contact_info
SELECT u.id, u.first_name, u.last_name, c.home_phone
FROM user AS u
JOIN contact_info AS c ON u.id = c.user_id
Then you can use this as if it's a table.
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home
FROM jobs
JOIN user_contact_info AS reps ON reps.id = jobs.sales_rep_id
JOIN user_contact_info AS account_managers ON account_managers.id = jobs.account_manager_id
I have three different tables in MySQL:
given an user id, how can i get a list of role's name of this user?
For example, user_id = 1
I need a list like this (1, deleteuser, modifyuser, viewuser)
How can I construct my SQL command to get such a list?
JOIN the three tables like so:
SELECT
u.id,
r.Name
FROM user u
INNER JOIN user_role ur ON u.id = ur.user_id
INNER JOIN roles r ON ur.Role_id = r.id
WHERE u.id = Someid
However, if you want the list of roles for each user to be concatenated into one string. Use GROUP_CONCAT like so:
SELECT
u.id,
GROUP_CONCAT(r.Name) roles
FROM user u
INNER JOIN user_role ur ON u.id = ur.user_id
INNER JOIN role r ON ur.Role_id = r.id
WHERE u.id = 1
GROUP BY u.id
SQL Fiddle Demo
SELECT User_id, Name
FROM User_role
JOIN Role ON User_role.Role_id = Role.Id
WHERE User_id = '1'
that would create a list like this:
User_id | Name
1 | DeleteUser
1 | ModifyUser
1 | ViewUser
Try this:
SELECT User_id, GROUP_CONCAT(r.Name)
FROM User_Role ur
INNER JOIN Role r ON ur.Role_id = r.Role_id
GROUP BY User_id;
I need help trying to join a table that uses an association table.
I have a users table
Users Table
user_id
user_name
Association Table
user_id
project_id
Project Table
project_id
project_name
I need to pull a user and the count of projects they are associated with.
SELECT u.user_name, COUNT(p.project_id) projects
FROM users u
LEFT JOIN association a ON u.user_id = a.user_id
GROUP BY u.user_name
How do I associate the two tables?
If you want to associate projects and users, you need to do 2 joins:
SELECT u.user_name, COUNT(p.project_id) projects
FROM users u
LEFT JOIN association a ON u.user_id = a.user_id
LEFT JOIN projects p ON p.project_id = a.project_id
GROUP BY u.user_name
If you want it faster you can do:
SELECT u.user_name, COUNT(a.project_id) projects
FROM users u
LEFT JOIN association a ON u.user_id = a.user_id
GROUP BY u.user_name
I think you can do something like:
SELECT
Utbl.user_name,
NumTbl.numProjects
FROM
UsersTable Utbl,
(SELECT
Atbl.user_id,
COUNT(*) AS numProjects
FROM
ProjectTable Ptbl,
AssociationTable Atbl
WHERE
Utbl.user_id = Atbl.user_id AND
Atbl.project_id = Ptbl.project_id) NumTbl
WHERE
Utbl.user_id = NumTbl.user_id
I have a payments table that has the following structure.
**Payments**
id
name
created_by - user_id
closed_by - user_id
**Users**
user_id
name
surname
What is the best way to show both the name and surname of the user who has created and closed the payment file.
The only way i can think this could work would be using a subquery for both(created_by,closed_by fields) thanks
Try this:
SELECT p.id, p.name,
CONCAT (u1.name,' ', u1.surname) created,
CONCAT (u2.name,' ', u2.surname) closed,
FROM payments p INNER JOIN users u1
ON p.created_by = u1.user_id
INNER JOIN users u2
ON p.closed_by = u2.user_id
EDITED: if you want name and surname splitted
SELECT p.id, p.name,
u1.name created_name, u1.surname created_surname,
u2.name closed_name, u2.surname closed_surname,
FROM payments p INNER JOIN users u1
ON p.created_by = u1.user_id
INNER JOIN users u2
ON p.closed_by = u2.user_id
A strict join is not enough, I think you can use joins in the where clause
select name, surname
from
payments p, users u
where
u.user_id = p.created_by
and u.user_id = p.closed_by
You can join to a single table multiple times, but you have to use an alias.
select
p.*,cr.*,cl.*
from
payments p
join users cr
on p.created_by = cr.user_id
join users cl
on p.closed_by = cl.user_id