I imported the 3 tables in a file to my database. Worked perfectly.
Here's the file code: https://pastebin.com/EVr9qGxe
....
However, I want to change the variable in each table so I went to phpMyAdmin to change the variables in the tables.
They change just fine, however, the options appear in alphabetical, and not the order I entered them in or the country_id
for example if I entered
USA with country_id 1
France with country_id 2
France appears first in the list.
So my question is, what should I change in the code linked above in order to remove the alphabetical auto-order and make the options appear by their ID?
If I understand correctly, your question is similar to this one. There's no way of permanently changing this behavior I'm aware of. You could add
ALTER TABLE `your_table` ORDER BY `column_name`
But the table will 're-order' itself back to where it was on the next CRUD operation performed on that table, and for some databases this query simply won't work.
As you are using phpMyAdmin, there's a simpler solution without coding- just enter your database, choose your table, and click on any column. Clicking on any column will order your table by values from that column. Clicking it again will change ordering from ascending to descending.
Rows in relational database are not really ordered, but you can order them when needed, by performing queries (with use of ORDER BY). For example, if you need to select all rows from table 'users', and order them by 'creation_date', you'd simply use SELECT * FROM users ORDER BY creation_date, but it won't change rows ordering in the table itself- you'd just get ordered result displayed.
Related
I've been at this for almost two days and I keep getting all sorts of errors in Access. So I am asking for your help. Not sure if this is even possible. THANKS
I have three tables.
The Ask
I would like to build a query to combine the data in ContractData and InvoiceData tables (see Query 1 Table).
When I run the query, Access should prompt me to enter the DocumentDate YEAR (ie. 2018) and return all records that are older than said YEAR (ie. 2018).
If ties exists for the same ID, then there is no preference as to which record is returned.
If there are no records for an ID with a DocumentDate, then Access should remove duplicates and return only one (no preference as to which record is returned). (See The Ultimate Result Table)
I think I was able to produce what you’re looking for, though it’s a multi-step process.
But first, Name is a reserved word in Access. I changed the Name field to SupplierName in all 3 of your example tables.
Field names will need to be the same in the ContractData and InvoiceData tables for this to work as described below.
I also setup a unique primary key (AutoNumber) in each table. Will probably still work without, but I didn’t try it, and would not recommend going forward without each record uniquely identified.
Now for the fun parts…
Step 1: create a Union Query.
Create ->
Query Design ->
Close the Show Table window without adding anything.
Top left of the screen click SQL View.
Delete what’s there, and paste the following:
SELECT ID, SupplierName, DocumentDate, DocumentNumber, DocumentStatus
FROM ContractData
UNION
SELECT ID, SupplierName, DocumentDate, DocumentNumber, DocumentStatus
FROM InvoiceData;
Click Run.
This should now be the data shown in your Query 1 Table example.
Close this query and save as Query1Table (or whatever name you want).
Step 2, create a second query, using the query we just crated (Query1Table):
Create ->
Query Design ->
Close the Show Table window without adding anything
Top left of the screen click SQL View
Delete what’s there, and paste the following:
SELECT Query1Table.ID, Query1Table.SupplierName, First(Query1Table.DocumentDate)
AS FirstOfDocumentDate, First(Query1Table.DocumentNumber)
AS FirstOfDocumentNumber, First(Query1Table.DocumentStatus)
AS FirstOfDocumentStatus
FROM Query1Table
WHERE (((Year([DocumentDate]))>[Enter the DocumentDate YEAR]))
GROUP BY Query1Table.ID, Query1Table.SupplierName;
If you didn’t call the first query we made Query1Table, then you’ll need to adjust the above SQL statement appropriately.
Close query and save it as TheUltimateResult (or whatever you want).
When you open this query, TheUltimateResult, you’ll be prompted to enter a Year.
Enter the desired year and click OK.
All done!
Hopefully this will fulfill what you’re looking to do.
I have an issue I really don't understand. In fact, I have a database of movies where there's a table for actors,containing two colums (movie id and name).
For example, then I enter the movie id of Django Unchained, the first result is Jamie Fox (the main actor).
But then I enter, this sql query (i would expect to get Jamie Fox, Christoph and Leonardo):
SELECT * FROM LesActeurs WHERE film_id=68718 ORDER BY acteur LIMIT 3
But I get 3 actors by alphabetical order. Do You Know how could I mimic the DB Browser order with command (I'm a beginner)?
Thank you!
Without any other column to order by, you can't get that result, at least not reliably. Without an explicit order by clause, the database is free to return the rows in whatever order it chooses (often it's just the order in which they were inserted).
If you want to get reliably get Jamie, Christophe and Leonardo you could add another column (e.g., "importance"), populate it and then query and explicitly order by it.
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.
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?
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.