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!
Related
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
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
I found another thread with this similar question but the query wasn't working for me for some reason or another.
I have a table like so:
id | 1 1 5 3 5
I need to use just SQL to echo out the most duplicated number
For instance, that would output:
id | 1 5 3 5
If that makes sense.
How would I achieve this?
Thank you so much
RETURNING JUST UNIQUE ID's
SELECT DISTINCT id FROM myTable
ID |
---------------
1 |
5 |
3 |
See DEMO
RETURNING JUST THE MOST DUPLICATED ID WITH COUNT
SELECT id, COUNT(id) AS Duplicates FROM test
GROUP BY id
ORDER BY Duplicates DESC
LIMIT 1;
ID | Duplicates
---------------
1 | 2
// without the LIMIT clause
ID | Duplicates
---------------
1 | 2
5 | 2
3 | 1
See DEMO
Or, as you see above, there may be TWO ID's that have been duplicated the same amount of times. You could do this, which would return both the highest duplicated ID's, if they're equal:
SELECT id, COUNT(id) AS Duplicates
FROM test
GROUP BY id
HAVING COUNT(id) = (
SELECT COUNT(id) AS great
FROM test
GROUP BY id
ORDER BY great DESC
LIMIT 1
)
ID | Duplicates
---------------
1 | 2
5 | 2
See DEMO
RETURNING JUST UNIQUE ID's IN PURE PHP
$results = // query
$results = array_unique($results);
Use a group by together with a count, like this:
select * from t group by id order by count(id) desc
And add a limit clause in order to get the single most duplicated value:
select * from t group by id order by count(id) desc limit 1
+----+
| id |
+----+
| 1 |
+----+
| 2 |
+----+
| 3 |
+----+
| 4 |
+----+
+----+
| 5 |
+----+
How do I show records from a specific ID so the list would show 2,3,4,5 or 3,4,5?
I figured to do with two queries in UNION but end up showing 2,1,3,4,5 or 2,5,4,3,1.
Did you mean
SELECT * FROM table WHERE id IN (2,3,4,5)
SELECT * FROM table WHERE id >= $id ORDER BY id ASC LIMIT 4
In the second query $id is the input from the user. Also instead of LIMIT 4, you can have the 4 taken as user input
Maybe
$sql = "SELECT * FROM table WHERE id >= $curID ORDER BY id ASC LIMIT 4";
If you want to specify how many entries to find, you can use:
$sql = "SELECT * FROM table WHERE id >= $curID ORDER BY id ASC LIMIT $number";
Be sure you sanitize the inputs before plugging them into the query. See this post for more info:
What's the best method for sanitizing user input with 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.