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 (*).
Related
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.
I have a check-in / check-out system which writes for every check-in a new row in my table and updates the table when the person checks out (like checkedout = 1)
Now I'm making a new site that always shows the newest checked in person. I do it by polling and storing the highest ID on a variable on that page. In the polling I search for entries > the id i stored. It's working good so far.
But now I want to extend it and show either the latest checked in person OR the latest checked out person. How can I get the last "updated" row in my table?
You can add a column for ex. date_checked of type datetime and update it whenever something happens.
After that just select by that column.
use mysql function called mysqli_insert_id() which will give you last inserted primary key value of the table
try this:
SET #update_id := 0;
UPDATE some_table SET column_name = 'value', id = (SELECT #update_id := id)
WHERE some_other_column = 'blah' LIMIT 1;
SELECT #update_id;
More Click Here
I have a MySQL database with a table called chapters.
I have a column called chapter_number which is an Integer data type and is NOT auto increment as I have another field which uniquely identifies the row.
Upon insertion to this table I would like the value of the chapter_number column to be the previous row's chapter_number incremented by 1 (+1).
The purpose of this is to ensure that every time a chapter is added, the right ordering takes place.
How could I achieve this in PHP?
Here is an example of what I mean?
Table structure:
$stmt = $db->prepare('INSERT INTO chapters VALUES (?,?,?,?)');
$stmt->execute(array('','title', '2015-04-03', 'chapter 1 body'));
What would I put for the first parameter ^
SELECT max(chapter_number) + 1 as total FROM table;
This would also allow you to later on have chapters assigned to multiple books by symply adding a where clause
The following example is how this syntax would look
insert into table_one (greeting_column, name_column)
SELECT
'hello',
(SELECT max(chapter_number) + 1 as total FROM table)
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.
Simply put, how can I count how many rows there are before a certain row. I'm using incremental ID's, but rows are deleted a random, so just checking to see what the ID is won't work.
If I have, say, 30 rows, and I've selected one based on a name (or anything really), how many rows are there before that one? It could be 16, 1, 12, or anything.
I'm using MySQL and CodeIgniter.
I assume your primary key column has datatype integer
SELECT COUNT(*) FROM `table`
WHERE id < (SELECT id FROM `table` WHERE `conditions are met for specific row`)
Assuming it's an auto_increment column, deleted rows won't be filled in again so this should do the job.
SELECT COUNT(*) FROM table WHERE id_column < your_selected_row_id;
On your model:
$id = $this->db->get('users')->where("name", "John")->id;
$rows = $this->db->get('users')->where("id < ", $id)->num_rows();
return $rows;
Notice how I'm using "chained methods" and for that you need PHP5 which is the default for CI 2.
You first need to get the ID of the record you need to start counting "backwards" which is the first line, considering a table called users and that the column you are filtering is "name" and the row you want to find has the name value of John.
The second line will give you the number of rows that the query "where id < number" returned where number is the ID you got from the first query. Maybe you can even chain both lines.