MySQL Error 1054 Unknown Column 'persons.PersonID' in on clause [duplicate] - mysql

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 6 years ago.
Here i am trying to run one query in MySQL editor and getting one problem
Here is my query
select *
FROM my_db.persons FULL JOIN
my_db.employee
ON persons.PersonID=employee.PersonID;
Any help would be appreciated

MySQL doesn't support FULL JOIN, so perhaps that is the problem. In any case, I prefer shorter table aliases:
select *
FROM my_db.persons p LEFT JOIN
my_db.employee e
ON p.PersonID = e.PersonID;
This, of course, assumes that the PersonID column exists in both tables.
Oh, I see why you got the error. Perhaps this will explain:
select *
FROM my_db.persons full JOIN
my_db.employee e
ON full.PersonID = e.PersonID;
That is, because MySQL doesn't support FULL JOIN, the full is treated as a table alias.

Check if PersonID column exists on Persons table. Make sure the spellings are exactly the same as in the table structure. Also check the case. Some IDE are case sensitive.

Related

Incorrect SQL Syntax when using JOIN [duplicate]

This question already has an answer here:
MySQL Inner Join Query Syntax error
(1 answer)
Closed 4 years ago.
In my DB I have 2 Tables. One for user Login information and one for general information.
Im trying to write a query that will select the column "firstname" from rows where the FK "users_id" is the same as the logged in users ID.
Before doing anything in PHP Im running the query in my database, so the logged in users ID, which would normally be a variable, is replaced with the id of my testuser.
This is my query:
SELECT b6vjp_user_info.firstname
FROM b6vjp_user_info
WHERE b6vjp_user_info.users_id LIKE 243
INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id;
And here is my (censored for security reasons) Login Table named "b6vjp_users":
And here is my other table named "b6vjp_user_info":
The error is:
#1064 - Mistake in SQL-Syntax. 'INNER JOIN b6vjp_users ON b6vjp_user_info.users_id=b6vjp_users.id LIMIT 0, 25' on row 4
Now fyi I translated that, because my work environment is in german. But im sure you know what a Syntax-Error is.
Anyways I checked the JOIN Part of my query over and over again and looked up the JOIN tutorial on W3Schools. But there is no apparent mistake.
Does anybody see what I somehow fail to?
Put the WHERE clause after the (last) ON clause.
I strongly recommend using table aliases. You also need to fix the order of your SQL clauses. So:
SELECT ui.firstname
FROM b6vjp_user_info ui JOIN
b6vjp_users u
ON ui.users_id = u.id
WHERE ui.users_id = '243';
Or, more simply without the JOIN:
SELECT ui.firstname
FROM b6vjp_user_info ui
WHERE ui.users_id = '243';
Notes:
The operand to LIKE should be a string. So, make it a string! Implicit type conversion causes all sorts of problems.
If you are not using wildcards, I think = is more informative. If users_id is really a number, then just use = 243 rather than LIKE.
WHERE goes after the FROM clause. JOIN is an operator in the FROM clause.
The JOIN is not necessary. Unless you are fetching columns from the users table (or need it for filtering which is highly doubtful), don't bother with it.
you have to put where after on clause
SELECT b6vjp_user_info.firstname
FROM b6vjp_user_info
INNER JOIN
b6vjp_users ON
b6vjp_user_info.users_id=b6vjp_users.id
WHERE b6vjp_user_info.users_id =243 // i think it int field so no need to use like operator

MySQL CROSS JOIN : Every Derived Table Must Have Its Own Alias [duplicate]

