how to sort mysql - mysql

I want to sort mysql by latest logged in but I have to resort it everytime i log in, how do I sort and save the latest login?

You can't reliably keep the values stored in MySQL in alphabetical order. The best you can do is use the ORDER BY clause in your SELECT query.

Usually you sort things when you select by adding an "ORDER BY" clause. Why do you care what order they are actually stored in the table?
If you really care, I guess you could do an INSERT...SELECT to copy it to a new table while sorting the column, but whenever you add a new record, it will end up at the end of the list.

SQL doesn't work that way. It's not like a spreadsheet where the rows are meant to be read "as-is" and you can put them in a certain meaningful order. Nothing you do ever changes the order that SQL stores the rows in. SQL gives you data through queries, and the query tells it which rows you want to see, in what order.
So, you have to reuse the "order by" clause every time you want to see your data in a certain order. However, you shouldn't need to manually query SQL a lot. If you're writing any kind of script or application to work with it, you'll just write the query once and re-run it each time you want the data.

To get the last login from your user table, you could use a query like
SELECT user_name, login_datetime
FROM user_table
ORDER BY login_datetime DESC
LIMIT 1
Assuming that user_name is the field you are looking for and login_datetime is a timestamp you can order on. LIMIT 1 will show only the top most result and can be changed depending on your needs.

With MySQL's InnoDB engine, your rows are physically ordered according to the primary key. So, if the primary key is on the login column, then the rows will stay in physical order by the values in login.
The requirement of a primary key is that each value must be unique and NULL is not allowed. So, you may consider making the login column your primary key. If login were a datetime column, then the latest login datetime value would always be at the end of the table.
Many people make an auto-increment integer column their primary key. In this case, the last record to be inserted would always be at the end of the table.
You must know the value of having your rows in physical order (by a particular column) for certain circumstances, otherwise you wouldn't be asking.
For example, doing the following SELECT returns the rows in physical order (same as primary key) without having to specify a column to order the results by, and without forcing MySQL to later order the result set:
SELECT *
FROM test
However, if you need to order the results other than by the primary key, then you should utilize the ORDER BY clause. For example, if the rows were physically ordered by the column name, but you want the results returned in order by login, you would issue the following query:
SELECT *
FROM test
ORDER BY login ASC
The above query asks MySQL to order the rows according to the specified column. You specify ASC in the ORDER BY clause to order the values in ascending order, and DESC to order them in descending order.

Related

MySQL simple query of table returns results in strange order

My MySQL table is set up with an auto-incrementing primary key. Whenever I
SELECT * FROM [MYTABLE]
The record with the highest primary key value is not displayed last. Is there a reason for this? I can
SELECT * FROM [MYTABLE] ORDER BY [MYTABLE].ID ASC
and the highest is displayed last. Sorry, I am not at liberty to share anything from the database. Perhaps there is some sort of underlying data field (like a record number) not contained in the table that is being used for the sort. This is MySQL 5.7.19 on a Windows server. It seems to me that sorting by a primary key makes more sense than what it's doing.
MySQL version 5.7.19.
What you want is not applicable.
Well, of course, MySquile inherently fetches data no matter how it is stored.
Sort the index table each time you change it, list the main key in the table, or sort the call each time.
select with ordering
SELECT * FROM [MYTABLE] ORDER BY [MYTABLE].ID ASC
Keep in mind that you can never guarantee the order of an auto-incremented column once you start inserting and removing data, so you shouldn't be relying on any order except that which you specify using ORDER BY in your queries. This is an expensive operation that you are doing, as it requires indexes to be completely re-created, so I wouldn't suggest doing it often.

Mysql - Primary key listing in the wrong order

