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
Related
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;
I'm fighting a bit with a query I'm building. Let's say I've got a DB table like this:
id | some_string
----------------
1 | 'lala'
2 | 'jeje'
3 | 'poopoo'
4 | 'wicked wicked'
I now want to get the last three records (2, 3, and 4) ordered ascending by key. I tried this:
SELECT * FROM tableName LIMIT 3 ORDER BY id ASC
This gets me the first three records, instead of the last three. I can of course also use the query below, which gets me the correct records, but then I don't get them in Ascending order:
SELECT * FROM tableName LIMIT 3 ORDER BY id DESC
Does anybody know how I can get the last three records in an ascending order? All tips are welcome!
select * from (
select * from table_name order by id desc limit 3
) last_3_rows
order by id
Sort on the resulting result set ie. do a select * from (<your query here>) order by id
This is a query inside another query. that reorders your query.(SQL - How to reorder a select query that uses the limit constraint)
select * FROM (SELECT * FROM tableName LIMIT 3 ORDER BY id DESC) AN_UNUSUAL_NAME ORDER BY id ASC
I try to program a solution where I have multiple linked tables.
Now I have another problem:
I want to limit count of returned lines to 1000.
But I want to show ID 1-1000, next page 1001-2000.
IDs may be stored in iregular order in database (ID 1 does not have to be the first row)
Not any problem so far:
SELECT * FROM table ORDER BY id ASC LIMIT 1000
BUT
Now I have to sort resulty by another column:
SELECT * FROM table ORDER BY name ASC LIMIT 1000
which might return other IDs than 1-1000
or I do
SELECT * FROM table ORDER BY id ASC, ORDER BY name ASC LIMIT 1000
But this will only sort by ID an then by name. So if I would have any ID as duplicate (which is not possible) I would then have those sorted by name.
How can I achive that I get the first 1000 IDs (some IDs might not exist as they might have been deleted before!) and those thousand rows sorted by name?
Is that even possible?
Try this:
SELECT * FROM table WHERE id IN (
SELECT ID FROM table ORDER BY ID ASC LIMIT 0, 1000
) ORDER BY name ASC
As mentioned in comments that subquery is not supported, 'This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'. Using JOINs is the only alternative left, like this:
SELECT table.* FROM tab JOIN (
SELECT ID FROM table ORDER BY ID LIMIT 1000
) temp
ON table.ID = temp.ID
ORDER BY table.name
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query
I have a MySQL table with an auto-increment field and I am trying to figure out how to get the next or previous value along with the rest of that row using purely MySQL. The field is AI but the numbers aren't necessarily consecutive as rows get deleted all the time.
field_id
8, 15, 17
This is what I started with:
//to get next value in table:
SELECT MIN(member_id) AS val FROM members WHERE member_id > 15 LIMIT 1
//to get previous value in table:
SELECT MAX(member_id) AS val FROM members WHERE member_id < 15 LIMIT 1
But that was only returning the member_id value. This works to get the next value (but doesn't make a difference if I use DESC or ASC (wtf):
SELECT MIN(member_id),members.*
AS val FROM members
WHERE member_id > 15
ORDER BY member_id DESC
LIMIT 1
That query reversed (to get the the previous value) always returns the lowest value in the table (1):
SELECT MAX(member_id),members.*
FROM members
WHERE member_id < 15
ORDER BY member_id ASC
LIMIT 1
These two queries, however, shows what I want but I actually am not entirely sure why:
//get next LOWEST row
SELECT MAX(member_id),members.*
FROM members
WHERE member_id < 15
GROUP BY member_id
ORDER BY member_id DESC
LIMIT 1
//get next HIGHEST row:
SELECT MIN(member_id),members.*
FROM members
WHERE member_id > 15
GROUP BY member_id
ORDER BY member_id ASC
LIMIT 1
I'm assuming GROUP BY allows me to pull more than one row from the query? Is there a better way to do this?
//to get next value in table:
SELECT * FROM members WHERE member_id > 15 ORDER BY member_id LIMIT 1
//to get previous value in table:
SELECT * FROM members WHERE member_id < 15 ORDER BY member_id DESC LIMIT 1
MIN() and MAX() are for getting the minimum or maximum value in a column for a group of same values in another column. For example getting a student's minimum and maximum grade. You would GROUP BY the Id of the student and MySQL would give you a single row with those two values.
As #AndreKR shows in his answer. MIN() and MAX() don't affect the where statement.
I'm not entirely sure what you're trying to do ... Why are you only looking at the first and last 15 rows. If you're trying to get the last value used by auto_increment after you do an INSERT you can run this query:
SELECT LAST_INSERT_ID(); and it will return the ID of the last INSERT statement (In that thread!). Auto increment always counts up so it will not reuse rows that have been deleted. The next ID is always the value of LAST_INSERT_ID() + 1.
Hope that helps.
Well that was easy. Thanks AndreKR, but a small correction:
//next value
SELECT * FROM members WHERE member_id > 15 LIMIT 1
//previous value
SELECT * FROM members WHERE member_id < 15 ORDER BY member_id DESC LIMIT 1