I have table A and table B and they are both joined through a relationship. table B has a foreign key of table A's key. Table A is joined when the program runs because it is dynamic and could reside in any database. How would I find all the rows in table B that are no longer found in table A?
For ex.
Starting out we have
table A table B
4 4
3 3
5 5
later that evening
table A deleted some rows
table A table B
NULL 4
3 3
NULL 5
I want to get the keys 4,5 so that I can tell what was deleted.
Just use mysql where not in table. I would just use cakephp query method versus find. Here is a topic that already has been answered: Where not in table
Related
Situation:
Old scripts added rows to a table without deleting existing rows.
Need to discover "duplicate" rows (based on matching two fields).
For each set of duplicate rows, sort by ids and return all but the newest one (highest id).
Each row has an associated external file, so can't simply delete the older rows - need to return a list of all the older rows, which will then be processed by a php script.
Example:
TABLE mytable:
ID A B Filename
1 10 abc aa.png
2 11 dddd bb.xml
3 10 abc cc.png
4 10 dddd dd.png
5 10 abc ee.xml
6 11 dddd ff.xml
Rows with IDs 1 & 3 & 5 are duplicates (both A and B match).
Similarly, 2 & 6 are duplicates. Return list (1, 2, 3) - these are the "older" rows that need to be processed.
Even better: return a set of records, containing 'ID' and 'Filename' for those rows.
My primary question is an SQL query that does this, though it would also be useful to me to see how to use the result of that query in php.
There are existing stackoverflow posts related to deleting duplicate rows, but the ones I found delete the rows directly. This won't work for me, as I need to have the external php script delete the corresponding external files:
Deleting Duplicate Rows from MySql Table
How to delete duplicate records in mysql database?
How to delete all the duplicate records in PHP/Mysql
IMPORTANT: The other posts which I quote don't bother to distinguish newer from older; they are about removing fully duplicate records, but that is not my situation. I have records which are partially duplicates; that is, several records match the specified criteria, but there is important information in other fields, hence I have to know which is newest (highest id) for each value of criteria; those are the ones to keep.
I would try this "make sure you test the code before to apply it on production data"
Assuming you have lots of data, I would create temporary table of the data that you want to keep so you can perform the operation fast.
-- Generate a list of the IDs to keep
CREATE TEMPORARY TABLE keepers (KEY(ID)) ENGINE = MEMORY
SELECT A, B MIN(ID) AS ID
FROM table
GROUP BY A, B;
-- Delete the records that you do not wish to keep
DELETE FROM table
WHERE NOT EXISTS (SELECT 1 FROM keepers WHERE ID = table.ID);
If the DELETE query does not work "return an error" about the sub query, you can try this instead of the DELETE query.
CREATE TEMPORARY TABLE deleteme (KEY(ID)) ENGINE = MEMORY
SELECT ID FROM table
WHERE NOT EXISTS (SELECT 1 FROM keepers WHERE ID = table.ID);
DELETE t.* FROM table AS t
INNER JOIN deleteme AS d ON d.ID = t.ID;
To get the data:
Select the records you want to keep (inner query) and join back on itself (outer query) keeping all records and using the dummyfield to find the to be deleted records.
CREATE TEMPORARY TABLE delete_these AS
SELECT *
FROM table a
LEFT JOIN (SELECT MAX(id) as non_deletion_id, 1 AS dummyfield,
FROM table a
GROUP BY your two fields) b ON non_deletion_id=a.id
WHERE dummyfield IS NULL;
I have created two separate tables.
Table 1 (username,email,gender,age)
Table 2(taskname,description,createdon,duedate,workstatus).
Now I want to create a
table 3 (taskname,description,createdon,duedate,staffs,workstatus)
where staffs will show all the registered members' name and admin can select multiple users to allocate in a task.
I am a premature php learner.
Will anyone help me out to solve the problem,please ?
First of all, create two primary keys as user_id and task_id in table 1 and table 2 respectively. Once you are done with it, create table 3 with fields user_id and task_id as two foreign keys referencing table 1 and table 2 respectively and your requirement should get fulfilled.
I have an mysql database named gpstracking.
There are a couple tables in this database: positions and devices.
What is the issue.
On the table positions we have a row called device_id.
On the table devices we have a row called id.
On the table devices we have a row called name.
The device_id and id rows are the same id's, they are pointed to get the name.
My problem is, how to in PHP a loop or something to get the name when compare the 2 id rows.
So example:
device_id row:
1
2
3
4
5
id row:
1 Jan
2 Klaas
3 Piet
So if i fetch the information from table with the device_id row, i want that the script can get the name from the device_id.
Can someone help me ?
You mean this:
Select * from positions
join devices ON positions.device_id = devices.id
I have 2 tables
Table1&Table2
Table1 Contains this Field
1) Field_ID
2) Field_Machine_name
3) Field_Date
And
Table2 Contains this Field
1) Field_ID
2) Field_Machine_name
3) Field_Date_Add
How can I make Field_Machine_name In Table 1
work together with Table 2 in FieldMachine_name
I Want to add a record in Table 1 and automatically its added to Table 2
Until now I have to do it manually in Table 2 So that it Not worth
Thanks to anyone who can help
not much clear with question...think on writing trigger on table 1.So when insert happens in Table 1 it will insert record in table 2 too..
table
ID|owner_id|work_id|lorem|etc|
1 |00123 | 00213 |XXXXX|XXX|
2 |00124 | 00213 |XXXXX|XXX|
owner_id (fk) owners.id (owners[id,name,etc])
work_id (fk) work.id (work[id,name,etc])
question is can I set codeigniter that when I
select(table.*,work.name as work,owners.name as owner) from table
it automatically handle joins since that table already contain the fk-ref ? or I must include join('owner','owner.id=table.owner_id) ?
actually all what I want is that when I select a table that contains a fk this fk column is replaced with one column from ref row by just passing the column name on ref table without having to worry about creating a specific function in my module for that each query.
My current solution:
was to create a view for each table that contain a relation and replace all fk columns with desired ref value, but since i have 6 tables 5 of them with fk,i now have 6 tables and 5 view (11 tables)in db which is really kind confusing for me, so any smarter way to do this ?
I think you are making some confusion on what FK is and what it does within a table.
FK constraint grants data integrity when it's present and relates data within tables. It doesn't join anything.
If you want to select records across related tables, you either use a
SELECT * FROM table1,table2 WHERE table1.K1 = table2.FK1
or
SELECT * FROM table1 JOIN table2 ON table1.K1 = table2.FK1
AND YES, you need to tell CodeIgniter to do those queries