Comparing two columns in two different tables? - mysql

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.

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 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.

How do I list records in a table only if a field is not present in another table?

First time posting here so please excuse my manners or lack thereof.
My issue is related to a single MySQL database.
How do I use a query to list all records where a specific field is not found in another table's specific field? So far I can use left outer join to show matches but I want to show misses.
select * from TABLE_A
left outer join TABLE_B
on (TABLE_B.id = TABLE_A.field)
where TABLE_B.id is not null;
How do I syntax a query to show records that do not have a match in TABLE_B.field? Leveraging "is null" provides no results.
A left join with where Table_B.id is null should work.
If you are not getting any results, it could be that there is no where the table_B = Table_A.field do not match.
I would suggest looking at your expect data output again more carefully.

How can I refer to a field being referred by the SELECT query?

I have 2 tables, Ongoing_Portfolio and Ongoing_Fees. Ongoing fees contains a foreign key for Ongoing-Portfolio. Below are the tables
Please have a look at the below code
SELECT Ongoing_Portfolio.*,
Ongoing_Fees.Ongoing_Net_Income FROM Ongoing_Portfolio
INNER JOIN Ongoing_Fees ON Ongoing_Fees.idPortfolio = Ongoing_Portfolio.idPortfolio
WHERE Ongoing_Portfolio.idPortfolio = 1
AND
Ongoing_Portfolio.idOngoing_Portfolio = ?
Can you see the ? mark there in the last row? What I wanted to do there is to refer the idOngoing_Portfolio field of the Ongoing_Fees which is being refereed by the query at the moment. In more detail, I need something like below.
AND
Ongoing_Portfolio.idOngoing_Portfolio = idOngoing_Fees of the Ongoing_Fees table where the query is currently accessing
How can I do this in mysql?
The direct answer to your question is to put the column in as you described it:
SELECT op.*,
Ongoing_Fees.Ongoing_Net_Income
FROM Ongoing_Portfolio op INNER JOIN
Ongoing_Fees onf
ON onf.idPortfolio = op.idPortfolio
WHERE op.idPortfolio = 1 AND
op.idOngoing_Portfolio = onf.idOngoing_Fees;
Does this do what you want? If not, you might consider asking another question. Putting the actual create table statements is more useful than a picture of two tables. Sample data and desired results are a big help both for you to understand what you want to do and to convey this information to others.

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