I have added a new column as "DateOrder" to my existing table called "orders" and I need to add (insert) data to the new column.
The only way I have found is using the "Update, Set, Where" syntax, however, it forces me to add values each by repeating the syntax.
I would appreciate if you help me how I can insert all my values at once.
Four rows only? Then use CASE WHEN in the SET clause:
update orders
set dateorder =
(
case id
when 1 then date '2018-10-13'
when 2 then date '2017-08-24'
when 3 then date '2019-01-11'
when 4 then date '2018-02-02'
end
);
Don't store dates as integers. Store them as dates as shown.
Initially, while creating the new column set a default value to it. And see if it has any binding with others. Then after you can directly insert new values to new records and update previous records just by update.
Related
I am working with phpmyadmin. I have one table log in phpmyadmin. I would like to change a value in one column. If I add new value, it must show me Arbeitsplatz 1 instead of 1 in the Station column.
This is my log table. In the third column I would like to replace values for every new entry. For example, if the new value is 3 in station column, it should show me Arbeitsplatz 3 in the log table. If it is 4, then Arbeitsplatz 4 and so on.
How do I implement this?
You probably need to change the column datatype from integer to varchar first, then update that row:
update `log` set `Station` = CONCAT('Arbeitsplatz ', `Station`);
But first, back up that table just in case something fails...
For example if a user inserts '2017-03-13 12:16:18.0' into the timestamp column,
the same user should not be allowed to enter another value in this column IF IT'S ON THE SAME DAY i.e 2017-03-13 (in this case). Or ultimately, update the timestamp column with the previously inserted value ('2017-03-13 12:16:18.0') each time the user tries to insert a timestamp date twice ON THE SAME DAY. I hope I've been explicit enough.
Below is a non-functioning query I came up with, but it shows what I would like the query to do ultimately. Thanks for your help and feedbacks.
INSERT INTO hr.entry(id,entry_time)
VALUES (45,
CASE WHEN '13-03-2017'= CAST(SYSDATE() AS date) THEN
(UPDATE hr.entry
SET entry_time =
(SELECT entry_time
FROM hr.entry
WHERE id=45
AND CAST(entry_time AS date)= CAST(SYSDATE() AS date) )
ELSE
SYSDATE());
You could add a DATE column to your table, and add a unique index to that column. Then, when you insert the timestamp into the timestamp column, you could also insert the date from that timestamp into the DATE column. Attempts to insert a timestamp whose date component already exists in that table would cause MySQL to throw an error.
I think you are going to need a trigger, unless you store the timestamp as a string using YYYY-MM-DD HH:MI:SS format. I don't really recommend that.
So, create a trigger that updates a column called timestamp_date. This simply extracts the date part of the timestamp.
With this column, you can define a unique index:
create unique index entry_userid_timestampdate on entry(userid, timestamp_date);
This will then enforce your condition.
If you decide that you want to store the timestamp as a string, you don't need the trigger (although will need to manually set the "timestamp"). Instead, you can use a prefix:
create unique index entry_userid_timestampstr on entry(userid, left(timestamp_date, 10));
Like:
INSERT INTO `video_play_counts`
(`id`,`video_id`,`date`,`count`,`created`,`modified`)
VALUES
("",1,"2016-12-01",26,"2016-12-03 17:51:53","2016-12-03 17:51:53")
ON DUPLICATE KEY UPDATE
`count` = GREATEST(`count`,VALUES(`count`)),
`modified` = IF(VALUES(`count`) > `count`,VALUES(`modified`),`modified`)
So, I have a unique key on video_id and date and when I make an UPDATE on this unique key, in the case that the new count value is bigger the existing one, I would like to also update the modified field accordingly.
The count field updates as expected but the modified field does not get the new value in the case of an UPDATE.
Please note that I'm using this to do multiple insert/update in one query, just in this example it has only one set of values.
What am I doing wrong ?
I have not tested, but i am nearly sure that you must switch the last 2 line of your query, so that you first set modified and the set countto VALUES(count), else count is set to the GREATEST and never greater count
After inserting new data into a table, I need to select some of the new data straight after, so I can use it for inserting into another table.
The only way I can think of doing this is using the 'datetime' field I have in my row, but how would I retrieve the latest date/time inserted.
INSERT statement with NOW() value for datetime
society_select = SELECT socID, creator, datetime FROM societies.society WHERE datetime='[..datetime is the newest...]';
Hope that makes sense. Thank you
There are a number of ways to do this.
Why not make use of a trigger for this?
When a trigger creates a record you can get the id's of the records inserted. You can then do a select and insert new values into the relevant table.
MYSQL has loads of resources on using triggers.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Or you can get the number of rows affected then use this to get the required result set in a select statement.
Get the last inserted ID?
If you are inserting one row into the database at a time then you would be able to get the last inserted id from MYSQL. This will be the Primary Key value of the record you last inserted into the database.
You would basically do something like this in mysql:
SET #inserted_id = LAST_INSERT_ID();
Or in PHP you can use the function:
mysql_insert_id(&mysql);
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
Sort the results by their datetime in descending order, and select the first of them.
society_select = SELECT socID, creator, datetime FROM societies.society ORDER BY datetime DESC LIMIT 1;
you can use this with an auto increment filed. after inserting data you can retrieve the list inserted id from the table. and use that id to get the latest record.
A trigger as suggested is an option. If you don't want to use that for some kind of reason you can:
Add an integer primary key with auto_increment as ID and sort it DESC (e.g. INT(11))
Sort descending on a timestamp column (ofcourse with an index on it)
Use a trigger after inserting the data. This is for sure the cleaner way.
Another option is to use a method like mysql_insert_id. Assumed that you use PHP. There are of course equivalent methods in other languages as well.
Sorting is not an option(if not wrapped smart in transaction) - If you have multiple writes and reads on the table this might end up pretty ugly.
what if I wanted to update the records in the table by altering values in one of the columns?
I have records in the table that have one column empty(null values). I want to change these values and insert values from another table into those records.
Basically I have a table with one column empty. I do not want to append to the end of the table but start inserting from record 1.
For the existing records, you would have to use UPDATE to update that one column, WHERE thatColumn IS NULL.
Shouldn't the values in that column have some relation to the rest of the record? I could understand initializing the existing records to a non-null value, or using an UPDATE query to populate data from another table in that column, but all related to the original row...
UPDATE old SET old.badColumn = new.newData
FROM oldTable old
JOIN newTable new on old.someID = new.someID
This would find the related data in newTable matching oldTable, and update the badColumn to some data from newTable... let me know if you need more help.
See the "Using the UPDATE statement with information from another table" section from this page of SQL Server Books Online.