Hi I know how to upload data from one table to another one:
INSERT INTO new_table (id, pricerange, rentrange, date)
SELECT id, price, rent, date
FROM initial_table
But I have a problem with this if you consider I have 10 rows in my initial table by this code I can upload all of them into new table and then after for example 10 hours if I have 10 new rows and I use this code in my new table I will have 30 rows. Because this code did not delete 10 old row and also add all 20 rows again. what can I do that not to upload first 10 row again?
You can use this query. This will insert only those rows which are not present in first table:
INSERT INTO new_table(id, pricerange, rentrange, date) SELECT id, price, rent, date FROM initial_table WHERE NOT EXISTS(SELECT * FROM new_table WHERE (initial_table.id=new_table.id))
You can add more condition in "if" statement if you want
Set a flag variable in the database. By default set the value as 0. If the data uploaded then set that value to 1. When you are upload one table to another check the flag value. Upload only those value which are 0.
Is there a primary key defined? Or if you can have a time stamp field / system change number on the table you can capture the scn / time stamp in a separate table after your first insert and get the records from base table by comparing the time stamp against the preserved one.
Related
I want to store the average values from one table to another table.And also I need to store the table name in another column along with its average data using mysql.Here below is the table I need to have.
Need to add test table's average values into "avg table" along with the test table name as a test id for the second table
I dont know how to add tables in here.
Test 1 table
AvgData table
I think you want to aggregate by date and then take the averages of the temperature and humidity columns:
SELECT
DATE(Time) AS Date,
AVG(Temperature) AS Avg_Temperature,
AVG(Humidity) AS Avg_Humidity
FROM Test1
GROUP BY
DATE(Time);
I am trying to update a table called "Orders" when a row on another table (called "store") is more than 30 days old.
When a value on "store" is created, it is given a timestamp of the time it was added to the table. But as soon as that value turns over 30 days old, I need it to get the ID (which is a column that has a unique ID per row) of that cell, and check the table called "orders" for that ID, and update another column where that ID is present.
So far, I've written this, but it is clearly wrong:
<?php
include ('connectdb.php');
$query = $db->query("SELECT * FROM store WHERE open_time < (NOW()- INTERVAL 30 DAYS)");
$update = $db->prepare("UPDATE orders SET notified=-1 WHERE unique_id=$query");
$db = null;
?>
If the tables are on the same database just use a subselect in the WHERE clause, like here: MYSQL UPDATE with IN and Subquery
Otherwise aggregate the IDs into an array and concatenate them in the WHERE clause with implode. Don't forget to quote every ID.
Also, try to select only the columns you need (unique_id) and not all at once (*).
i am trying to develop php simple script that can be entered records like this: (mysql is my db engine)
id (auto increment, primary key)
StudentName
StudentNumber
ClassName
Grade
ScreeningDate (mm/dd/yyyy)
etc.
now what I need is to prevent the StudentNumber to be entered in the same day, for example, if other user has entered it already, then a message says: this Number was already added for today...
In other words, i need before the insert, to check if the studentNumber is there, then give the message, otherwise, will add the row normally...... hence, next day, it is okay, they can add same studentNumber again like yesterday.... something like PrimaryKey but only for today! how is this possible?
Create a unique compound index on (ScreeningDate, StudentNumber). Then INSERT operations that would place duplicates into your table will fail with duplicate-key errors.
You will need to detect this duplicate-key situation in your PHP code and return the appropriate message to the user who attempted to insert the dup. The INSERT statement will return an error.
Or, you can use INSERT ... ON DUPLICATE KEY UPDATE if you want to update the row if it already exists. Read this. http://dev.mysql.com/doc/refman/5.6/en/insert.html
To create the index, do this:
ALTER TABLE whatever ADD UNIQUE INDEX NoDailyDups (ScreeningDate, StudentNumber)
This approach has the advantage that it works correctly, without explicit table locking, even on a very busy system. If two users happen to be racing to insert the first item for a particular student and day, one of them will win and the other will get the duplicate-key error.
Notice also that your table is slightly denormalized -- it contains slightly redundant data. You might want to create another table containing the columns StudentNumber and StudentName, and move the student names out of this table.
Do a select to get the specified student number on today's date like this:
SELECT * FROM table WHERE StudentNumber = 123 AND DATE(ScreeningDate) = CURDATE()
If any rows are returned, there's a record for this student number on today's date, otherwise you are free to insert. The DATE function extracts the date part of a date, i.e. the date month and year
This may not be efficient but you can fetch all the rows of current date first:
SELECT StudentNumber, ScreeningDate FROM tablename WHERE StudentNumber = '22' AND ScreeningDate = 'mm/dd/yyyy';
And don't add(insert query) if the above query returned more than 0 rows(it will be one generally).
I am trying to write a single MySQL insert query that will identify the highest value in a column and then increment it by one for the record being inserted. I thought when I made the table that I had this field set to auto_increment but it does not work for some reason. My current insert statement is:
INSERT INTO victoria (name, album, order_by) VALUES (:name, :album, :order)
The order_by field is the one that needs to increment by one.
If you want, for some reason, to increment a value of a column without using an auto_increment column you can do something like this
INSERT INTO victoria
SELECT :name, :album,
COALESCE((SELECT MAX(order_by) FROM victoria), 0) + 1;
Note: it might fail to provide you with a distinct value under heavy load, meaning several concurrent users inserting rows at the same time can grab the same MAX(order_by) value. Therefore if you are not planning to "reorder" rows latter you better stick with an auto_increment column.
Here is SQLFiddle demo
I have a number which is in this form : 2012-01 (2012 as current year) and 01 is just a the maximum value of a field in my database incremented by 1, and each year that number is reset to 0.
but if there are two users that try to do the same operation at the same time the value is the same for both and thus i get the same number inserted twice in my database .
I thought of creating a sequence but that requires a job that resets the sequence each year and i would prefer if there is a way to make a lock before i get the next number and
release it after an insert is done ?
Thanks.
CREATE UNIQUE INDEX index_name ON table_name (column_name);
or
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);
You don't specify where you store the field that is used as the counter. But maybe it is possible to use a SELECT FOR UPDATE statement.
Before you increment the value of your counter field by 1 you can lock that record by using a SELECT FOR UPDATE. Then update the counter.
Something like this, assuming the table has only 1 record:
SELECT *
FROM CounterTable
FOR UPDATE;
UPDATE CounterTable
SET Counter = Counter + 1;
COMMIT;
If one session (user) has done the SELECT FOR UPDATE and not yet committed or rolled back, the other session (user) doing a SELECT FOR UPDATE will block waiting to be able to get a lock. This prevents two users from getting the same number.