Can I copy something to an other table and also update this in the same table?
Like:
INSERT INTO 'table_new' (name) values ("thomas")
At the same time:
UPDATE 'table_old' set ChangesWereMadeAt = (the date, where the changes were made)
Can I put something in other table, while it stays also in the old table and just updates one column ?
I work with PHP/MySql
use LAST_INSERT_ID();
INSERT INTO 'table_new' (name) values ("thomas");
$last_id_inserted = LAST_INSERT_ID();
UPDATE 'table_old' SET blah='$blah' WHERE id='$last_id_inserted';
Begin by inserting your new line into your table.
Then delete the line from the first table.
And finally copy the line you inserted with this:
INSERT INTO first_table
SELECT *
FROM dues
WHERE id = 5;
Like: INSERT INTO 'table_new' (name) values ("thomas")
At the same time:
UPDATE 'table_old' set ChangesWereMadeAt = (the date, where the
changes were made)
Based on this, I'd say the solution would be to make two requests.
This means... Can I put something in an other table while it stays also in the old table and just updates one column ?
But based on this, I could give a sort of solution.
$reqSelect= 'select * from YourTable
where [insert the condition that would make you select only the data you want to copy]';
$data = mysqli_query($connection, $reqSelect);
$reqInsert='insert into YourDestinationTable(row1, row2, etc)
values('.$data[row1].', '.$data[row2].', '.$data[etc]);
mysqli_query($connection, $reqInsert);
$reqUpdate='update YourTable where [conditions here]
set TheRowYouWantToModify = '.data[TheDataYouWantInYourRow];
mysqli_query($connection, $reqUpdate);
This sounds like it would be easier to solved my a MySql trigger. There is a basic example here: Creating Triggers to add the data into Audit Table
Related
I know I am supposed to create a temporary table based on an existing row, remove the id and then insert an new row in my "real" table based on the temporary table. But it does not work in my prestashop.
$id = 4;
$createTemp = Db::getInstance()->Execute('CREATE TABLE temp_table AS SELECT * FROM my_table WHERE id="'.$id.'"');
$updateTemp = Db::getInstance()->Execute('UPDATE temp_table SET id=NULL');
$insertQuery = Db::getInstance()->Execute('INSERT INTO my_table SELECT * FROM temp_table');
$deleteTemp = Db::getInstance()->Execute('DROP TABLE temp_table');
If I make a var_dump of those I always get a FALSE. I tried to make the select only in a query and it does work so my SELECT * FROM my_table WHERE id="'.$id.'" is correct.
I need to be able to duplicate a row and I could'nt find another/a better way to do so. What do I have to change in my code to make it work ?
If source data is OK your code should work,
I would enable Prestashop mode_dev so you can see the query errors on-screen for better debug.
Since you're working with Prestashop - maybe your "my_table" id is not called id but something different like "id_product" ?
If this is the case, first create will fail and all other queries will fail consequently.
my table has an id field with auto_increment and I want some rows to move to end of the table (changing number id to end number).
Like This:
$query= mysqli_query($con,"UPDATE moshakhasat SET id=<?end table?> WHERE username='$username' ");
sorry about this code. I don't know a lot about PHP
I am tired and really need your help
Thank you.
I'm not sur if you can change that as mysql is handling the auto increment.
But maybe you could insert new row with your data and then delete the old row.
you could use the INSERT... SELECT syntax
INSERT INTO tbl1 (field1, field2, ...)
SELECT tbl1.field1, tbl1.field2
FROM tbl1 WHERE id IN (1,2,3,4);
then
DELETE FROM tbl1 WHERE id IN (1,2,3,4)
This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.
I want to perform a mysql UPDATE query and then get an array of ids that were effected in the change.
This is my update query
mysql_query("UPDATE table SET deleted='1' WHERE id='$id' OR foo='$foo' OR bar='$bar'");
I know that I can do something like this to get the created id from an INSERT query
mysql_query("INSERT INTO table (id,foo,bar) VALUES ('$id','$foo','$bar')");
$newid = mysql_insert_id();
I don't think MySQL has anything like the OUTPUT or RETURNING clauses that other databases support. You can get the list of ids by running a select before the update:
create table temp_table ids_to_update as
SELECT id
FROM table
WHERE (deleted <> '1' or deleted is null) and *id='$id' OR foo='$foo' OR bar='$bar');
Note that MySQL doesn't do an update when the value doesn't change. Hence the first condition -- which you may or may not find important.
Then, to ensure integrity (in the event of intervening transactions that change the data), you can do:
update table t join
temp_table tt
on t.id = tt.id
set deleted = '1';
You could also wrap the two queries in a single transaction, but I think using a temp table to store the ids is probably easier.
I believe the question have emerged as my irritation of doing twice as much work as I could imagine is necessary.
I accept the idea that I could be lacking experience with both MySQL and PHP to think of a simpler solution.
My issue is that I have several tables (and I'd might be adding more) and of these is a parent table, only containing two fields - an id (int) and a name identifying it.
At this moment, I have seven tables with at least 15 fields in each one. Every table has a field, containing the id which I can link to the parent table.
All of these data isn't required to be filled - you will just have to create that one entry in the parent table. For the other tables, I have separate forms.
Now, these forms are made for updating the data in the fields, which means I have to pull out the data from the table if any data is available.
What I would like to do is when I receive the data from my form, I could just use an UPDATE query in my model. But if the table I want to update doesn't have an entry for that specific id, I need to do an insert.
So, my current pseudo code is like this:
$sql = "SELECT id FROM table_x WHERE parent_id = ".$parent_id;
$res = $mysql_query($sql);
if( mysql_num_rows($res) == 1 )
{
$sql = "UPDATE table_x SET ... WHERE parent_id = ".$parent_id;
}
else
{
$sql = "INSERT INTO table_x VALUES ( ... )";
}
mysql_query($sql);
I have two do this for every table I have - can I do something different or smarter or is this just the way it has to be done? Cause this seems very inefficient to me.
Use
INSERT ... ON DUPLICATE KEY UPDATE Syntax
It will insert if record not found,
otherwise, it will update existing record,
and you can skip the check before insert - details
This assuming relation for each 7 table to the parent table is 1:1
Or use REPLACE instead of INSERT - it's an insert, but will do an DELETE and then INSERT when a unique key (such as the primary key) is violated.
in mysql you can do this:
INSERT INTO table
(
col1,
col2
) VALUES(
'val1',
'val2'
) ON DUPLICATE KEY UPDATE table SET
col2 = 'val2'
take a look at the documentation for more information
mysql_query("UPDATE table table_x ..... WHERE parent_id=".$parent_id);
if (mysql_affected_rows()==0) {
mysql_query("INSERT INTO .....");
}