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.
Related
I'm still learning to program and I challenge my self to create slideshow retrieving images according to it's date. Thank you in response.
You can fetch pic from database like this
SELECT * FROM `pic` ORDER BY time ASC
Order by will give you the result as you want, make sure you have a time attribute in the table.
and you have to just loop through the return data and display as you want
I'm new to sql & php and unsure about how to proceed in this situation:
I created a mysql database with two tables.
One is just a list of users with their data, each having a unique id.
The second one awards certain amounts of points to users, with relevant columns being the user id and the amount of awarded points. This table is supposed to get new entries regularly and there's no limit to how many times a single user can appear in it.
On my php page I now want to display a list of users sorted by their point total.
My first approach was creating a "points_total" column in the user table, intending to run some kind of query that would calculate and update the correct total for each user every time new entries are added to the other table. To retrieve the data I could then use a very simple query and even use sql's sort features.
However, while it's easy to update the total for a specific user with the sum where function, I don't see a way to do that for the whole user table. After all, plain sql doesn't offer the ability to iterate over each row of a table, or am I missing a different way?
I could probably do the update by going over the table in php, but then again, I'm not sure if that is even a good approach in the first place, because in a way storing the point data twice (the total in one table and then the point breakdown with some additional information in a different table) seems redundant.
A different option would be forgoing the extra column, and instead calculating the sums everytime the php page is accessed, then doing the sorting stuff with php. However, I suppose this would be much slower than having the data ready in the database, which could be a problem if the tables have a lot of entries?
I'm a bit lost here so any advice would be appreciated.
To get the total points awarded, you could use a query similar to this:
SELECT
`user_name`,
`user_id`,
SUM(`points`.`points_award`) as `points`,
COUNT(`points`.`points_award`) as `numberOfAwards`
FROM `users`
JOIN `points`
ON `users`.`user_id` = `points`.`user_id`
GROUP BY `users`.`user_id`
ORDER BY `users`.`user_name` // or whatever users column you want.
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.
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.
I have a jQuery/PHP tutorial i followed in a book that created pagination that uses Ajax to retrieve the records from a MySQL table each time the page is changed using the limit clause in the SQL statement. I'd like to add the ability to have the user click the column headings to sort them but i tried tablesorter but that just sorts the visible records. Is there a way to sort in the SQL statement and have it remember the sorting if the user clicks the next or previous buttons?
I would suggest having a URL parameter for the page which holds the current column you would like to order by. You can then append something similar to "ORDER BY ".$_GET["order"] to the end of your SQL query. Then simply make each column heading a link to the page with ?order=column added to the end, replacing "column" with whatever column in the table you want it to be ordered by.
Of course, you should use something like mysql_real_escape_string to make sure that the URL parameter is safe to use in the query.