update multiple rows with current rows values mysql - php

I don't know if the title is clear but I'll try to explain it here. We have thousands of record for our database and there are a couple of datetime columns in theses tables. We have to move to int using unix_timestamp but I can't figure out a query in MySQL that would update these fields.
Let's say I have a simple table with a field like that :
user_table :
id : int,
name : string,
date_joined : datetime;
To change all of these date_joined filed to a int I thought adding a int column and copying the date as a int into the new column then drop the old column and rename my new column could be a solution. I built a little php script my script will then make thousands of MySQL query which is quite long. My script would run queries like :
UPDATE user_table SET date_joined_int=UNIX_TIMESTAMP(date_joined) where id = 1
...
Is this possible to do this with one MySQL query?

You right, you should add new INT column
ALTER TABLE user_table ADD date_joined_int INT(11) not null;
Than convert your dates
UPDATE user_table SET date_joined_int = UNIX_TIMESTAMP(date_joined);
And finally remove date_joined column, and remane date_joined_int to date_joined

You're so close. Just drop the WHERE clause to update all the rows in a single query.
UPDATE user_table
SET date_joined_int = UNIX_TIMESTAMP(date_joined)

If you remove the WHERE clause, the UPDATE will be applied to all rows in the table.
UPDATE user_table SET date_joined_int=UNIX_TIMESTAMP(date_joined)

Related

MySQL migrate data from a table with a set field

I have a table with a set column that contains 2 languages and another column on this table containing an order.
Is it possible migrate my table and to create a record in a new table for each set item with a MySQL query?
Directly migrating records works with records that have a set column without a language combination. But if a record would contain a set of languages I am left with one record with an empty column for language. I would preferably want 2 new records inserted into my fresh table.
One for each language in the set.
INSERT INTO newtable (
newtable.oldtable_id,
newtable.language,
newtable.order
)
SELECT oldtable.id, oldtable.languages, oldtable.order
FROM oldtable
WHERE oldtable.order IS NOT NULL;
I doubt it is possible but I would still like to know if somebody might have a workaround for this since I need to migrate the data somehow.
Create a helper table with one field that lists all languages that can be in the set column. The table can be a temporary one that you drop after the migration. Then create an insert ... select ... statement in which you join the source table on the helper table using MySQL's find_in_set() function:
INSERT INTO newtable (
newtable.oldtable_id,
newtable.language,
newtable.order
)
SELECT oldtable.id, helpertable.language, oldtable.order
FROM oldtable inner join helpertable ON find_in_set(helpertable.language, oldtable.languages)>0;

Inserting Columns Without Duplicated Null Data in MySQL?

i don't know mysql very much. And i've problem about that. I've a database and it's 20 GB. I want to combine 4 columns and then move the combined column to the new one. But the problem is duplicated data in the table.
For example i wanna combine;
Column1(Not Null),
Column2(Some of them null, some of them not null),
Column3(Not Null),
Column4(Some of them null, some of them not null).
And my new column, which i want to move my combined columns, is fully empty. After my longly research, at last i find this code on dev.mysql.com
INSERT INTO my_table (new_content)
SELECT Column1
FROM my_table WHERE my_table > 0;
As a result, it moved Column1 to the new_content. But my the other 20 columns were duplicated too, as empty fields. How can i make it in an easy way?
Sorry for my bad English. Thanks in advance.
If you want a resulting column based on the string concatenation fo the column you can use concat
INSERT INTO my_table (new_content)
SELECT concat(Column1 , Column2, Column3, Column4)
FROM my_table ;
Create the column in already existing table:
ALTER TABLE my_table ADD COLUMN new_content VARCHAR(55);
Update table and concate all the columns to the newly created column:
UPDATE my_table SET new_content = CONCAT(Column1, Column2);
Create a trigger for all inserting values in future as well:
CREATE TRIGGER insert_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
SET new.new_content = CONCAT(Column1, Column2);
You can also create a trigger for UPDATE:
CREATE TRIGGER
BEFORE UPDATE ON my_table
FOR EACH ROW
SET new.new_content = CONCAT(Column1, Column2);

Insert distinct records in the table while updating the remaining columns

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.

How can i order by last update row

I have one Sql Query to get all the informations from my table.
I created an list using an foreach.
And i want to order this list, by the last updated row.
Like this
$query - "SELECT * FROM table ORDER BY last_updated_row";
//call Query here
And when i updated a certain row, i want to put this row on the top of the list
I heard about time_stamp, can i use time_stamp for that?
how can i do that?
Thanks
Assuming your using MySQL your table needs to be like this
CREATE TABLE table (
last_updated_row TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
That will give the row a create time stamp and update it on each update statement which effects the row
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
You can use just about any date/datetime/timestamp column in a table to sort by if needed. The only catch is you need to actually have it in the table.
Any of the above will allow sorts by ascending/descending order, but need to be maintained when inserting/updating a row.
Assuming you have the following structure:
table - someTable
id someVale updateTime
1 54634 ......
2 65138 ......
3 94141 ......
4 84351 ......
It doesn't matter what type of column updateTime is - whether it is a date, a datetime, a timestamp, a simple order by updateTime will work.
But you need to make sure that each insert/update you make to that row updates the column so that the sort will be true.

Selecting a single row in MySQL

I am using MySQL, I have a table that has 9 columns. One of them is the primary key.
How can I select a single row, by the primary key or column 8 or 4?
If I understand your question correctly:
SELECT * FROM tbl WHERE id = 123 OR colname4 = 'x' OR colname8 = 'y' LIMIT 1
The 'LIMIT' keyword makes sure there is only one row returned.
select *
from MyTable
where MyPrimaryKey = 123
Columns in SQL don't have a defined 'order'. Database systems generally keep track of an order for display purposes, but it doesn't make sense to ask a database to select a column by number. You need to know the column's name in order to query its contents.
The same thing goes for the primary key (which, incidentally, may not be just a single column). You have to know which column it is, and what that column is named, in order to execute a query.
If you don't know these things, or need to figure them out dynamically, then
DESCRIBE tablename;
will tell you the names of each column, and whether it is part of the primary key or not. It will return a table that you can read, like any other result.

Categories