Inner Join SQL table multiple times for the same table - mysql

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

Related

Syntax error or access violation when trying to select distrinct

I'm honestly not sure why I'm getting an error since I just moved the two selects to be distrinct and in the left outer join but they were working before in the main opening select statement
SELECT body, timestamp FROM chats
LEFT OUTER JOIN messages ON chats.id = messages.chat_id
LEFT OUTER JOIN (SELECT DISTINCT sender.first_name AS sender_first_name, sender.last_name AS sender_last_name,
sender.username AS sender_username) users AS sender ON chats.from_user_id = sender.id
LEFT OUTER JOIN (SELECT DISTINCT reciever.first_name AS reciever_first_name, reciever.last_name AS reciever_last_name,
reciever.username AS reciever_username) users AS reciever ON chats.to_user_id = reciever.id
WHERE from_user_id = :fromUserId AND to_user_id = :toUserId
Also tried doing it with the FROM clause
LEFT OUTER JOIN (SELECT DISTINCT sender.first_name AS sender_first_name, sender.last_name AS sender_last_name,
sender.username AS sender_username FROM users) users AS sender ON chats.from_user_id = sender.id
This is not a valid subquery:
(SELECT DISTINCT sender.first_name AS sender_first_name, sender.last_name AS sender_last_name,
sender.username AS sender_username) users AS sender
You haven't explained what the query is supposed to be doing, but I speculate that you want:
SELECT body, timestamp,
us.first_name AS sender_first_name, us.last_name AS sender_last_name, us.username AS sender_username
ur.first_name AS receiver_first_name, ur.last_name AS receiver_last_name, ur.username AS receiver_username
FROM chats c LEFT OUTER JOIN
messages m
ON c.id = m.chat_id LEFT OUTER JOIN
users us
ON c.from_user_id = us.id LEFT OUTER JOIN
users ur
ON c.to_user_id = ur.id
WHERE c.from_user_id = :fromUserId AND c.to_user_id = :toUserId;
You should not need DISTINCT when joining to a table on the id.

Select Command Where a field must equal a value but another field can have 2 values

I have the following SQL query:
SELECT users.user_id,
users.first_name,
users.last_name,
roles.role,
roles.role_id,
users.username,
users.description,
users_vs_teams.team_id,
teams.team_name,
teams.status,
teams.notes
FROM teams
INNER JOIN users_vs_teams ON teams.team_id = users_vs_teams.team_id
RIGHT OUTER JOIN users ON users_vs_teams.user_id = users.user_id
INNER JOIN roles ON users.role_id = roles.role_id
WHERE( users.role_id = 3 ) AND ( teams.status = 'Completed' ) OR ( teams.status IS NULL )
I want to display only users with a role_id of 3 but team.status can be either Completed or NULL. However, this query displays all roles where teams.status is either Completed or NULL. Any help resolving this issue will be greatly appreciated.
First, I'm not sure if you need an outer join for this. Second, your problem seems to be parentheses in the WHERE clause:
SELECT u.user_id, u.first_name, u.last_name, r.role, r.role_id,
u.username, u.description, uvt.team_id,
t.team_name, t.status, t.notes
FROM teams t INNER JOIN
users_vs_teams uvt
ON t.team_id = uvt.team_id INNER JOIN
users u
ON uvt.user_id = u.user_id
roles r
ON u.role_id = r.role_id ON u
WHERE (u.role_id = 3) AND (t.status = 'Completed' OR t.status IS NULL)
Note that table aliases make the query easier to write and to read.
Remove the RIGHT OUTER JOIN and fix your parenthesis in your WHERE clause.
SELECT users.user_id,
users.first_name,
users.last_name,
roles.role,
roles.role_id,
users.username,
users.description,
users_vs_teams.team_id,
teams.team_name,
teams.status,
teams.notes
FROM teams
INNER JOIN users_vs_teams ON teams.team_id = users_vs_teams.team_id
INNER JOIN users ON users_vs_teams.user_id = users.user_id
INNER JOIN roles ON users.role_id = roles.role_id
WHERE( users.role_id = 3 ) AND ( teams.status = 'Completed' OR teams.status IS NULL)
you can also do something like this:
( teams.status = 'Completed' OR ISNULL(teams.status,'') = '')

MYSQL: Using a join after and AND

the MYSQL query below combines a number of tables. However, as you can see, I would like to add a LEFT JOIN at the end on the receipt table. The query returns an error when I add the LEFT JOIN. Anybody know the best way to LEFT JOIN the receipt table to the rest of the query. Sorry if this is a newbie question. Thanks !!
SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status, receipt_image, expense_comment
FROM users, expenses, merchants, receipts
WHERE ".$adminId." = expenses.admin_id
AND expenses.user_id = users.user_id
AND expenses.merchant_id = merchants.merchant_id
AND LEFT JOIN (receipts)
ON expenses.receipt_id = receipts.receipt_id
Here is a clean approach of doing it, note that I have added alias for the tables for better readability so you may use the alias name in the select statement to fetch the column from the proper table.
SELECT
u.user_name,
ex.expense_category,
mer.merchant_name,
ex.expense_cost,
ex.expense_date,
ex.expense_status,
re.receipt_image,
ex.expense_comment
FROM users u
JOIN expenses ex on ex.user_id = u.user_id
JOIN merchants mer on mer.merchant_id = ex.merchant_id
LEFT JOIN receipts re on re.receipt_id = ex.receipt_id
where
ex.admin_id = '$adminId'
Try this,
SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status, receipt_image, expense_comment
FROM users, expenses, merchants, receipts
LEFT JOIN receipts ON expenses.receipt_id = receipts.receipt_id
WHERE ".$adminId." = expenses.admin_id
AND expenses.user_id = users.user_id
AND expenses.merchant_id = merchants.merchant_id
Use join clauses instead of where clause. I.e.
SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status, receipt_image, expense_comment
FROM users
INNER JOIN expenses on users.user_id = expenses.expenses_id
INNER JOIN merchants on merchants.merchant_id = expenses.merchant_id
LEFT JOIN (receipts)
ON expenses.receipt_id = receipts.receipt_id
WHERE ".$adminId." = expenses.admin_id
Note that any columns from the receipts will be NULL in the select statement whenever there's no matching record.

How to select fields from three different tables in 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..

Select Two Values Based Off Same Column

I am trying to select two usernames in one row of a query one would be called 'username' and the second one say 'username2'. This is the code I have atm. The users table uses a primary key of user_id.
SELECT
r.report_id,
r.poster_id,
r.reporter_id,
u.username,
(2nd username)
FROM reports r,
users u
WHERE r.report_id = :report_id
AND r.reporter_id = u.user_id
AND r.poster_id = u.user_id
you need to join the table user twice
SELECT
r.report_id,
r.poster_id,
r.reporter_id,
u.username AS ReporterName,
b.userName as PosterName
FROM
reports r
INNER JOIN users u
ON r.reporter_id=u.user_id
INNER JOIN users b
ON r.poster_id=b.user_id
WHERE
r.report_id=:report_id
Here is the code for MySQL
SELECT
r.report_id,
r.poster_id,
r.reporter_id,
u.username,
u.username username2,
FROM reports r,
users u
WHERE r.report_id = :report_id
AND r.reporter_id = u.user_id
AND r.poster_id = u.user_id