Usually do the query:
select * from table
It should be listed ordered by the primary key in ascending order, but it does not.
I think it's a problem in my primary key, but have used the commands to repair and it did not work.
MySQL makes absolutely no guarantee about what order the data comes to you unless you provide an ORDER BY statement.
For example...
SELECT *
FROM foo
ORDER BY id ASC;
This is the only way to guarantee that your data comes back sorted by ID. Otherwise (depending on the engine you're using for the table), it could come back in any random order. Perhaps, for example, the data comes back in the order that rows were inserted, which might not be apparent from the id values with respect to eachother.
In SQL the order is not anpredefined or inherent property of a set of data.
Then, is not garanteeted that your data are returned in a predefined order .. this include also the primary key order
If you want a proper order by you must use the ORDER BY clause explicatally .

store data ordered by a row in mysql?

Can I somehow make that mysql inserts data in a way that the database keeps an predefined order?
Let's say I make a highscore list. I could always use ORDER BY when selecting the stuff, but all the ordering in 1,000,000+ datasets takes alot of performance when a user browses the highscore list.
My idea is now that I want to insert the data in a way, that the table is always ordered by score desc, so all the ORDER BY work doesn't have to happen when users browse the list.
Tables have no inherent order that you should rely upon (they may, by coincidence, return rows in the same order that they were inserted, or by primary key sort order), so if you need a particular order, you should always use an ORDER BY clause.
However, the system may be able to perform the ordering more cheaply if it has an index available on the column on which you will have your ORDER BY clause - so add an index:
CREATE INDEX IX_Table_Scores ON `Table` (score desc);
Also there is another solution. If you store these much records in order by than there is tedious. Another way is you can create index on high score column so mysql internally create one index so when you try to fetch these record in order by than its performance is better than direct fetching query.
Please create index of high score column.

MySQL, ORDER BY insertion order, no sorting columns

How can I order values from a table, ascending from the time they were inserted. There is no special column for this matter, like a timestamp or autoincrement.
I know this is not recommended to do... Still I would like to know how to do this.
As I understand from the answers, if no sorting columns e.g: timestamp or autoincremental were added before the values were inserted, there is no way of sorting them by insertion.
There is no guarantee that rows will be returned in any particular order without an ORDER BY clause in the query.
Consider a simple query that returns all columns of all rows in a table. For example:
SELECT * FROM mytable ;
For that query, it is likely that MySQL will perform a full table scan, from the beginning of the table. So it is likely that the rows will be returned in the order they are found in physical storage.
This may roughly correspond to the order that rows were inserted, if there have been no deletes, no updates and no reorganization, where space for an inserted row was later reclaimed, and reused to store a newly inserted row.
But this behavior is NOT guaranteed.
To return the rows in the order that they were inserted, the query must specify the sequence that rows are to be returned, by including an ORDER BY clause.
For the rows to be returned in "insertion order", that means the query needs to be able to have that information available, or be able to derive that. For a simple query against a single table, that means the information needs to be stored in the row.
You can ORDER BY something you can get out of your table. If you do not have anything in there that can be used to find out the order you need, you cannot order by it.
Depending on the data in the table, you may be able to order by the id of the data - if the data has a single incremental integer to assure PK uniqueness. There is no other way to sort on insertion order unless the data is captured and recorded in the table.
I don't know of anything in MySQL that retains extra (meta) information on records that you have not specified at the table level.
There needs to be a column to order your query by. Usually this would be an insertion timestamp, or incrementing id/incrementing key. There is no way to guarantee the order otherwise, because there is no record of it.
relevant thread from MySQL forum

Insert row in proper order on table based off of primary key.

I like being able to go to phpmyadmin and seeing my rows ordered.
Is there an easy way to insert a row into the correct order by it's primary key?
There is no order you can rely on.
Unless you specify ORDER BY, there are no guarantees about what rows
will be returned first.
But in practice, the order will typically match the clustered index
order, and will not vary between calls. Don't rely on this behaviour.
Does MySQL's LIMIT keyword guarantee order of returned data?
There is no default sort order. Even if the table has a clustered
index, you are not guaranteed to get the results in that order. You
must use an order by clause if you want a specific order.
SQL best practice to deal with default sort order
Just insert the data; tables are not fundamentally ordered. The only ordering of table results occurs when you define it yourself in your select statement.
Is it possible to insert data into the mid section of a table using the INSERT command?
You can't. There is no such thing.
To be precise: in MyISAM engine, current order is whatever the order
was when the last ALTER TABLE ORDER BY was performed, modified by all
the deletions and insertions since.
InnoDB just sorts the tables as it wishes.
view current table order
However be aware of the advice from another poster above: the order in
which rows are returned without an ORDER BY clause is arbitrary and
may change without notification. It would be best to amend your table
if at all possible.
Reverse the “natural order” of a MySQL table without ORDER BY?
.... and maaany more.