I have probably a very simple question.
I have a MySQL database called "alldata", which contains various variables. The first column called LogDateTime contains date and time. Now the thing is that I want the db to be sorted from the oldest to the newest - in other words by column 1.
I know how to do a MySQL query using ORDER BY Logdatetime etc. But what I would like to do is to reorder tha data in the actual database and save it ordered. Right now there are some dates a bit messed up. Then I would not have to use the ORDER BY statement in all my queries because the database would already be sorted.
Could anyone please just give me the SQL command I should use to reorder the entire database?
You cannot setup a relational database table to return results ordered by a specific column of your choosing. You need to use ORDER BY. You could work around this by using views.
The view definition would include an ORDER BY. You could select from the view and it would show results in your desired order.
CREATE OR REPLACE VIEW `mytable_view` AS SELECT * FROM `my_table` ORDER BY `my_date_column`
Then you can select the data:
SELECT * FROM `mytable_view`
Results are shown in desired order.
Related
Is it possible to get the list column names from a query in IBM db2?
Consider a very simple example I want the column names fetched by following SQL;
select * from db.t1,db.t2 where t1.id = t2.id
Actually I know how to get column names from a a single table. But facing difficulties to get column names from such scenario.
I want the list of columns as an Array in PHP. It can be done if I just add "FETCH FIRST 1 ROW ONLY" as the end of the SQL, and run it. Then from result set I can get the columns.
But if there is no data then also I need the list of columns. How to achieve that?
Any help would be great for me.
You can use db2_num_fields() to determine the number of columns in the result set, then loop over them and call db2_field_name() to obtain the names.
You could always just do something like
describe select * from tablea, tableb
In mongodb php, I need to get a distinct list of records based on one field but the search should start from the last record.
db.collection.distinct doesn't sort records and doesnt work with a paginated data set, and it's not given the way it searches for distinct values: any order you see is incidental and could change with implementation or access plan.
I have a MySQL database. I have a PHP page which grabs the rows of the database and displays the column data (in a format that I created) on the PHP page.
What I would like to do: display the data in order (of the date, there is a date that users set in a form that then submits it to the database and it goes into a date data type in the MySQL database).
Should I make sure that when I enter in a new row that it goes in the proper place (order the data as it enters the database),
Or should I try to have the PHP page grab the information in the right order (or try to arrange everything in the right order)?
I don't know how to go about doing this. If there is another method of doing it that will be fine, just let me know if there is a more efficient way.
Relational databases don't have any inherent order to the data. If you care about the order of data when you're displaying it, use the ORDER BY clause in the SELECT statement.
Do the ordering in mysql, let php handle only the display part.
ORDER BY `date` DESC
Will print your records by date descending order. Change order accordingly
When you select the data from your table, you specify its order with the ORDER BY clause:
SELECT * FROM SomeTable ORDER BY SomeField
Natural ordering within the table itself isn't guaranteed. A clustered index will determine an ordering, which your primary key is probably doing. But "storing the data in order" isn't really what you're meant to do. The data is just data, it's not "ordered" until its viewed. So when viewing the data, you specify the order you want it in.
I need to pull a list of items from a MySQL database and then display them using PHP. I have this figured out. My question is how to change the order of the items. I know you can order them based on a certain field from the database, such as 'id' or similar.
What I need to do is do a calculation on 2 of the fields and then order the resulting list based on that calculation. For example, I need to show an Integer-X from the db and Integer-Y. I also need to show the result of Integer-X minus Integer-Y and I need to sort the list of responses based on that result of Integer-X minus Integer-Y.
Any help would be much appreciated. Thanks in advance.
You can put calculations in the ORDER BY clause:
ORDER BY integerXField - integerYField
Also, if you give an alias to the calculation in the SELECT list, you can sort by the alias:
SELECT
... integerXField - integerYField as theDiff
..
ORDER BY theDiff
I have a table in MySQL that I'm accessing from PHP. For example, let's have a table named THINGS:
things.ID - int primary key
things.name - varchar
things.owner_ID - int for joining with another table
My select statement to get what I need might look like:
SELECT * FROM things WHERE owner_ID = 99;
Pretty straightforward. Now, I'd like users to be able to specify a completely arbitrary order for the items returned from this query. The list will be displayed, they can then click an "up" or "down" button next to a row and have it moved up or down the list, or possibly a drag-and-drop operation to move it to anywhere else. I'd like this order to be saved in the database (same or other table). The custom order would be unique for the set of rows for each owner_ID.
I've searched for ways to provide this ordering without luck. I've thought of a few ways to implement this, but help me fill in the final option:
Add an INT column and set it's value to whatever I need to get rows
returned in my order. This presents the problem of scanning
row-by-row to find the insertion point, and possibly needing to
update the preceding/following rows sort column.
Having a "next" and "previous" column, implementing a linked list.
Once I find my place, I'll just have to update max 2 rows to insert
the row. But this requires scanning for the location from row #1.
Some SQL/relational DB trick I'm unaware of...
I'm looking for an answer to #3 because it may be out there, who knows. Plus, I'd like to offload as much as I can on the database.
From what I've read you need a new table containing the ordering of each user, say it's called *user_orderings*.
This table should contain the user ID, the position of the thing and the ID of the thing. The (user_id, thing_id) should be the PK. This way you need to update this table every time but you can get the things for a user in the order he/she wants using ORDER BY on the user_orderings table and joining it with the things table. It should work.
The simplest expression of an ordered list is: 3,1,2,4. We can store this as a string in the parent table; so if our table is photos with the foreign key profile_id, we'd place our photo order in profiles.photo_order. We can then consider this field in our order by clause by utilizing the find_in_set() function. This requires either two queries or a join. I use two queries but the join is more interesting, so here it is:
select photos.photo_id, photos.caption
from photos
join profiles on profiles.profile_id = photos.profile_id
where photos.profile_id = 1
order by find_in_set(photos.photo_id, profiles.photo_order);
Note that you would probably not want to use find_in_set() in a where clause due to performance implications, but in an order by clause, there are few enough results to make this fast.