How do I insert data while updating? - php

There are two tables called table1 and table2
both table has got same index. when updating table1 and table2 if table2 hasnt got a data and table1 has got the same then it should insert that data to table2. so that both tables got the data and is updated. how can I do this??

create a trigger on table 1 to query for the new row in table 2. if not found, insert.

showing code would help, but one option is to use REPLACE() it will add if needed to replace otherwise, if you really need two identical tables you should look in to replication and stored procedures

Related

Mysql to delete from one table using condition from two tables

I am using this code/query to delete bogus users using a list from 'bogus' table
and Obviously this query is not correct and shows error: Unknown column 'bogus.user' in 'where clause'
Consider that tables sample and bogus have ONLY ONE COLUMN each and I want to delete rows from sample table only retaining the data of table bogus.
delete from sample where sample.user=bogus.user;
How about:
delete from sample where sample.user in (SELECT user FROM bogus);
I think that's the savest way. It's probably possible to put both tables in a single statment without a join or nested select. But If you do that wrong you risk deleting both tables content. Thus I'd say it's better to do it this way.
You need to join for this
delete s from sample s
join bogus b on b.user = s.user
delete from sample where user in (select user from bogus)

Copying tables mysql

I am newbie to sql advance and i am trying to insert rows to one table by selecting from another one. But some bug is coming out.
Here is my problem
So i have two tables
table1 having id(autoincrement), names
table2 having id(autoincrement), names
Now at starting table1 is empty and table2 having 2 rows
1,'myself'
2,'yourself'
So the problem starts here
When i execute following query
Insert into table1 (names) select (names) from table2
So now both rows of table2 must be copied to table1
Ya its working fine.
But what about autoincrement value of id?
By till now table1 autoincrement id should store 3 since next row to be inserted should have id 3
But its not working like expected so table1 autoincrement id stores 4 i.e, 1(current id value)+2*(no of rows inserted)-1
So next time when i execute same query it inserts row with id 4. Skips id=3.
This is problem hope you all got what i am talking about.
Thanks for helping in advance.
Try this
INSERT INTO TABLE1 SELECT * FROM TABLE2
Go ahead an copy your data over, and then you can modify the starting auto_inc to whatever you choose.
ALTER TABLE table AUTO_INCREMENT = 1000;
Alter Table will take around 1 sec as it first creates shadow of the table then the query is processed and if the no. of rows of table goes above then it may take around 20-30 sec. so I dont think that alter is good answer for this problem. Hope someone will help you with insert command such as:
Insert into table1 set id = 1; etc...

Moving data from 1 pair of table to another similar pair in Cakephp

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.

How do I find which of two tables has the row with a given value in a known column

If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;

How to update table1 with content from table2 with php and mysql

I have two tables in my database, table1 and table2. They are identical. But sometimes I change the data in table1.
How do I copy the data from table1 and update table2 to look the same?
I tried REPLACE INTO table2 SELECT * FROM table1 but it works like INSERT and just make new rows instead of updating the existing ones.
For REPLACE INTO to work as intended, the destination table must have a primary key defined, otherwise MySQL cannot determine whether a row already exists and always assumes a new row. As a result, for tables without a primary key, REPLACE INTO acts exactly like INSERT INTO.
Alternatively, you can use two queries, one UPDATE and one INSERT, with appropriate WHERE (NOT) EXISTS clauses. The advantage of this is that it's portable (REPLACE INTO is a MySQL extension).
Another alternative is to run two commands...
truncate table table2;
insert into table2 select * from table1;

Categories