PHP MYSQL QUERY ORDER BY desc problem - php

I have two field name $date and $time.
date | time
2011/01/09 | 08:22:25
2011/01/09 | 13:00:55
2011/01/09 | 17:45:18
2011/01/09 | 17:30:26
2011/01/08 | 18:22:00
2011/01/08 | 12:06:39
How to let the newest before the oldest. I want them to be:
date | time
2011/01/09 | 17:45:18
2011/01/09 | 17:30:26
2011/01/09 | 13:00:55
2011/01/09 | 08:22:25
2011/01/08 | 18:22:00
2011/01/08 | 12:06:39
How to write a select * from article order by...desc...? Thanks.

SELECT * FROM yourtable ORDER BY date desc, time desc

select * from article order by date desc, time desc

SELECT * FROM article WHERE 1 ORDER BY `date` DESC, `time` DESC;
should do just fine.

$sql="SELECT * FROM CAFTERIA ORDER BY sl_no DESC;"

After reading the answer to this question and a bit of pain I discovered the following worked for me. This answer includes a PHP example for SELECT, WHERE, ORDER BY and LIMIT. They need to be in that ORDER with LIMIT last of it doesn't work.
$query = "SELECT * FROM Page_Relations WHERE Child=$article_id ORDER BY Page_ID ASC LIMIT 0 , 30 ";
I wanted to create a context sensitive menu list where the pages in the index are ordered by a Page_ID number.

Related

How to display the last records of the user

I have a table and I have more than 100 records in the table. I have the same records but the date of added is different.
Table is
m_id | member_id | c_k | b_k | date_of_added
1 | 101 | qwer |sdad1 | 14-02-2019 02:26:30
2 | 101 | qwe2 |sdad2 | 14-02-2019 03:30:20
3 | 102 | qweg |sdad3 | 14-02-2019 04:00:40
4 | 101 | qwe3 |sdad4 | 14-02-2019 04:30:20
5 | 102 | qweg |sdad5 | 14-02-2019 05:45:30
I tried below query but it's displaying all the records related to the member_id=101. I need a last record of the member_id.
SELECT * from m_details WHERE member_id=101 GROUP by member_id ORDER BY date_of_added DESC
My expected output is
m_id | member_id | c_k | b_k | date_of_added
4 | 101 | qwe3 |sdad4 | 14-02-2019 04:30:20
Would you help me out what is wrong with the query?
Tagging PHP team because I am working on PHP but getting the issue in query.
Use this instead :
SELECT * // Gets everything
FROM m_details // From your table
WHERE member_id=101 // For member #101
ORDER BY date_of_added DESC // Gets the last result
LIMIT 1 // Only shows that result
SELECT * from m_details
WHERE member_id=101
ORDER BY date_of_added DESC
LIMIT 1
The GROUP BY is just obscuring the result because it does not necessarily pick the data from the last row it finds.
Your query is correct. You just need to use LIMIT 1, if you need the last record:
SELECT * from m_details WHERE member_id=101 GROUP by member_id ORDER BY date_of_added DESC LIMIT 1;
SELECT * from m_details WHERE member_id=101 ORDER BY m_id DESC LIMIT 1
Using the above query for expected output. In this case, does not need to group by clause.
In MySQL 8.0, use ROW_NUMBER() :
SELECT * FROM (
SELECT t.*, ROW_NUMBER() OVER(PARTITION BY member_id ORDER BY date_of_added DESC) rn
FROM mytable t
) WHERE member_id=101 AND rn = 1
The advantage of this approach is that it will work even if you need to display the information for more than on logic, as opposite to the GROUP BY/LIMIT solution, that only works for a single user at a time.
In earlier versions of MySQL, this can also be implemented using a NOT EXISTS condition :
SELECT t.*
FROM mytable t
WHERE
t.member_id = 101
AND NOT EXISTS (
SELECT 1
FROM mytable t1
WHERE t1.member_id = t.member_id AND t1.date_of_added > t.date_of_added
)
As you want to get the only one latest record, use DESC and LIMIT = 1 as below:
SELECT * from m_details WHERE member_id = 101 ORDER BY date_of_added DESC LIMIT 1;
As member_id can have duplicate value(according to your given dataset), you can identify m_id column as unique and (auto) increasing value(it seems so) and sort based on that column in descending order to get the updated value and then just pick the first row by setting LIMIT 1. So write your query removing group by clause:
SELECT * from m_details WHERE member_id=101 order by m_id DESC LIMIT 1;
Hope, this will help.
N.B.: It seems that new entries can't have lower date_of_added value than previous as there is no specific info about that, otherwise sort by date_of_added

Php+Mysql, order by id + start with specific id

ID | NAME
1 | yassine
2 | yask
3 | oisus
4 | iouosiud
5 | iouous
$query = mysql_query("SELECT * FROM table ORDER BY id DESC");
what i want to do is that i want to start with 3 . It should output like this:
3,1,2,4,5.
is it possible ?
This works:
SELECT * FROM table ORDER BY id=3 DESC, id ASC
Arranged the list using ORDER BY.
SELECT * FROM `table` ORDER BY (id=3) ASC
No need to use union!

