I want to find the last inserted row that matches a query (ie, find the row that has the largest id among the matching rows.)
for instance, suppose the following query matches 3 rows. with ids, 1,2,and 3. I want to get only the row with id 3.
SELECT * FROM `table` WHERE `mail` = 'someone#example.com'
How do I do this?
You need to write your query like this
SELECT *
FROM table_name
WHERE `mail` = 'someone#example.com'
ORDER BY id DESC
LIMIT 1
Simply use Order By. You sort your result with Id values in decreasing order (that way, you would have maximum ID at the top, in this case 3) and then just limit your result with value 1. That would give you only one row, with max ID. So,
here goes the query:
SELECT * FROM *YourTableName* where mail = '*YourMail*'ORDER BY id DESC LIMIT 1;
Query
SELECT * FROM tbl
WHERE `mail` = 'someone#example.com'
AND id=
(
SELECT MAX(id) FROM tbl
WHERE `mail` = 'someone#example.com'
);
Fiddle demo
Related
I have a simple table with ID, STATUS, DATE columns, the rows in the table are ordered by DATE, I want to get all the rows until a specific ID is reached, and then stop, something like:
SELECT FROM myTable WHERE `DATE` <= '2017-10-09' ORDER BY `DATE` ASC UNTIL? `ID` = 119;
I like to know if that is possible somehow, to stop on a specific ID, whatever the ID was..
Thanks.
EDIT EXPLAINING
I want to select rows that are ordered under any column, but stop when a specific provided ID is reached. in the above image the result should be all the rows except the ones below the row 119.
I hope it's clear now.
Something like this might work:
SET #marker = NULL;
SELECT *
FROM myTable
WHERE `DATE` <= '2017-10-09'
AND ISNULL(#marker := IF(id = 119, 1, #marker))
ORDER BY `DATE` ASC;
You should phrase the query to select records whose date is less than the date of the record for which ID = 119:
SELECT *
FROM myTable
WHERE DATE <= (SELECT DATE FROM myTable WHERE ID = 119);
Tim Biegeleisen's answer is correct, but if you have no time in your date field then the order by is going to use the date and then any ordered indexes you have specified. So you will get all of the records back that equal the date of the id specified in the subquery's WHERE clause.
So for example, you are going to also return the rows for ids 200-202, no way around that unless you provide more precision on your date OR add the id field to your order by clause, in which case you'll need to be comfortable excluding all IDs above the specified ids integer value for the same date.
I want to select rows starting from a row like this:
SELECT * FROM table WHERE Type=1 LIMIT $Start,infinity
If you have a primary key say id, you can use
SELECT * FROM table WHERE Type = 1 AND id >= $Start;
There is nothing to indicate infinity in MySQL. To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter:
SELECT * FROM table WHERE Type=1 LIMIT $Start, 99999999999999999999;
In a give table I want to select the row with the maximum value in column-A, but in the same time if there are 2 or more rows that have a maximum value a want to get the row where the value in column-B is not null, if there is one, otherwise I just get the first row with the maximum value even if the value in Column B is null.
In summary:
first choice : Column-A maximum, Column-B Not null
in case such a row does not exist
second choice : Column-A maxim, Column-B Null
Is it possible to write a single query with this constrain or do I have to create 2 queries with some logic in between?
You can use multiple columns with ORDER BY like below
SELECT columnA,columnB
FROM YourTable
ORDER BY columnA DESC, columnB DESC
It'll order by columnA first descending order, then columnB in descending order the null will be the last choice.
add a LIMIT 1 at end of query if you just one row returned.
Below Query will do the trick
SELECT * FROM tableA where columnA=(select max(columnA) from tableA) order by columnB desc
You could use an inline view to return the maximum value of col_a, and then do a join to get all the rows that have the same col_a value, then get the maximum value of col_b from those rows.
For example:
SELECT s.col_a
, MAX(t.col_b) AS col_b
FROM ( SELECT MAX(r.col_a) AS col_a
FROM mytable r
) s
JOIN mytable t
ON t.col_a = s.col_a
I'm fairly new to SQL so this may be fairly simple but I'm trying to write a script in SQL that will allow me to get a set of data but I don't want the first or last result in the query. I can find lots on how to remove the first result and how to remove the last result but not both.
This is my query so far:
SELECT * FROM itinerary Where ID = 'A1234' ORDER BY DateTime ASC
I want to remove the first and the last record of that select based on the DateTime.
This may not be the most performant way to do this, but you didn't give any schema info, and it looks like your ID column is not unique. It would be easier if you had a primary key to work with.
SELECT * FROM itinerary
WHERE ID = 'A1234'
AND DateTime <
(SELECT MAX(DateTime) FROM itinerary WHERE ID = 'A1234')
AND DateTime >
(SELECT MIN(DateTime) FROM itinerary WHERE ID = 'A1234')
ORDER BY DateTime ASC
This will basically select every record where the ID is A1234 and the DateTime doesn't equal the max or min datetime. Please note, if you have multiple records with the same value for DateTime and that also happens to be the min or max value, you might exclude more than just the first or last.
This might be good enough though. If not, you might need to write a stored procedure and not just straight ANSI SQL.
Try this ..
select * from
(select a.*,row_number() over (partition by DateTime order by DateTime desc) as rnm
from itinerary Where ID = 'A1234')x
where rm <> 1 and rm not in (
select max(rm) from
(
select row_number() over (partition by DateTime order by DateTime desc) as rnm
from itinerary Where ID = 'A1234'))
Select in reverse order & skip first and then select in the required order from the result, skipping first.
SELECT * FROM (SELECT *
FROM itinerary
Where ID = 'A1234'
ORDER BY DateTime DESC
LIMIT 1, 18446744073709551615) x ORDER BY DateTime ASC
LIMIT 1, 18446744073709551615
18446744073709551615 is max integer just in case you wanted to know why I picked that value
I want to select all the rows from my table except the first 20 rows. How it possible? The total number of rows are not static.
SELECT statistics_id,title, user_name FROM (
SELECT statistics_id,title, user_name FROM statistics ORDER BY statistics_id DESC
LIMIT(select count(*)from statistics )-20
) sub
ORDER BY access_statistics_id ASC
I know 'LIMIT(select count(*)from statistics )-20' is not a correct method. Please help.
the documentation says (https://dev.mysql.com/doc/refman/5.5/en/select.html) the following:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
so you could use something like
LIMIT 20, veryLargeNumber
Try this
DECLARE v_max bigint unsigned default ~0;
SELECT statistics_id,title, user_name
FROM statistics
LIMIT 20, v_max;
After writing the select query u just have to include,
LIMIT 21,100;
21-Offset i.e from which row you want to start selecting and
100- is the Limit[Which you can set according to your need]
You actually need:
SELECT
statistics_id, title, user_name
FROM statistics
ORDER BY
statistics_id ASC
LIMIT 20, 18446744073709551615;
As per MySQL Documentation
To retrieve all rows from a certain offset up to the end of the result
set, you can use some large number for the second parameter. This
statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
If your table grows fast, selecting all rows (with exception of first 20) is not a good idea. In such case, you should batch your query, and handle a subset of entries at a time, some thing like:
SELECT * FROM tbl LIMIT 20,120;
Try using the offset option for the LIMIT syntax. you can read more about the LIMIT syntax at http://dev.mysql.com/doc/refman/5.0/en/select.html.
SELECT `statistics_id`
, `title`
, `user_name`
FROM `statistics`
ORDER BY `statistics_id` ASC
LIMIT 20, 18446744073709551615