mysql prevent insert row auto sorting - mysql

I use Codeigniter to perform insert to mysql (not sure if relevant), I have table column and some data like this after I insert:
[invoice_id][product_id][unit_cost][quantity]
[42][1][50][2]
[42][2][100][3]
[42][5][45][1]
The problem is mysql auto sort it by invoice_id first then product_id like the above.
Before I insert them, my invoice item-list position was :
[42][5][45][1]
[42][1][50][2]
[42][2][100][3]
I do not want any of this auto-sort because when I retrieve them, they went like the list in database not as in the invoice. I dont think I can use sort for a particular column because they are all random in the first place.
I can only start thinking to add another column [position] contain number just for the sake of sorting it later, or is there a better way without it?
Thanks in advance for any reply.

You are not guaranteed the order of MySql resultsets unless you specify an ORDER BY clause. The only way to do it if you cannot sort with the parameters you already have would be to add another column to the table. Typically this would be an autoincrement integer field. You would then be able to order by id, and return the rows in the order they were entered.

Related

Limit identical input in database to a certain count

I know that we can limit an identical input in database to only one by the keyword UNIQUE, but how about limiting it to more than one? For example, 3,4? Is there a way to achieve this?
The only way is to do it on your way by checking that amount before doing a new insert.
Before insert a new row, check how many identical rows there are and handle that case (e.g. don't perform the insert and/or show a message to the user).

How to adjust Auto Increment?

Good day to all. So I have lots of data in excel that i needed to insert in the db but I didn't notice I inserted a duplicate value (cause hr didn't sort it out).
So anyways, here is the deal
[id][name]
[1][name1]
[2][name2] // I want to delete this row
[3][name3]
[4][name4]
I want to delete row 2. I want to make the id auto adjust like,
[id][name]
[1][name1]
[2][name3] // The id will adjust
[3][name4]
Is this possible to achieve? Or if not is there a work around I can do?
PS: My data is already at 50k and I want to delete the rows in the 7k
In general, you should avoid manually intervening with an auto increment column. This means, among other things, that when you insert data into your table, you should omit the auto increment column, thereby allowing MySQL to automatically assign a value to it.
Perhaps the best solution here would be to have a third timestamp column which records when each record were inserted. Then, you may use ROW_NUMBER to generate the sequence you want, e.g.
SELECT
ROW_NUMBER() OVER (ORDER BY ts_column) id,
name
FROM yourTable
ORDER BY
id;
You might be able to also order ROW_NUMBER using the id column, and get the same result. If you are using a version of MySQL earlier than 8+, then you may simulate ROW_NUMBER using user variables.

storing records in mysql db in sorting order of date

I want to store some records in mysql database. records have a date column. i want to store them in sorting order of that date column.
For example, record having date 27/sep/2011 get stored as first row on the top of record having date 26/sep/2011 as:
id_1,name_1,27/sep/2011
id_2,name_2,26/sep/2011
if new records come on future dates they would get inserted on the top.
I DONT want to order them while using select by using order by desc .
i want they get inserted into db directly in sorted order.
how to do this???
thanks...
I am always surprised when people want to determine physical order of storing records.
Basically, it's a terrible idea for multiple reasons.
1) How the record is physically stored should not be of your concern.
2) How the record is presented should be of your concern. That's why we have ORDER BY built in.
3) Determining physical storage should be done by experts in the field, since it has performance implications - which is a topic in its own and I won't go into details.
Basically, worry about getting the data out in the sorted order, not getting it in in the sorted order.
Reason why it's a bad idea is because you'll be tampering with the primary key which is never, ever a good idea. On top of that, you'll have to reorder the records every time you insert something. Just don't reinvent hot water.
You could do this by adding another table - inserting all of the records into that table (the current and the new ones) then doing and insert as follows :
INSERT into newtable
select * from temptable
order by temptable.date
Why do you need to do this ? why not just use orderby on the query ?
As pointed out in the comments below - you would need to truncate the newtable each time
You cannot choose where to insert your row.
Here's one possible solution: MySQL syntax for inserting a new row in middle rows?

Make my own incrementing mysql id without auto increment

How can I duplicate the function of an auto incrementing Id field without making the field itself auto incrementing? I suppose on my INSERT statement, I would need to somehow grab the last id created, and add +1 on the new entry. But I don't know how. Any help is appreciated.
EDIT: I ended up taking ypercube's advice and keeping the id field as autoincrementing and making my searchstring unique. Thanks!
You'd need to select the highest current ID in one query, then use that when inserting using a second query.
To get round the race conditions, you could create a table lock (nasty) or use transactions (better, but not ideal)

Mysql Insert data into table have a arranges question?

I found a weard problem with my MySQL DB.
sometime when I insert new data into it, the way it arranges the data is like a stack, for example
4 (newest)
3
2
1 (oldest)
...
how can I make it arranged like this?
1 (newest)
2
3
4 (oldest)
thanks all.
SELECT *
FROM TABLE
ORDER BY ID
You have to remember that when viewing/selecting data from a table without any ORDER BY specified does not garuantee any specific order.
The way you are view the data (unordered) can be due to any one of a lot of factos (the database engine, schema, page storage, page fragmentation, indexes, primary keys, or simply execution plan optimization).
The SQL standards specifically states that tables do not have a "natural" order. Therefore, the database engine is free to return a request without an ORDER BY in any order it wants to. The order may change from one request to another because most engines choose to return the data in whatever order they can get it to you the most rapidly.
It follows, therefore, that if you want the data out in a particular order you must include a column in your table whose job is to proxy for the order in which you added records to the table. Two common ways of doing this are using an autoincrement field which will be in numerical order from oldest to newest record, and a TIMESTAMP column which does just what it says. Once you have such a column you can use ORDER BY ColumnName when searching to get an ordered result set.