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
Related
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;
I have 2 tables:
posts: userid, lastuserid
users: id, name
I need to join posts.userid = users.id and posts.lastuserid = users.id to get username and lastusername.
My query did as below:
SELECT posts. * , users.name, vUsers.name
FROM posts
INNER JOIN users ON users.id = posts.userid
INNER JOIN Users ON vUsers.id = posts.lastuserid
Is there any other (better) way to do this?
Your query is probably correct. I would encourage you to use table aliases that are abbreviations for the things you are looking for:
SELECT p. * , u.name as username, l.name as lastusername
FROM posts p INNER JOIN
users u
ON u.id = p.userid INNER JOIN
users l
ON l.id = p.lastuserid;
Your query has something called vUsers, which is not defined.
I have two table, users and comments.
In the users table, there're columns id and username.
In the comments table I have user_id and his message.
And I wanted to create a table that select the username and his comment count when I search a particular username.
How do I write this?
my testing attempt:
SELECT COUNT(*) AS comment_count
FROM song_comments
RIGHT JOIN users
WHERE user_id = 7 AND comments.user_id = users.id
Try this:
SELECT U.Username, COUNT(SC.message) AS comment_count
FROM song_comments SC JOIN
users U ON U.id=SC.user_id
WHERE U.user_id = 7
GROUP BY U.Username
This gives you users and count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
group by u.username
You can add a where to get one user's count
select u.username, count(c.user_id) as comment_count
from users u
join comments c on u.id = c.user_id
where u.username = 'Hogan'
group by u.username
I have 2 tables in my database. Table users and table profile.
users(user_id, surname, email)
profile(profile_id, country, user_id)
user_id in table profile, is FK (comes from users table). I have the following query in order to select all surnames "smith" from my database, that are from country "USA". This is my query:
SELECT u.name,
u.surname,
u.email,
u.user_id,
p.user_id
FROM users u
INNER JOIN profile p ON p.country = 'USA'
WHERE u.surname = 'smith' AND u.user_id = p.user_id
this query works fine, but the problem is that returns only 1 result and not all results from my database (people with surname smith that are from USA). Any idea where it might be the wrong and how to correct it?
You should put the u.user_id = p.user_id condition with ON condition because you want to apply JOIN on 'user_id' field. And where clause should have the remaining condition.
SELECT u.name,
u.surname,
u.email,
u.user_id,
p.user_id
FROM users u
INNER JOIN profile p ON u.user_id = p.user_id
WHERE u.surname = 'smith' And p.country = 'USA'
You can read about INNER JOINS
Use only the columns in your join on condition that are relevant for the join. The columns that link 2 tables together. And put the rest in the where clause. That should work:
SELECT u.name,
u.surname,
u.email,
u.user_id,
p.user_id
FROM users u
INNER JOIN profile p ON u.user_id = p.user_id
WHERE p.country = 'USA'
AND u.surname = 'smith'
This is also better for readability. You can read it like this:
SELECT these columns from table users. JOIN also table profile ON this 2 columns. After that filter the data and return only the records WHERE these conditions are met.
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