Not unique table/alias - mysql

I get the error ERROR 1066 (42000): Not unique table/alias:
I cant figure out whats wrong with it.
SELECT Project_Assigned.ProjectID, Project_Title, Account.Account_ID, Username, Access_Type
FROM Project_Assigned
JOIN Account
  ON Project_Assigned.AccountID = Account.Account_ID
JOIN Project
  ON Project_Assigned.ProjectID = Project.Project_ID
where Access_Type = 'Client';

Your query contains columns which could be present with the same name in more than one table you are referencing, hence the not unique error. It's best if you make the references explicit and/or use table aliases when joining.
Try
SELECT pa.ProjectID, p.Project_Title, a.Account_ID, a.Username, a.Access_Type, c.First_Name, c.Last_Name
FROM Project_Assigned pa
INNER JOIN Account a
ON pa.AccountID = a.Account_ID
INNER JOIN Project p
ON pa.ProjectID = p.Project_ID
INNER JOIN Clients c
ON a.Account_ID = c.Account_ID
WHERE a.Access_Type = 'Client';

select persons.personsid,name,info.id,address
-> from persons
-> inner join persons on info.infoid = info.info.id;

I had this error, and the cause was an incorrect string concatenation. I was joining two sql string and forgot to put a space between them.

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

Retrieve column names instead of foreign id in MySql

I have a table in MySql Db named as newtbl which has 2 columns referenced as a foreign keys they are m_id, p_id. I want that instead of these m_id and p_id their names should be displayed.
Query which I wrote is below, but it has errors.
select newtbl.*, pa.p_name, m.m_id
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
You can try to column an alias name used as
select newtbl.*, pa.p_name, m.m_id as 'mmID'
from newtbl
left join matches m on newtbl.m_id = m.m_id
left join players pa on newtbl.p_id = pa.p_id
Below is a answer for my question.
select newtbl.u_id, pa.p_name from newtbl left join players pa on newtbl.p_id = pa.p_id

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 combine two tables to get results from different table in MySQL

I have three tables
I am userID: 1231. I am trying to get results of people who have given me access. I mean their marks with name.
So, according to the tables above I should get results as:
To fetch the above result I was trying out few queries. One of them is below:
SELECT a.UserID, a.Mark1, a.Mark2, b.Name FROM details a, profile b WHERE a.UserID IN (SELECT UserID FROM Access WHERE GrantStatus = 'granted' and GrantUserID = '1231');
I get the following results for executing the above query:
Can somebody help me to fix this?
You need a join condition:
SELECT d.userID, d.mark1, d.mark2, p.name
FROM details d JOIN
profile p
ON d.userID = p.userId
WHERE d.userID IN (SELECT userID
FROM access
WHERE grantstatus = 'granted' and grantuserID = '1231'
);
Simple rule: Never use commas in the from clause.
You can do this only with joins:
SELECT d.userID, d.mark1, d.mark2, p.name
FROM details d JOIN
profile p
ON d.userID = p.userId JOIN
access a
on a.userId = d.userId
WHERE a.grantstatus = 'granted' and a.grantuserID = '1231';

How to query a row in MYSQL and join results to a row from another table?

SELECT DISTINCT a.userID, buildstatus
FROM meeting a
WHERE buildStatus = 'complete'
INNER JOIN user.contactName ON (a.userID = b.userID)
Ok so I'm trying to join these two tables where buildstatus from the meeting table is found to be complete it will then find the user id from the user table and join the contact name to this query.
Unfortunately, whatever I try seems to throw up an error message.
Any ideas how to solve this issue?
The order of the query clauses have to be correct. switch join and where
SELECT DISTINCT a.userID, a.buildstatus
FROM meeting a
INNER JOIN `user` b ON a.userID = b.userID
WHERE a.buildStatus = 'complete'