How to select fields from three different tables in MySQL - mysql

This is driving me crazy, i've spent hours in this and i just don't know what to do:
i have 3 tables:
users
companies
company_permissions
in users i have coduser and username
in companies codcompany and company_name
and in company_permissions i have idcompany_permissions, codcompany, coduser
what i want to do is basically, with one query, get the name of the company to which the given username has permissions to access.
i had this query:
select users.coduser, users.username, companies.company_name
from users
inner join company_permissions on users.coduser = company_permissions.coduser
inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
But it doesn't return anything :C (it did at one point but now it doesn't do it)

there is no error with your query
check the database if the user 'test' exists
if it's exist because you using inner join it must be records in all three tables
check them again

I think for this case, you should use LEFT JOIN instead of INNER JOIN.
Anyway, WHERE users.user = 'test', I guest the field must be username.

Test your statement step by step
1.)
select users.* from users
--inner join company_permissions on users.coduser = company_permissions.coduser
--inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
is there a result... user-data is exist
2.)
select users.*
from users
inner join company_permissions on users.coduser = company_permissions.coduser
--inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
is there a result... the join between company_permissions and users is ok (data exist)
3.)
select users.*
from users
--inner join company_permissions on users.coduser = company_permissions.coduser
inner join companies on companies.codcompany = company_permissions.codcompany
where users.username = 'test'
is there a result the join between companies and company_permissions is ok..

Related

Inner Join SQL table multiple times for the same table

I have two tables (msg and users). I am trying to join these two tables together and print out the first and last name of the sender and the recipient as well as all of the message content if the message is marked as flag. I keep getting errors though, and have not found a good way to do this yet.
Pictures of tables for reference: https://imgur.com/a/wYuptfR
SQL query I'm using right now:
SELECT msg.*, users.uuid AS users.ruuid, users.uuid AS users.suuid, users.firstName, users.lastName
FROM msg
INNER JOIN users ON users.ruuid = msg.recipient
AND INNER JOIN users ON users.suuid = msg.sender
WHERE msg.flag = 0
You can query the same table several times if you use different aliases. Also, note you shouldn't have an and between the two join clauses:
SELECT msg.*,
r.uuid AS ruuid, r.firstName AS rfirstname, r.lastName AS rlastname,
s.uuid AS suuid, s.firstName AS sfirstname, s.lastName AS slastname
FROM msg
INNER JOIN users r ON r.uuid = msg.recipient
INNER JOIN users s ON s.uuid = msg.sender
WHERE msg.flag = 0
Try This
SELECT msg.*, u.uuid , u.firstName, u.lastName, s.uuid , s.firstName, s.lastName
FROM msg
INNER JOIN users u ON u.uuid = msg.recipient
INNER JOIN users s ON s.uuid = msg.sender
WHERE msg.flag = 0

Many to many join

I have this
and I am trying to select all users that are friends with a user given by username.
I tried something like this
select friends.id_friend
from friends inner join users on users.id = friends.id_user
where users.Username = 'Dani'
but I want access to the fields from users table.
Youre going to need to join to the user table twice, like this (change SELECT to fit your needs):
select user.*, friend.*
from friends user_friend_link
inner join users user on user.id = user_friend_link.id_user
inner join users friend on friend.id = user_friend_link.id_friend
where user.Username = 'Dani'!
In this case, friends is a linking table between records in the users table. It associates one user record with another user record. To get user record info on both entities in the link table, you have to join it to both linking ids.
SELECT friends.ID_friend, u.*
FROM friends
INNER JOIN users
ON users.ID = friends.ID_User
LEFT JOIN users u
ON u.ID = friends.ID_friend
WHERE users.Username = 'Dani'

SQL query to link two tables returning error

