Incorrect SQL Syntax when using JOIN [duplicate] - mysql

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

Related

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

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.

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.

SQL query to select based on many-to-many relationship

This is really a two-part question, but in order not to mix things up, I'll divide into two actual questions. This one is about creating the correct SQL statement for selecting a row based on values in a many-to-many related table:
Now, the question is: what is the absolute simplest way of getting all resources where e.g metadata.category = subject AND where that category's corresponding metadata.value ='introduction'?
I'm sure this could be done in a lot of different ways, but I'm a novice in SQL, so please provide the simplest way possible... (If you could describe briefly what the statement means in plain English that would be great too. I have looked at introductions to SQL, but none of those I have found (for beginners) go into these many-to-many selections.)
The easiest way is to use the EXISTS clause. I'm more familiar with MSSQL but this should be close
SELECT *
FROM resources r
WHERE EXISTS (
SELECT *
FROM metadata_resources mr
INNER JOIN metadata m ON (mr.metadata_id = m.id)
WHERE mr.resource_id = r.id AND m.category = 'subject' AND m.value = 'introduction'
)
Translated into english it's 'return me all records where this subquery returns one or more rows, without returning the data for those rows'. This sub query is correlated to the outer query by the predicate mr.resource_id = r.id which uses the outer row as the predicate value.
I'm sure you can google around for more examples of the EXIST statement

Is this a MySQL JOIN? [duplicate]

This question already has answers here:
Explicit vs implicit SQL joins
(12 answers)
Closed 8 years ago.
SELECT
dim_date.date, dim_locations.city, fact_numbers.metric
FROM
dim_date, fact_numbers
WHERE
dim_date.id = fact_numbers.fk_dim_date_id
AND
dim_locations.city = "Toronto"
AND
dim_date.date = 2010-04-13;
Since I'm not using the JOIN command, I'm wondering if this is indeed a JOIN (and if not, what to call it)?
This is for a dimensional model by the way which is using surrogate and primary keys to match up details
Yes, it is joining the tables together so it will provide related information from all of the tables.
The one major downfall of linking tables via WHERE instead of JOIN is not being able to use LEFT, RIGHT, and Full to show records from one table even if missing in the other.
However, your SQL statement is invalid. You are not linking dim_locations to either of the other tables and it is missing in the FROM clause.
Your query using an INNER JOIN which is comparable to your WHERE clause may look something like the following:
SELECT DD.date, DL.city, FN.metric
FROM dim_date AS DD
JOIN fact_numbers AS FN ON DD.id = FN.fk_dim_date_id
JOIN dim_locations AS DL ON DL.id = FN.fk_dim_locations_id
WHERE DL.city = 'Toronto'
AND DD.date = 2010-04-13
yes, it is joining tables. It is called non-ANSI JOIN syntax when join clause is not used explicitly. And when join clause is used, it is ANSI JOIN
If you reference two different tables in a where clause and compare their referential IDs, then yes, it acts the same as a JOIN.
Note however that this can be very inefficient if the optimizer doesn't optimize it properly see: Is there something wrong with joins that don't use the JOIN keyword in SQL or MySQL? and INNER JOIN keywords | with and without using them

Inexplicable inner query

I have a nested SQL query that is exhibiting results which I can't understand. The query joins the PARTNER and USER tables via the PARTNER_USER table. A partner is basically a collection of users, and the objective of this query is to figure out when the 20th user registered with the partner that has ID 34:
select p.partner_id id,
u.created_on launch_date
from user u join partner_user pu
using (user_id) join partner p
using (partner_id)
where p.partner_id = 34
and u.user_id =
(select nu.user_id
from user nu
join partner_user npu using (user_id)
join partner np using (partner_id)
where np.partner_id = 34
order by nu.created_on limit 19, 1)
However, if I change the 2nd last line to
where np.partner_id = p.partner_id
The query fails with the error message "Subquery returns more than 1 row".
Why does the first query work, but not the second? They look equivalent to me.
Thanks,
Don
JPunyon is right. One or the other query has to run first, and then have its results trimmed after the fact.
If you look at the queries as written, the outer query has to know the result of the inner query to apply its where clause. However, when you specify
where np.partner_id = p.partner_id
in the inner query, then you're trying to make the inner query know the result of the outer query to apply its where clause as well. That's a circular dependency.
As a human, you can read the query and you can tell that in this particular case, you're asking for one particular value in the where clause in the outer query and you're asking to use that same value in the inner query, so it seems as though the database should see that and use the same literal value from the outer query.
In reality, the inner query is simply run first without knowing the possible values of p.partner_id, hence the "multiple rows" error.
Whe you use the = operator to compare with the results of a subquery, your subquery may return only a single row.
if you want to check for all rows that are returned by the subquery, you have to use the IN operator.
AND u.User_Id IN ( SELECT .... )
When you change the where clause in the sub query you're letting the floodgates open. The where clause in the main query doesn't restrict the subquery. So you're getting more than 1 result.
EDIT: What database is this? I've not come across the "using" construct before...
#Jason Punyon
mysql supports the USING construct.