Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to insert a new row at top of MySQL table?

Tags:

mysql

All rows in MySQL tables are being inserted like this:

1
2
3

Is there any way how to insert new row at a top of table so that table looks like this?

3
2
1

Yes, yes, I know "order by" but let me explain the problem. I have a dating website and users can search profiles by sex, age, city, etc. There are more than 20 search criteria and it's not possible to create indexes for each possible combination. So, if I use "order by", the search usually ends with "using temporary, using filesort" and this causes a very high server load. If I remove "order by" oldest profiles are shown as first and users have to go to the last page to see the new profiles. That's very bad because first pages of search results always look the same and users have a feeling that there are no new profiles. That's why I asked this question. If it's not possible to insert last row at top of table, can you suggest anything else?

like image 583
tfe Avatar asked Oct 13 '11 11:10

tfe


People also ask

How do I insert a row at the top of a table in SQL?

If you want to add data to your SQL table, then you can use the INSERT statement. Here is the basic syntax for adding rows to your SQL table: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The second line of code is where you will add the values for the rows.

Can we use top in MySQL?

Note − All the databases do not support the TOP clause. For example MySQL supports the LIMIT clause to fetch limited number of records while Oracle uses the ROWNUM command to fetch a limited number of records.

Which MySQL command is used to add a new row in a table?

The INSERT ... VALUES , INSERT ... VALUES ROW() , and INSERT ... SET forms of the statement insert rows based on explicitly specified values. The INSERT ... SELECT form inserts rows selected from another table or tables. You can also use INSERT ... TABLE in MySQL 8.0.

How can you insert a new row into the store table?

Click in a cell above or below where you want to add a row. Under Table Tools, on the Layout tab, do one of the following: To add a row above the cell, click Insert Above in the Rows and Columns group. To add a row below the cell, click Insert Below in the Rows and Columns group.


2 Answers

The order in which the results are returned when there's no ORDER BY clause depends on the RDBM. In the case of MySQL, or at least most engines, if you don't explicitly specify the order it will be ascending, from oldest to new entries. Where the row is located "physically" doesn't matter. I'm not sure if all mysql engines work that way though. I.e., in PostgreSQL the "default" order shows the most recently updated rows first. This might be the way some of the MySQL engines work too.

Anyway, the point is - if you want the results ordered - always specify sort order, don't just depend on something default that seems to work. In you case you want something trivial - you want the users in descending order, so just use:

SELECT * FROM users ORDER BY id DESC
like image 144
Nikoloff Avatar answered Oct 05 '22 07:10

Nikoloff


I think you just need to make sure that if you always need to show the latest data first, all of your indexes need to specify the date/time field first, and all of your queries order by that field first.

If ORDER BY is slowing everything down then you need to optimise your queries or your database structure, i would say.

like image 39
Codecraft Avatar answered Oct 05 '22 06:10

Codecraft