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!
Related
I have a mysql database with 2 tables. The first "spec" is a specification table, the second 'log' is table containing logged entries of previous measurements. Each part being logged is identified by a part number and test measurement. There may be many log entries for any given part number, but only 1 entry per part number in the 'spec' database giving the actual specification. What I need to do is obtain an average of the test measurement for every different part in the 'log' table, and insert this into the 'spec' table as a new specification. The log table will have already have been corrected to remove outliers.
I have been able to update existing records in the 'spec' table, but have been unable to insert records that do not already exist.
This works
update no_flow.spec s join
(select part, round(avg(cc),0) as avgcc
from no_flow.log l
group by part) l
on s.part = l.part and l.avgcc > 0
set s.cc = l.avgcc;
This does not work
INSERT INTO no_flow.spec set (part, cc) s join
SELECT part, avg(cc)
FROM no_flow.log l
WHERE id != 0
values (l.part, l.avgcc);
Suggestions?
If there is a unique index on part in spec you could use INSERT ... ON DUPLICATE KEY UPDATE Syntax
It would look something like this:
INSERT INTO noflow.spec (part, cc)
select
part as logPart,
round(avg(cc),0) as avgcc
from
no_flow.log
group
by logPart
ON DUPLICATE KEY UPDATE cc = VALUES(cc);
This inserts all the records from the inner SELECT into the spec table. When a given inserted record encounters a duplicate key error (i.e. there is already a record for the current part number) the ON DUPLICATE KEY clause updates the existing record by setting its cc column equal to the cc column on the record it was trying to insert.
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?
I have a situation like this:
I have 3 tables, for example:
table phones
table computers
table printers
Every table has the same column named "Address" and every column has the same record "06-00-00-00-00-00" (a duplicate record).
Now, I was wondering if it's possible somehow to check all the records from all of the tables and delete the duplicate records from table "computers" and table "printers" but leave the record in table "phones"
In other words: Delete all the duplicate records from all of the tables except from one chosen table (in this case table "phones").
Thanks a lot.
For deleting records,
DELETE TABLE TABLE1
WHERE ADDRESS = (SELECT ADDRESS FROM TABLE2)
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
I have two tables, one temporary called Persons, the 2nd permanent called employee.
The temporary table is updated by someone every few hours and contains two fields, firstname and lastname.
The permanent table is called employee. It is our permanent record of employees and includes all of their contact information, etc. Importanly, it includes two fields firstname and lastname.
I have this query that shows me if a record in persons matches a record in employees.
SELECT T.FirstName, CASE WHEN P.FirstName IS NULL THEN 'DOES NOT EXIST' ELSE 'DOES EXIST' END
FROM employee T
LEFT JOIN Persons P ON T.FirstName = P.FirstName AND T.LastName = P.LastName
I want something to run within MySQL, on a constant basis and do 2 things:
If a name matches in firstname and lastname in the Persons table and the employee table I want to receive an email that says "Duplicate employee found". AND, I want it to add just those two fields to the employee table then remove the record from the Persons table.
If there is no match, I just want it to add those two fields to a new row in employee table and remove the row from Persons.
I know it sounds backwards but I have thought it through. I can do a query, but I need MySQL to do this automatically on a periodic basis somehow.
Would love your help.
MySQL 5.1 introduced a feature called Events, which allows you to execute a block of procedure code on a schedule (like cron).
See http://dev.mysql.com/doc/refman/5.5/en/events.html