This question already has answers here:
What is the error "Every derived table must have its own alias" in MySQL?
(4 answers)
Closed 6 years ago.
I am trying to make a CROSS JOIN between "Selected" columns in two tables,
For example : Table 1 (a, b, c) -- Table 2 (a, b, d)
I want to select a and b from each table and then join them, but I keep getting this error :
Every derived table must have its own alias
I know this is because I need to name any derived table, but I still can't figure what's the problem and how to fix it even after searching online. This is the query :
SELECT (x.targetNumber, x.name, x.lat, x.lng)
FROM ((SELECT (u.name, u.targetNumber, u.password)
FROM Users AS u WHERE 'target' = u.type)
CROSS JOIN
(SELECT (l.targetNumber, l.password, l.lat, l.lng) FROM Location AS l)
WHERE (u.targetNumber = l.targetNumber AND u.password = l.password )) AS x;
So far it seems your query is supposed to be this:
select u.targetnumber, u.name, l.lat, l.lng
from users u
join location l on l.targetNumber = u.targetNumber and l.password = u.password
where u.type = 'target';
only that you want to apply some tricks to force the DBMS to follow some execution plan that you consider best. In doing so you make your query completely unreadable and introduce errors.
As you can see there is no cross join. You are not looking for getting all possible combinations of two data sets at all - which is what a cross join is.
And anyway this is not how SQL works. You are to write the query straigh-forward telling the DBMS what to do. The DBMS decides then how to do it.
The short answer is to move the closing parentheses from right after FROM Location AS l) to just in front of the as and remove the alias from the select list of the subquery:
...(SELECT targetNumber, password, lat, lng FROM Location) AS l...
Slightly longer answer: I do not even understand why you have subqueries in the from clause at all. You can simply join these 2 tables.

How do I do a DELETE in MySQL with NOT IN? [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 7 years ago.
I'm getting this error when running the code:
Error Code: 1093. You can't specify target table 'details' for update
in FROM clause
DELETE FROM details WHERE detail NOT IN
(
SELECT detail
FROM user_details
JOIN data ON user_details.data_iddata = data.iddata
JOIN details ON details.iddetails = data.details_iddetails
)
What am I doing wrong here?
I don't have a system handy, but I think the problem is that you can reference the same table in a subquery as the main one you are deleting from. I.e. it doesn't like JOIN details ON details.iddetails = data.details_iddetails in the subquery because that's in the main select clause.
One workaround is to create a temp table, insert the records you are interested in into it, and then find your set from a join off of that.
You're trying to do a DELETE from a table while you're doing a SELECT from it.
In order to get this to work, make a list from your SELECT query and put its results in the parentheses.
If you aren't doing this in an external script, you should define a view that will give you the desired SELECT output.

Comparing two columns in two different tables?

First of all apologies for if the syntax and framing of question isn't up to the standards.
I have a MySql database .I have a table answer which contains idquestion, userAnswer, userEmailAddress as columns.
Another table multi_choice_pool, which contains idQuestion, answer_all.
Every answer.userEmailAddress has multiple entries of idQuestion and userAnswer.
I want to obtain userEmailAddress in answer table where id and answer of that userEmailAddress equals the iq and answer of multi_choice_pool.
I wrote this:
Select answer.userEmailAddress from answer
where (answer.idQuestion=multi_choice_pool.idQuestion) AND
(answer.userAnswer=multi_choice_pool.answer_all);
Which is giving me an error: "Unknown column 'multi_choice_pool' in where clause.
Is the syntax wrong? Or the query is wrong itself? Or my approach isn't right? Can you rectify and provide suggestion?
Select answer.userEmailAddress
from answer left join multi_choice_pool
on answer.idQuestion = multi_choice_pool.idQuestion
and answer.userAnswer = multi_choice_pool.answer_all;
It seems you don't need the WHERE clause.
But you need the JOIN one :
SELECT answ.userEmailAddress
FROM answer answ
LEFT OUTER JOIN multi_choice_pool mcp
ON answ.idQuestion = mcp.idQuestion
AND answ.userAnswer = mcp.answer_all
Here is the MySQL documentation for JOIN.

Everytime i run this query on mysql-workbench i get error [duplicate]

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 8 years ago.
SELECT
first_table.Name,
second_table.Working_hours
FROM first_table
FULL OUTER JOIN second_table
ON first_table.Member_id=second_table.Member_id;
MySQL doesn't support FULL OUTER JOIN. And the error you get if you try can be misleading.
The error is the result of a syntax bug in MySQL. The standard SQL keyword FULL is not treated as a reserved word. Using the keyword FULL therefore acts like a table alias.
It's as if you had written the query like this:
SELECT
first_table.Name,
second_table.Working_hours
FROM first_table AS `FULL`
OUTER JOIN second_table
ON first_table.Member_id=second_table.Member_id;
The error is that OUTER JOIN needs either the LEFT or RIGHT qualifier, but neither is present in this case.