How to fetch data using One to Many relationship using MySql and joins in MySql - mysql

I am creating a demo application. I am stuck in a scenario, I am not getting the exact way and query to fetch data from sql database in the following scenario:
I have a table named RegistrationTable, this table has a column RegistrationId as its primary key. There is another table named ApplicationDetails, this table has a column ApplicationId as its primary key.
I have referenced ApplicationId as Foreign key to RegistrationId column.
My requirement is, single user can apply to multiple jobs. job details will be present in ApplicationDetails table.
How can I check to how may jobs the user has applied based on his email id stored in registration table.
I have a column Status in ApplicationDetails table, where as soon as user applied to a job I am updating the status column.
I am trying the following query but its not working:
SELECT Status FROM ApplicationDetails
INNER JOIN RegistrationTable ON ApplicationTable.ApplicationId = RegistrationTable.RegistrationId
WHERE RegistrationTable.EmailId = "abc#xyz.com";
Can any one please suggest me how can I go about this. I am a beginner to SQL. Please do suggest a way to solve this. Thanks in advance.

You need to change the table name in your query to ApplicationDetails. This is what you mentioned in your post
SELECT Status FROM ApplicationDetails
JOIN RegistrationTable ON ApplicationDetails.ApplicationId = RegistrationTable.RegistrationId
WHERE RegistrationTable.EmailId = "abc#xyz.com";

Related

MS-Access 2016 - update a record based on a record from another table

I'm new to MS access. I have a few tables. Table 'Learners' contains columns such as 'LearnerID' (unique), firstname, surname, status, etc. There's another table (LearnerUpdate) that I created by importing a spreadsheet. The LearnerUpdate also contains a learnerID and their names. I need to match the learner ID and update teh LearnerUpdate table to include the 'status' taken from the 'Learners' table. How do I do it?
I'm not even sure what to google for. The Access terminology is alien to me.
Thank you
Assuming the column you want to update in the LearnerUpdate table is named 'Status', then try to execute this SQL in the Queries table:
UPDATE LearnerUpdate
INNER JOIN Learners
ON LearnerUpdate.LearnerID = Learners.LearnerID
SET LearnerUpdate.Status = Learners.Status

Using Foreign Keys, but showing another column (not id) during a query in MySQL

I have a question regarding foreign keys. I have searched for the answer and was unable to location one.
I have a table 'projects' that has the column 'owner_id' which references 'managers.owner_id' as a foreign key.
Would it be possible to reference 'managers.owner_id' as a foreign key, but show the column 'managers.full_name'? When I run a SELECT query against the 'projects' table, I want to see the manager's name to come up and not the manager's id.
If it is possible, is this normally done with the SELECT command or can I configure it when I CREATE/ALTER the 'projects' table?
I am fairly new with MySQL, thank you for your time and patience!
If what I'm asking seems insane, I wouldn't mind hearing what your thoughts are or if you have any other suggestions.
You just need to join the tables and select the fields that you want e.g.
SELECT projects.project_name, managers.full_name
FROM projects
INNER JOIN managers on projects.owner_id = managers.owner_id
If there are projects where the owner_id is NULL but you still want to list it then use a LEFT JOIN instead.

SQL Database structure - no unique column

I have a problem with this very simple sql statement:
SELECT * FROM video NATURAL JOIN user
I get a warnig ins phpMyAdmin, that there is no unique column in the selection and i cannot edit any values.
But the id column should be unique...
My database structure:
structure 1
structure 2
id in video is a primary key.
id in all other tables are foreign keys pointing to video.id
Could you tell me what is wrong in my structure?
Thanks!
Benjamin
SOLVED:
I created a view from my SQL selection. Inside the view I was able to do changes.

How do delete related data in MySQL tables in a database?

I'm sure that this is a very basic question but, I at a loss and recently starting with MySQL. I have modified, created databases, users, tables, added and modified entries to the table but now I think I need to use a Join here, but I'm not sure.
In the same db I have two tables. The tasks table has two columns of interest user and keyid. While the activities table has one column of interest which is task. What I need to do is delete all tasks in the table tasks for a certain user. However, this also means I need to delete all activities for those tasks. Now the way these are related, is that the keyid value in the tasks table is the value in the task column in the activities table.
My question is how do I write the DROP or DROP + JOIN query to do this?
You could use JOIN on this one like:
DELETE Tasks, Activities
FROM Tasks INNER JOIN Activities
ON Tasks.KeyID = Activities.Task
WHERE Tasks.User = 'User Name Here'
But it would have been better if you use ON DELETE CASCADE when you designed the table so that you will have to delete from the mother table and all the related records of the child table would also be deleted automatically.
See example here.
You could do it in 2 separate queries?
DELETE FROM activities WHERE task IN
( SELECT keyid FROM tasks WHERE user = 'CertainUser') ;
DELETE FROM tasks WHERE user = 'CertainUser' ;

MySQL update table 1 with table 2 data

Sorry for the ambiguous title.
I have two tables:
table 1: mailing_email
table 2 (dynamic table but for now is): membership
table 1 contains a list of all email accounts in the database and few ancillary fields such as name. It also has a column called communicate.
communicate is basically my terminology for subscribed. Any unsubscribe link will set communicate to false.
Both mailing_email and membership have a email and communicate column.
I need to write a query where the following happens:
mailing_email.communicate gets updated to the current status of membership.communicate where mailing_email.email = membership.email. If an email exists in mailing_email which does not exist in membership, the communicate field stays the same.
How would i go about doing this the fastest possible way? Each table will have thousands of rows this sync command would run often.
MySQL offers an update join syntax:
UPDATE mailing_email
JOIN membership ON mailing_email.email = membership.email
SET mailing_email.communicate = membership.communicate