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);
Related
I have two database table.Two table have same structure. Now i want to insert data from old table to new table if data already exists it will update the old data otherwise insert new. I want to insert or update data by matching some column field value. Any help?
You can make use of ON DUPLICATE KEY UPDATE feature of MySQL. From MySQL doc -
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL
performs an UPDATE of the old row.
So, if you have keys defined in the tables you can use this feature. For example, your statement will look like -
insert into target_table (col1, col2, ...)
select col1, col2,... from source_table
on duplicate key update
col1 = values(col1),
col2 = values(col2),
The Best way is you can use the left outer join concept.That will be easy.
INSERT INTO table1
(col_1, col_2, col_3, col_4, col_5) values("","","","")
SELECT
table2_col_1,
table2_col_2,
table2_col_3,
table2_col_4,
1
FROM
table_2 AS t2
LEFT OUTER JOIN
table1 AS t1
ON
t1.col_1 = t2.table2_col_1;
UPDATE table_2
SET table2_col_1 = 'value'// here is the value that you need to implement
WHERE t1.col_1=t2.table2_col_1;//here is your condition
From your Question , i understood that your table2 is not that much important.
So you can drop the values present in the entire table2 ,so that the structure will not get affected.After that was finished.. you can just export the insert query to implement the values that is present in the table1.
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 have problems with the last line of the code:
if(isset($_POST['kolona']))
{
foreach($_POST['kolona'] as $vrednost)
mysql_query("ALTER TABLE tablica ADD $vrednost text NOT NULL");
mysql_query("INSERT INTO tablica ( ".(implode(',',($_POST['kolona']))).") SELECT ".(implode(',',($_POST['kolona'])))." FROM druga");
}
First query is making columns in table 'tablica' and second query suppose to insert values in that columns from all tables from which are the columns, for now it's just hard coded, it's only from table 'druga', but i don't know how to go through the all tables, not just 'druga'. I tried with a loop and also with implode function but nothing seems to be working. Can anyone help?
You can tackle merging problems like this using the ON DUPLICATE KEY feature:
INSERT INTO target (id, column1, column2)
SELECT id, column1, column2 FROM source
ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)
It works well provided you have a PRIMARY KEY column without conflicts between your source and target tables.
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)
I have a table called scheduler_sched which has several columns, including a column called schedule_id.
I need a function where I can pass 2 ids (copy_from_id, copy_to_id) as parameters. And what I need to do is take every row where schedule_id = copy_from_id AND duplicate it but change the copy_from_id to the copy_to_id
So basically I want to to the equivalient of this:
UPDATE scheduler_sched SET schedule_id = 32 WHERE schedule_id = 28
Only I do not want to UPDATE any rows, I want to create duplicates with the new ID's
Does this make sense?
How can I do this?
THANKS!
(By the way schedule_id is not a unique/index field on this table)
Insert into scheduler_sched (column1, column2, column3,schedule_id )
Select column1, column2, column3, 32 from scheduler_sched WHERE schedule_id = 28
I think that ON DUPLICATE KEY UPDATE syntax may help you:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
e.g.:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Just INSERT a new row instead of updating. SELECT first if that schedule_id 28 exists, and if it does, insert a new one with that being the ID.
Since you haven't specified a version of MySQL, I'm going to assume that it is the lastest (5.4).
Assuming I am understanding you correctly, you should be able to implement this using triggers: http://dev.mysql.com/doc/refman/5.4/en/create-trigger.html
One of the benefits of using triggers, is it is all handled by the database itself.