Get last record from group data. MySql - mysql

Can anyone please help to get last record from the group.enter image description here

I think you need this:
select * from t where col = 85 order by id desc limit 1
According to your comment, this should get last records for every group: (this assumes that id is unique and "last record" means record, with highest id)
select t.* from t
inner join (select max(id) as maxid from t group by col) s
on t.id = s.maxid

To fetch the 1 row from mysql use 'limit' keyword.
MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
E.g.:
From your screenshot, subscription id is same for multiple id's you want to get last record which id is greater. The below query gets your result, grouped by subscription_id and ordered by id desc and limiting to 1 makes fetching only 1 row from database.
select * from tableName group by subscription_id order by id desc limit 1

You can used last() function.
SELECT LAST(CustomerName) AS LastCustomer FROM Customers;

Related

want to get total count of records in a table using COUNT(*) in MySQL

I have a mysql query which will return all the details from table along with i need max_row count i.e total no of rows in a table using COUNT(*) in a single select query without using cross join.
Note: MySQL version is earlier version of 8
Query :
SELECT * FROM tablename ORDER BY column name DESC LIMIT 0,10;
The total count of a table is simple, when you want to add it to every row.
SELECT
*
,(SELECT COUNT(*) FROM tablename ) count1
FROM tablename
ORDER BY column name
DESC LIMIT 0,10;,

how to select row in mysql with max id

Hi I am trying to select row in mysql whose visitor id=1 and max id then it should be the last row as I focus in this picture
but it is showing something else, it showing this output
here the mysql code I tried
SELECT *,max(id) FROM activity WHERE visitorid=1
you can do it with:
SELECT * FROM activity WHERE visitorid = 1 ORDER BY id DESC LIMIT 1
this will order your rows (where visitorid = 1) descending by id and select only the first one.
When there exists multiple entries for an id in a table
and you still want to fetch the latest, then
filter the records in descending order of auto incremented primary key and limit to top record.
Example:
select * from activity
where visitorid = 1
order by id desc
limit 1

MySQL - Select row count for matching values only querying the last 20 entries

I'm trying to run a MySQL query as part of a bash script to find out if the last 19 entries all have the same value (and then do something if so). I have an 'id' row that auto increments with each entry and then the row that holds the integer value, 'number'.
My thought was to try the below however this is limiting the number of rows returned to 19 and as the count only returns one row this doesn't work:
mysql --host=localhost --user=uname --database=dbname -s -N -e "select count(*) from table order by id desc limit 19"
I need to limit the actual count itself to only check the last 19 rows. I'm not able to say "where id = x to y" as the table will constantly be growing and so the last 19 id's will always be different.
I would be grateful for any help with this one, thanks!
The limit is not doing anything in your query. The aggregation occurs first, aggregating all the rows to produce one row. Then the limit 19 is applied, which is useless, because there is only one row.
To get the most recent 19 rows, you will want a subquery, so the following will get you a count of at most 19:
select count(*)
from (select t.*
from table t
order by id desc
limit 19
) t;
If you want to get the number of values of some column, you might want count(distinct) instead:
select count(distinct col)
from (select t.*
from table t
order by id desc
limit 19
) t;
Use a subquery.
SELECT COUNT(DISTINCT sq.number)
FROM
(SELECT number FROM TABLE ORDER BY ID DESC LIMIT 19) as sq
As far what I have understood is that you would always be wanting the last 19 records from the table so,
select *
from 'your table name'
order by id desc
limit 19
and if you want distinct values just then type this:
select distinct(column name)
from 'your table name'
order by id desc
limit 19
Hope it helps you.

How can I select the row with the highest ID in MySQL?

How can I select the row with the highest ID in MySQL? This is my current code:
SELECT * FROM permlog WHERE max(id)
Errors come up, can someone help me?
SELECT * FROM permlog ORDER BY id DESC LIMIT 0, 1
if it's just the highest ID you want. and ID is unique/auto_increment:
SELECT MAX(ID) FROM tablename
For MySQL:
SELECT *
FROM permlog
ORDER BY id DESC
LIMIT 1
You want to sort the rows from highest to lowest id, hence the ORDER BY id DESC. Then you just want the first one so LIMIT 1:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement.
[...]
With one argument, the value specifies the number of rows to return from the beginning of the result set
SELECT *
FROM permlog
WHERE id = ( SELECT MAX(id) FROM permlog ) ;
This would return all rows with highest id, in case id column is not constrained to be unique.
SELECT MAX(id) FROM TABLENAME
This identifies the largest id and returns the value
Suppose you have mulitple record for same date or leave_type but different id and you want the maximum no of id for same date or leave_type as i also sucked with this issue,
so Yes you can do it with the following query:
select * from tabel_name where employee_no='123' and id=(
select max(id) from table_name where employee_no='123' and leave_type='5'
)
SELECT MAX(ID) FROM tablename LIMIT 1
Use this query to find the highest ID in the MySQL table.
Since both SELECT MAX(id) FROM table and SELECT id FROM table ORDER BY id DESC LIMIT 0,1 fulfill the goal, the interesting part is, which performs better.
SELECT MAX(id) FROM table: 152ms
SELECT id FROM table ORDER BY id DESC LIMIT 0,1: 25ms
(InnoDB-table with 55M rows on MySQL 8.0, 10 runs, average result)
Of course thats not representive, but gives an idea, that the ORDER BY method performs significantly better.
This is the only proposed method who actually selects the whole row, not only the max(id) field. It uses a subquery
SELECT * FROM permlog WHERE id = ( SELECT MAX( id ) FROM permlog )
SELECT * FROM `permlog` as one
RIGHT JOIN (SELECT MAX(id) as max_id FROM `permlog`) as two
ON one.id = two.max_id

How do I use group by showing the newest row of data

Can someone please tell me how to use the group by clause, grouping by one of the keys in the table but yet having the newest timestamp at the top? I have multiple rows of data but I only want to show the newest row
If you want only the most recent one per group:
SELECT somefield
FROM table t1
WHERE timestamp = (SELECT MAX(timestamp)
FROM table t2
WHERE t1.somefield = t2.somefield);
Or just the latest most recent one:
SELECT somefield
FROM table
GROUP BY somefield
ORDER BY MAX(timestamp) DESC
LIMIT 1;
I think you're looking for the ORDER BY clause.
SELECT Foo.Bar, Foo.SomeTimestamp
FROM Foo
ORDER BY Foo.SomeTimestamp DESC
If you're grouping by a column, you're probably returning aggregate data. If the timestamp is unique for each row of aggregate data, you may need to use the MAX function (or the like) to return a single timestamp for each group. For example:
SELECT Foo.TypeID, SUM(Foo.Price) AS Price, MAX(Foo.OrderDate) AS LastOrder
FROM Foo
GROUP BY Foo.TypeID
ORDER BY MAX(Foo.OrderDate) DESC
If you only want the first row, you can use the LIMIT clause:
SELECT Foo.Bar, Foo.SomeTimestamp
FROM Foo
ORDER BY Foo.SomeTimestamp DESC
LIMIT 0, 1
This starts at row 0 and returns at most 1 row.