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.
Related
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 have created a database in MySQL. When I try to get all the records for a table, the last-added records are displayed last. How can I efficiently display the latest records first, without using a datetime column? Adding a datetime column would mean reinitializing all tables, which seems too troublesome to me.
How can I efficiently display the latest records first, without using
a datetime column?
If you have an auto increment ID column you could try ordering by the record ID in descending order.
e.g.
SELECT * FROM my_table ORDER BY id DESC;
To follow on from the previous answer many ORMs inster CreatedAt and UpdatedAt columns. Slap an ORDER BY CreatedAt DESC and there you have it in the order you want and it's independent of the Id you use.
does you table have a index? then you could
SELECT * FROM table ORDER BY index DESC
Use ORDER BY on an appropriate (time-ordered) column. As stated above, you can use an auto-incremented primary key, however this doesn't allow you to select a date range.
FWIW, it's not difficult to add timestamp columns to existing tables, and you gain the ability to return rows within a range of dates.
Approximate syntax:
ALTER TABLE `table_name`
ADD COLUMN `created_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE `table_name`
ADD INDEX `created_time` (`created_time`);
I have question, I want to insert new row to db (mysql) using php and I need to add it to first place in table, but it comes to last position.
I need to short it like this:
3
2
1
but it shorts like
1
2
3
Can you help me please?
Thanks a lot, Stepan
Assuming that you are using an incremental ID as the unique key, you can do:
order by id desc
if not, you can add a column to the DB called "created" which would be a datetime that you set to NOW() when you create the row. Then you would do:
order by created desc
SQL databases do not guarantee an order, unless you specify an ORDER BY clause.
If you have an identifier column, then you can do:
SELECT ...
FROM your_table
ORDER BY id DESC
I have column in mysql table (my_sale_time) of type timestamp....infact the rows value look like this
2010-12-01 14:38:07
2010-12-01 17:14:18
...
so what i need mysql query to delete those value whose date is repeated multiple times in table.....as i mentioned in sample sale_time_value.....only date is repeated with same value but time is different....so i want to get all rows, date is repeated multiple times and delete duplicate dates
The basic principle of deleting duplicate rows:
CREATE TEMPORARY TABLE tmptbl AS SELECT DISTINCT * FROM my_sale_time;
DELETE FROM my_sale_time;
INSERT INTO my_sale_time SELECT * FROM tmptbl;
You may have to specify columns and WHERE clauses (I didn't really understand your criteria).
And of course you should test-run it on a development server and don't forget to run it as a single transaction with locked tables.
If you have an auto_increment field, use this:
DELETE FROM
`mytable`
WHERE
`my_auto_increment_field` NOT IN (
SELECT
MAX(`my_auto_increment_field`)
GROUP BY
`my_sale_time`
);
What SQL query would I use to display the newest entry?
Details:
id is the primary field. I have other fields but that are not related to when they were added.
ORDER BY SomeColumn DESC
LIMIT 1
or
use the MAX() function
Since you didn't give any details about your table it is hard to answer
SELECT * from yourTable ORDER BY `id` DESC LIMIT 1;
Another (better) way would be to have a "date_added" column (date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP) so you could order by that column descending instead. Dates are more reliable than ID-assignment.
not sure if this is what your looking for but I use mysql_insert_id() after inserting a new row
The auto incremented ID columns are not always the latest records inserted, I've remember really painful experience with this behavior. Conditions where specific, it was mysql 4.1.x at the time and there was almost 1 million records, where 1 out of 3 deleted everiday, and others re inserted in the next 24hours. It made a huge mess when I realize ordering them via ID was not ordering them by age....
Since then, I use a specific column for doing age related sorts, and populating these fields with date = NOW() at each row insert.
Of course it will work to found the latest record as you want, doing an ORDER BY date DESC LIMIT 0,1on your query
SELECT Primary_Key_Field FROM table ORDER BY Primary_Key_Field DESC LIMIT 1
Replace Primary_Key_Field and table obviously :)