I have a logs table where I store all kinds of log information. This table contains foreign_table_name and foreign_table_id columns.
When pulling in the logs, to verify that the foreign records still exists, I want to run a "variable" join statement, e.g. like this:
SELECT *
FROM `logs`
INNER JOIN `[logs.foreign_table_name]` ON
`[logs.foreign_table_name].id` = `logs.foreign_table_id`
How can I dynamically "generate" these table names?
Related
I have a MS Access DB that has several tables.
My main table is called Client, which is linked to another table called Abonament, among others.
Both tables share the same field named client_id, which contain identical id's.
Table named Abonament has another field named data_expirarii which contains dates.
How do I delete all the entries from both tables (Client & Abonament) based on a specific date in data_expirarii field, from the Abonament table?
I've managed to delete them from the Abonament table using the Design Wizard and subsequently deleting every entry below or equal to 1-Jun-17 (<= 1-Jun-17) but the client entries remain in the Client table. MS Access gives me a headaque.
First you need to delete the records from the Abonament table.
DELETE *
FROM Abonament
WHERE data_expirarii<=#06/01/2017 23:59:59#
Note: the US format of the date (mm/dd/yyyy)- this format must be used.
Next any client records that no longer have any matching records in Abonament can be deleted. This query will return all records from Clients (written on the LEFT of the join) and only those records in Abonament that have a matching Client_ID - any other record will return NULL in the Abonament.Client_ID field which are filtered for in the WHERE clause and deleted.
DELETE DISTINCTROW Clients.*
FROM Clients LEFT JOIN Abonament ON Clients.Client_ID = Abonament.Client_ID
WHERE Abonament.Client_ID IS NULL
Make sure to take a back-up of your data while testing!
I am currently reading about inner joins and left joins on W3Schools and was wondering how to pull the correct data for a user. From what I have read so far from sql_join_left and sql_join_inner, these joins pull out all data from the tables. The inner joins pull all matching data from both tables while the left join pull all matching data only from the left table. If I have a user register on my application and the user joins a thread and leaves comments, would the only way to retrieve the correct comment history for that user be to send back the users Primary Key as a reference to the users ID after registration, then loop through all the data I get back with a for loop until the users ID matches the ID I set for comment table.
My Users table has columns Primary Key, First Name, Last Name, Password, Email.
My Comments table has the columns, Primary key, Comment, and User ID (this is the primary key the user is assigned after registration and I will be using this column to match with the primary key in the Users table in the for loop. Also since this column is a reference to the users Primary Key in the Users table, it is okay to have multple IDs that are the same which will be referencing the same user?)
Would this be the correct approach to retrieve the correct data for the user or can it be pulled specifically with a query and not after I get a response of all the data and use a loop to match Users Table primary key to the Comments table Users ID
Your tables are correct, so you will have Users and Comments tables, which Comments table will have UserId that referred to Users table primary key.
That's called one-to-many relationship. (mean 1 user can have multiple comments). And it's OK to have multiple userIds in comments table, that referred to 1 single user in Users table.
Depend on the use case you can use join or not, for example, if you already have the user, you want to get all the comment, simply says:
SELECT * FROM COMMENTS WHERE UserId=%UserId%
If, you want to get top 10 comments, with the user Informations, you can use INNER JOIN.
SELECT Users.UserName, Comments.*
FROM Comments
INNER JOIN Users
ON Users.UserId == Comments.UserId
LIMIT 0, 10;
Then you get the comments data along with the corresponded user information.
This answer (https://stackoverflow.com/a/6188334/5580417) provides a really good explanation for MySQL Joins.
For your specific challenge, you do not need to load all comments and then loop through to filter for the user's specific comments. In fact, in this case you don't really need a JOIN. You simply need to query the comments table using your Foreign Key (User ID). A query like this would work:
SELECT User_ID, Comment FROM Comments WHERE User_ID = '165'
This gives you User 165's comment history.
I have 2 sql separated tables in my database:
ds_users, containing: group_id
and
ds_users_data_members, containing: data_gender
I would like to set / Update the group_id to 6 for all data_gender equal to 2.
All this morning i tried to solve this issue , without success.
Please help. Thank you very much
I am assuming there must be a relation between those two tables. Without any relationship you cannot update record in one table by checking condition in another table.
let's say ds_users table has column user_id which is also exist in ds_users_data_members table.
so, you can write following query to update all records in ds_users for data_gender=2 in ds_users_data_members table
SQL SERVER EXAMPLE
UPDATE T
SET group_id=6
FROM ds_users T INNER JOIN ds_users_data_members T1 ON T.user_id=T1.user_id
WHERE T1.data_gender=2
MySQL EXAMPLE
UPDATE ds_users T INNER JOIN ds_users_data_members T1 ON T.`user_id`=T1.`user_id`
SET T.`group_id`=6
WHERE T1.`data_gender`=2;
You can replace the column name of user_id what you have given in your table.
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";
I created a table w_provider_remove to keep the Provider_no and max(prov_effective_Date) of the records in w_providers_load because I have duplicate providers but some have expired. I later try to join the w_providers_load table and the w_provider_Remove table and insert the records into the w_providers_main table. The problem is I get back to many records because it turns out I have more than one record for the particular provider with the same effective date. How can I limit it so it only inserts one of them? Or maybe there is another way to go about this were I do not need 3 tables to accomplish this task
Truncate w_provider_remove;
insert into w_provider_remove
select provider_no as provider_no, max(PROV_DATE_EFFECTIVE) as prov_date_effective
from w_provider_load
group by provider_no;
Truncate w_provider_main;
INSERT INTO w_provider_main
Select l.*
from w_provider_load as l
inner JOIN w_provider_remove as r on l.provider_no = r.provider_no AND l.prov_date_effective = r.prov_date_effective;
If you want to limit the rows in the table to one effective date per provider, then create a unique index:
create unique index w_provider_load_provider_effectivedate on w_provider_load(provider_no, prov_effective_date);
This will generate an error if two records for the same provider have the same effective date in the load.
You can do the same thing for the main table:
create unique index w_provider_main_provider_effectivedate on w_provider_main(provider_no, prov_effective_date);