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 .
Related
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.
I have a very large sql table that I want to display. However, ist most cases I just want to have it sorted by my datetime-column.
Unfortunately, the regular select-query only displays the table ordered by the primary key (id) - therefore, I have to always manually add 'order by' to my select-query, which is a problem, because sorting the table takes several seconds, even with index.
So my question, is there a way to insert new entries sorted into the table? (by 'sorted', I mean, sorted by the datetime-column, instead of the id column). The incoming entry (id=50) should be sorted before id=49, because it has an earlier datetime-value
Then, using a SELECT * FROM myTable should already show everything sorted by datetime
I realize that it's a bad practice and that I should use order-by, but its not really feasible in my case, because I have millions of entries, which have to be sorted again and again for every select
When you have a MyISAM table, you can do
ALTER TABLE your_table ORDER BY whatever_column;
But the table does not stay this way, when you insert or update entries.
Read more about it here.
This does not make sense for InnoDB tables, however. They are sorted by the clustered index. In this case an option can be to extend your primary key to (datetime_column, column_thats_primary_key_so_far). This is untested, personally I would add an ORDER BY to the SELECT, just in case.
It won't keep the data sorted for future inserts, but you can change the table ordering like this:
ALTER TABLE myTable ORDER BY timestamp
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.
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.
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.