how to use order by field as well sort by id

i have the lot of data and i have to short data that has on Approval and Approval data with
short id descending.
Like the following query:
$query = "select * from event ORDER BY FIELD(status, 'Approval') desc";
But when new data added in event table. as when new data added in table its status is on Approval. and when i show the data .data is not sorted with event id descending.
I am confused how to use both order by field and order by id descending please help
Thanks and Regards
You can do as following if you want the Approval to appear first and then by descending order of id
select * from event
ORDER BY FIELD(status, 'Approval') desc ,event_id desc
Here is a real time example
mysql> select idusers from users where status = 'paid' order by idusers desc limit 1 ;
+---------+
| idusers |
+---------+
| 150949 |
+---------+
1 row in set (0.00 sec)
Above is to get the most recent paid user
mysql> select idusers,status from users order by field (status,'paid')desc ,
idusers desc limit 3;
+---------+--------+
| idusers | status |
+---------+--------+
| 150949 | paid |
| 150948 | paid |
| 150947 | paid |
+---------+--------+
In the above the most recent is the first row followed by others.

Select last two rows from mysql php

I have a database like this:
+----------+----------+------+
| username | password | time |
+----------+----------+------+
| a | b | 1234 |
| c | d | 5678 |
| e | f | 9012 |
+----------+----------+------+
Now I have to arrange this by time so I ran:
mysql_query("SELECT * FROM `table` ORDER BY `time`")
This showed me rows arranged by time in ascending order, but I have to get only username c and e or I have to get the last two rows from the query.
I have tried:
mysql_query('SELECT * FROM `table` ORDER BY `time` LIMIT 2')
But this showed me usernames a and c but I have to get the last two, how to do that?
SELECT * FROM table
ORDER BY time DESC
LIMIT 2
SELECT * FROM table ORDER BY time DESC LIMIT 2
Adding the DESC keyword will sort the results in descending order.
Try this:
SELECT * FROM table ORDER BY time DESC LIMIT 2
Try
mysqli_query('SELECT * FROM table ORDER BY time DESC LIMIT 2')
Assuming you have an id field with AUTO_INCREMENT set.
To get the last two from the table.
SELECT * FROM table
ORDER BY time DESC
LIMIT 2

Display MySQL results by date

I have this mysql Table:
+--------------------+---------+-------+
| date | query | count |
|--------------------+---------+-------|
|2012-11-18 09:52:00 | Michael | 1 |
|2012-11-18 10:47:10 | Tom | 2 |
|2012-11-17 15:02:12 | John | 1 |
|2012-11-17 22:52:10 | Erik | 3 |
|2012-11-16 09:42:01 | Larry | 1 |
|2012-11-16 07:41:33 | Kate | 1 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and so on. I can simply take results and order them by date in one row via this code:
$queries = mysql_query("SELECT * FROM my_tables ORDER BY date DESC LIMIT 20");
while($row = mysql_fetch_array($queries)){
echo "Name ".$row['query']."";
}
But how to display elements from table ordered by specific date like this:
In 2012-11-18:
Michael
Tom
In 2012-11-17:
John
Erik
In 2012-11-16:
Larry
Kate
and so on. Thanks!
Here is teh PHP code:
$query = mysql_query("SELECT date, query FROM table6 ORDER BY date DESC LIMIT 20");
$group_date = null;
while ($row = mysql_fetch_assoc($query)) {
if ($group_date !== substr($row["date"], 0, 10)) {
$group_date = substr($row["date"], 0, 10);
echo "<h1>$group_date</h1>\n";
}
echo "${row['query']}<br>\n";
}
Output:
2012-11-18
Tom
Michael
2012-11-17
Erik
John
2012-11-16
Larry
Kate
Note that while this code "groups" rows by one column, it can easily be extended to group rows by multiple columns. Left as an exercise.
+1 good question, THIS QUESTION IS NOT SIMPLY HOW TO ORDER A QUERY!!
I think this can be done using GROUP_CONCAT function. this will combine the matching results when you group them and seperate them by commas. ONce you have the results you can explode or do whatever you want
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php
Your going to have to convert and group by date, and group_concat the names. then (in php or whathaveyou) explode the names by commas, and echo the date followed by the names for each result.
edit Looks like you wanted a PHP way to solve this? oops
Use this:
SELECT * FROM my_tables WHERE date > "2012-11-16" ORDER BY date LIMIT 4
Try the WHERE statement:
select * from my_table where date_column > "2012-11-18 00:00:00" and date_column < "2012-11-18 23:59:59" order by date_column
What about
SELECT * FROM my_tables GROUP BY YEAR(date), MONTH(date), DAY(date) ORDER BY date DESC LIMIT 20

Categories