I have having problems updating one table from another. I want SQL to update the rows in Employees from the data in CompanyEmployees where the two EmployeeNum fields are the same. Also if an EmployeeNum exists inside of CompanyEmployees that doesn't match one in Employees then I need a new row created in Employees
I so far have tried a join for the two tables.
SELECT Employees.PhoneNum, Employees.Data,
CompanyEmployees.PhoneNum, CompanyEmployees.SystemData
FROM CompanyEmployees
INNER JOIN Employees
ON CompanyEmployees.Employees=Techs.EmployeeNum
I get the right column data in both tables but i doesnt update Employees. Do I need an INSERT or UPDATE somewhere?
How can I insert the whole row of data from CompanyEmployees into Employees where CompanyEmployees.EmployeeNum doesn't exist in Employees?
I need to do this because CompanyEmployees is only a phone directory and Employees has phone numbers and more information. But CompanyEmployees has new hires inside it that are not inside Employees.
Remember RDBMS has the R for relational. They are built for relationships.
To update you Employees table with the CompanyEmployees table you could use something like the below:
INSERT INTO Employees (columns) VALUES (SELECT columns FROM CompanyEmployees) ON DUPLICATE KEY UPDATE
You probably do not even need 'Employees' as it is a superset of 'CompanyEmployees'. I would suggest looking to merge the two tables or at least move the data into a single table.
Related
I have 2 similar pairs of tables (Table1 and Table2, TableA and TableB).
Table1 has many Table2. Table2 belongs to Table1. In other words, there is a one-to-many relationship between Table1 and Table2. TableA and TableB has the same one-to-many relationship as Table1 and Table2.
I would like to move a row in Table1 plus all the associated rows in Table2 to the matching table pair TableA and TableB.
What is the best way to transfer the table row and the associated rows? Do I save each row to the new table one by one using save() or is there a way to save all the rows at one go using saveall()?
Is it a problem to use saveall() to do the data transfer if the table fields are similar but not exactly the same. Between the table pair, some rows are the same and some rows are different.
I am using Cakephp 2.4.5. Thank you for your help.
You'll need to use saveAssociated to solve this, but if your tables are not exactly the same then you will first need to transform your data into something manageable. Here is an example:
$table_1 = $this->Table_1->find('first', array('conditions => array('Table_1.whatever' => 'something')));
// here you can manually build the data you need to save.
$transfer_data['Table_A'] = $table_1['whatever you needed to change'];
You might need a for loop or something to convert your data to the slightly different version that you need for table a and b, but without knowing what the difference is there I cannot write the code that does the transformation. Once the transformation is complete you can just save your save as explained in the manual using saveAssociated.
I have this code to delete data from multiple tables in one go:
DB::table('tb_stikes_register_school')->where('register_id', $_POST['id'])->delete();
DB::table('tb_stikes_register_guardian')->where('register_id', $_POST['id'])->delete();
DB::table('tb_stikes_register_student')->where('register_id', $_POST['id'])->delete();
I'm trying to shorten this into 1 query only, register_id from guardian and school tables is the foreign key of student table. I've been trying to use join but only student table record is deleted. Is there any workaround this?
Something like this maybe - haven't tested
DB::table(DB::raw('FROM tb_stikes_register_school, tb_stikes_register_guardian, tb_stikes_register_student'))
->join(ENTER JOIN INFO) // wasn't clear how your tables were related
->where('register_id', $_POST['id'])
->delete();
Or you could use a fully raw query:
DB::query('SQL statement here');
Basically recreating something similar to this: delete rows from multiple tables
I would like to know the best way to check one table of data, and when no more rows exist matching the WHERE clause, delete a row from another table.
I have tried myself but it has become too cumbersome with 6 queries and nested if/else, and it doesn't work to top it off.
I have never used SQL join's before, so examples will help me to understand responses.
I have a table of devices, there is a master table with a device and a password.
There is a second table containing the multiple rows of the device in the above table, and a series of serial numbers.
When the second table no longer contains any of the serial numbers listed in the master table, I want the row containing the device and password from the master table.
If you mean like when you have a table customer and a table order, delete the customers if they have no orders? Then you can use subselect:
delete from customer where customerid not in (select customerid from order)
You coult make a DELETE statement like
DELETE FROM masterTable WHERE ID NOT IN (SELECT masterTableID FROM secondaryTable)
This would delete all the rows from the master-table which don't have any references in the second table. That also means it would not delete only one row, but all of the matching ones. The only necessary thing you need is that every row in the second table references to the master table.
DELETE table_devices
FROM table_devices
left JOIN serial ON table_devices.id= serial.device_id
WHERE serial.device_id is null
I am about to begin my "Data Auditing" section of my site. What does that mean?
Well basically users can view and update records they have submitted to the database.
This is my first time doing this and I am guessing I will need to use Joins, Selects? etc.
So I was wondering if you could help me with the logic on how I will work through this query. I don't want code as I want to do it myself but the logic is what is bothering me.
Here is an image of my Database schema showing relationships etc:
http://i.imgur.com/Dju0G.png
This is what I have thought how to start (pseudocode):
-Get procedure row where procedure_id = posted value from previous
page.
-Retrieve values from that row in procedure table.
-Get patient row that matches the foreign key value in procedure table.
-Get department row that matches the foreign key value in procedure table.
-Get procedure_name row that matches the foreign key value in procedure table.
-Get dosage row that matches the foreign key value in procedure table.
However after this point I am a bit stuck on how I deal with the Many-to-Many (n--> n) relationship tables , is this where a join comes in?
What about my linking tables how do I tackle them?
If there is an easier way of doing this than what I have thought could you please tell me :)
Links to resources or examples would be much appreciated. Thanks in advance
Thankyou
Take Care!
This is the query for your pseudocode:
SELECT
procedure.*,
patient.*,
department.*,
procedure_name.*,
dosage.*
FROM
procedure
INNER JOIN
patient
ON
patient.patient_id = procedure.patient_id
INNER JOIN
department
ON
department.department_id = procedure.department_id
INNER JOIN
procedure_name
ON
procedure_name.procedure_name_id = procedure.name_id
INNER JOIN
dosage
ON
dosage.dosage_id = procedure.dosage_id
WHERE
procedure.procedure_id = ?
You can join any of your other tables exactly the same way. If you want rows back even if the table you're joining has no matching rows to the rows from the rest of the query, use a LEFT OUTER JOIN instead of an INNER JOIN. Where there are multiple rows matching the current rows (1:N/N:M) you will get as many rows as there are pairs in your result set.
If you need additional guidance/examples you'll have to be more clear about what you want to retrieve and what you don't understand.
I am writing a converter to transfer data from old systems to new systems. I am using php+mysql.
I have one table that contains millions records with duplicate entries. I want to transfer that data in a new table and remove all entries. I am using following queries and pseudo code to perform this task
select *
from table1
insert into table2
ON DUPLICATE KEY UPDATE customer_information = concat('$firstName',',','$lastName')
It takes ages to process one table :(
I am pondering that is it possible to use group by and get all grouped record automatically?
Other than going through each record and checking duplicate etc.?
For example
select *
from table1
group by firstName, lastName
insert into table 2 only one record and add all users'
first last name into column ALL_NAMES with comma
EDIT
There are different records for each customers with different information. Each row is called duplicated if first and last name of user is same. In new table, we will just add one customer and their bought product in different columns (we have only 4 products).
I don't know what you are trying to do with customer_information, but if you just want to transfer the non-duplicated set of data from one table to another, this will work:
INSERT IGNORE INTO table2(field1, field2, ... fieldx)
SELECT DISTINCT field1, field2, ... fieldx
FROM table1;
DISTINCT will take care of rows that are exact duplicates. But if you have rows that are only partial duplicates (like the same last and first names but a different email) then IGNORE can help. If you put a unique index on table2(lastname,firstname) then IGNORE will make sure that only the first record with lastnameX, firstnameY from table1 is inserted. Of course, you might not like which record of a pair of partial duplicates is chosen.
ETA
Now that you've updated your question, it appears that you want to put the values of multiple rows into one field. This is, generally speaking, a bad idea because when you denormalize your data this way you make it much less accessible. Also, if you are grouping by (lastname, firstname), there will not be names in allnames. Because of this, my example uses allemails instead. In any event, if you really need to do this, here's how:
INSERT INTO table2(lastname, firstname, allemails)
SELECT lastname, firstname, GROUP_CONCAT(email) as allemails
FROM table1
GROUP BY lastname, firstname;
If they are really duplicate rows (every field is the the same) then you can use:
select DISTINCT * from table1
instead of :
select * from table1