How to display the last records of the user - php

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

Related

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!

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

How to sort with specific order in mysql query?

I have following sample table and one status field.
Name | status
-------------
Ab | 2
Xy | 0
Pq | 3
Rs | 1
I would like to execute query and would like to sort in this way.
Name | status
-------------
Rs | 1
Ab | 2
Pq | 3
Xy | 0
Is it possible in MySql query?
Note : Here status field is ENUM.
You can sort by the status but put 0 at the end by the first order condition.
In MySQL you can do (since boolean results are evaluated to 1 and 0)
select * from your_table
order by status = 0,
status
in other DB engines you can use
select * from your_table
order by case when status <> 0 then 1 else 2 end,
status
you can also try
order by find_in_set(`status`, '1,2,3,0');
I got Four solutions for this:-
SELECT * FROM b ORDER BY FIELD(status,3,0);
SELECT * FROM b ORDER BY find_in_set(status, '1,2,3,0');
SELECT * FROM b ORDER BY status = 0,status;
SELECT * FROM b ORDER BY CASE WHEN status <> 0 THEN 1 ELSE 2 END,
status;
Here is the MySQL query to sort the data in ASC or DESC order
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

SQL Counting and ordering by duplicate rows

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

SQL query to select only the maximum items?

I have this table: I want to search by UID
ID | VID | UID
1 | 1 | 5
1 | 1 | 6
1 | 2 | 6
2 | 3 | 5
2 | 3 | 6
2 | 4 | 6
I want to end up with this result:
ID | VID | UID
1 | 2 | 6
2 | 4 | 6
In other words, only select the entries where the VID is MAX of the UID but keeping in min NID could differ. Something like this I suppose:
select * from TABLE where uid = 6 and max(vid)
???
But this doesn't work?
One way is to order by the value in descending order (so the max is at the top), then just select the first result.
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
ORDER BY t.VID DESC
LIMIT 1
Or do you mean you want all rows where t.VID is the highest value? In which case you could do something like this,
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
AND t.VID = (SELECT MAX(VID) FROM table);
EDIT: Based on the edit to your question, it looks like you just want the max VID value for each ID? If I'm understanding you correctly, then this should give you what you need.
SELECT t.ID,
max(t.VID) as VID,
t.UID
FROM table t
WHERE t.UID = 6
GROUP BY t.ID
You need to have a subquery. This should work:
select * from TABLE where ID='1' AND VID=(select max(VID) from TABLE)
I expect your real-life example is more complicated (at least has more data).
This query will give you the row you want.
SELECT id,vid, uid
FROM TABLE
where id = 1
and vid in (select max(vid) from TABLE where id = 1)

Categories