When i get rows from the db with the same exact same date's but the id's get fillped as such:
select * from rent ORDER BY date_at ASC;
id: 70 date: 12-01-2013
id: 69 date: 12-01-2013
Is there a way to work around this so that i can sort by date but still retain the id order if the date's are the same?
Add another column you want to sort by in the order condition
SELECT * FROM rent
ORDER BY date_at ASC,
id ASC
Related
Is there a way to use limit offset and get the most recent (MAX) date from that group
My table: column_id, column_data, column_date
I've tried
SELECT max(column_date) FROM table_name limit 2000 offset 22000
I'm trying to get the most recent date in the 2000 rows returned using the offset. In other words, I'm looking for the last date modified in each group of 2000.
The table structure above has 100,000 rows. each query gets 2000 rows and I would like to retrieve the most recent date from the 2000 rows (using offset).
You must extract the whole group then find MAX() over it:
SELECT MAX(date_column)
FROM ( SELECT date_column
FROM source_table
ORDER BY some_expression /* compulsory! must provide rows uniqueness! */
LIMIT #rows_in_group OFFSET #group_offset ) AS subquery
I know how to search the LATEST date and the MOST value specifically:
Most Quantity Used:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY quantity_used DESC
Latest Date:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY start_date DESC
But how can I do both, by able to search the LATEST date WITH the MOST value in quantity_used? Is this practice even possible?
EDITED: I think my question is not clear enough.
I intend to find the data with the LATEST date first, then from that result FIND the highest VALUE from quantity_used.
I think you just want two order by keys:
SELECT tp.*
FROM tour_packages tp
WHERE tp.active = 1
ORDER BY tp.start_date DESC, tp.quantity_used DESC;
This returns the rows ordered by date and within each date, the ones with the largest quantity go first.
I need to sort a table by date (descending), but all columns in the table are varchar, so I need to manipulate the data on the fly for sorting it correctly.
date sales
10/09/2014 100
13/09/2014 250
30/08/2014 200
Is that possible without altering the table? So the result will be like below, newest dates first?
date sales
13/09/2014 250
10/09/2014 100
30/08/2014 200
Like pseudocode
SELECT * FROM table ORDER BY (CONCAT(REGEXP(date, '[0-9]{4}'),
REGEXP(date, '/[0-9]{2}/'), REGEXP(date, '^[0-9]{4}/')) DESC
I think I need to use substring_index somehow, because regexp just returns 1 or 0, not the actual value found.
You need to convert your varchar-stored date objects into DATE objects, then use them to order.
This you can do on the fly like so
ORDER BY STR_TO_DATE(date,'%d/%m/%Y') DESC
But performance is going to be horrible. For best results store your dates in a DATE column in your table.
you can use STR_TO_DATE
SELECT *
FROM Table1
ORDER BY STR_TO_DATE(date, '%d/%m/%Y') desc,
sales desc
This would be my query:
SELECT * FROM Bans ORDER BY Date DESC LIMIT 10
Here's how the timestamp(Date) looks: September 01, 2012 - 10:33:13 | May 31, 2012 - 19:28:25, etc.. Now, my problem is, is that I have records from June, but they aren't showing.
How can I fix this?
Edit: This table should show the "Latest" 10 bans.
I may supose that dates are sorted in the alphabetical order, maybe you should try
SELECT * FROM Bans ORDER BY TO_SECONDS(Date) DESC LIMIT 10
Date might be considered as a keyword in MySQL. Wrap it inside the backticks. Dates are not in the correct form. Use TO_SECONDS and alter the query this way:
SELECT * FROM `Bans` ORDER BY TO_SECONDS(`Date`) DESC LIMIT 10
The following code works, but I want the groups to show their latest query by datetime not the first datetime query. I've tried changing around the ORDER BY from ASC/DESC, but that just changes the order of all the groups, not the data within the groups. I need for both all the inside data of the groups and all the groups to order by the latest datetime.
$query="SELECT * FROM emails
WHERE sentto='$sid'
GROUP BY sentto
ORDER BY datetime DESC LIMIT $eu, $limit ";
Instead of it showing groups and ordering them by the first query:
Message from Sam Richards on January 22, 2011 (this is a group)
Message from John Smith on January 5, 2011 (this is a group)
I want it to show groups and order them by the latest query:
Message from John Smith on April 19, 2011 (this is a group)
Message from Sam Richards on March 10, 2011 (this is a group)
Please help.
I think part of your problem is that you are selecting non-aggregate columns with a group-by query. You should be explicit about which values you want it to return in the aggregate query result.
SELECT sentto, MAX(datetime) AS datetime FROM emails
GROUP BY sentto
ORDER BY datetime desc LIMIT $eu, $limit;
I'm still not sure that this gives you what you want. It sounds like you want to actually retrieve the rows for each individual email and just use the GROUP BY maximum for sorting. To do that, you'd probably need to do the above query, then go back and do a second query for each sentto. I can't think of a way offhand to do it in a single query.
SELECT * FROM emails
WHERE sentto=$sid
ORDER BY datetime DESC;
(For each sentto returned in the first query.)
How about:
ORDER BY sentto ASC, datetime DESC
To sort the data with in the groups you need to include sentto in the ORDER BY clause.
Try this:
SELECT * FROM emails
WHERE sentto='$sid'
GROUP BY sentto
ORDER BY sentto, datetime DESC LIMIT $eu, $limit