I have two tables as follows
user [ ID , username ]
relationship [ user1_id(FK) , user2_id (FK) , status ]
I am trying to get the username by using either user1_id or user2_id where the status = 1 from relationship table. user1_id and user2_id are both IDs from the user table. The following query is failing and I am not sure where it's going wrong.
SELECT
U.username,
(R.first_user_id, R.second_user_id AS friends)
FROM
user U,
`relationship` R
WHERE (R.`first_user_id` = {$userID} OR R.`second_user_id`)
AND (`status` = 1 AND U.ID = friends)
returns both names of users in a relationship with a status of 1.
this also assumes that if a relationship record exists, both users must be in the user table.
SELECT U1.UserName, U2.username
FROM Relationship R
INNER JOIN USER U1
on R.User1_ID = U1.user_ID
INNER JOIN USER U2
and R.User2_ID = U2.user_ID
WHERE R.Status=1
It looks like you may be trying to get the usernames of all users that have a relationship with a certain specified user, regardless of the order of user IDs in the relationship record. That could be this:
SELECT
U.username,
U.first_user_id,
FROM
user U
JOIN `relationship` R
ON R.first_user_id = U.ID
WHERE
(R.`second_user_id` = {$userID})
AND (`status` = 1)
UNION ALL
SELECT
U.username,
U.second_user_id,
FROM
user U
JOIN `relationship` R
ON R.second_user_id = U.ID
WHERE
(R.`first_user_id` = {$userID})
AND (`status` = 1)
If that produces duplicates (or could do) and you don't want it to do, then change the UNION ALL to a straight UNION.
I have succeeded based on xQbert answer:
SELECT
U1.username,
U2.username
AS user_friend
FROM
Relationship R
INNER JOIN
user U1
ON
R.first_user_id = U1.ID
INNER JOIN
user U2
ON
R.second_user_id = U2.ID
WHERE (R.`first_user_id` = {$userID} OR R.`second_user_id` = {$userID})
AND `status` = 1

Mysql database selecting with relation

I have this table that I hold userlist for my msn application. There's another table for the friendship where it has two foreign keys from userlist.
user: id, name, online, ip...
friend: id1, id2
I want the information of the users that are friend with a specific id.
I'm using this sql query:
SELECT (latest_ip, email, online, pass, status)
from im.user JOIN im.friend ON user.id = friend.id1
WHERE user.id = 5
what am i missing?
remove the parenthesis on your select clause, you don't need them
SELECT latest_ip, email, online, pass, status
from im.user
INNER JOIN im.friend
ON user.id = friend.id1
WHERE user.id = 5
UPDATE 1
you need to have extra join with the table user again since you want to get the informations of the user's friends.
SELECT c.*
from im.user a
INNER JOIN im.friend b
ON a.id = b.id1
INNER JOIN im.user c
ON b.id2 = c.id
WHERE a.id = 5

MySQL Nest Join

I have a users table which contains the users information (fname, lname...etc) and a invoices table. In the invoices table I have a field called created_id which links to the user.id that created the invoice. I also have a field called staff_id which links to the staff user.id that approved the invoice.
How can I query the first and last name for both the created_id and the staff_id in a single query? Here are a few things I've tried....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname
FROM
invoices
INNER JOIN users
ON users.id = invoices.created_id;
This works, but it only gets me the person's name that created the invoice. How can I add the staff's name to that as well....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname,
users2.fname as staff_fname,
users2.lname as staff_lname
FROM invoices, users
LEFT JOIN
invoices,
users AS users2
ON
users.id = invoices.created_id,
users.id = users2.id
That doesn't work, but is closer. Any guidance or examples would be very helpful. Also, if you have any recommendations for good books on learning how to do more advanced MySQL queries that would be helpful too.
You need to join users table twice on table Invoice.
SELECT a.*,
b.fname created_firstName,
b.lname created_LastName,
c.fname staff_firstName,
c.lname staff_LastName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id
and best thing is you can concatenate their names into one using CONCAT
SELECT a.*,
CONCAT(b.fname, ' ', b.lname) created_fullName,
CONCAT(c.fname, ' ', c.lname) staff_fullName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id