mysql show position in resultset - mysql

So I have a list of say 10 rows. Each with an key and value.
Is there a way in mysql where I can, get a rows position in a given resultset:
SELECT 'resultSetPosition' FROM table WHERE rowKey=x ORDER BY rowValue
id value
1 a
2 b
3 c
o/p required
if id 3 the i need to get position of that row is 3
EDIT: I only want to get one row out in my resultset, but the 'position' should relate to its position for example if you sort by id.
(Obviously this whole thing is easy if I just pull the entire resultset and search the array in my programming, but I wanted to see if it could be done in mysql alone.)

You can try this:-
SELECT *, #rownum := #rownum + 1 AS ROW_NUMBER
FROM TABLE
JOIN (SELECT #rownum := 0) R;
This might help you.

Try this:
SET #rank=0;
SELECT #rank:=#rank+1 AS resultSetPosition
FROM tableName
WHERE rowKey=x
ORDER BY rowValue
Also take a look at this link.

Try with this hope this will clear your problem
select count(*) as pos from table_name where id<=current_id

Related

Wondering why I must assign to session variable in mysql?

Why do I have to assign to a session variable for it to have the right number in a query like this:
SELECT #row_number := #row_number + 1, name FROM cities;
Instead of something like:
SELECT #row_number, name FROM cities;
In the second form it returns what I'm guessing is the last row number. Maybe even the value of a COUNT(*). It's almost as if the value is somehow closed over. What is going on in these two queries?
you have #row_number variable. Everytime the below sql hits the record it shows the result and increments by one.
SELECT #row_number := #row_number + 1, name FROM cities;
if you are using mysql 8.0+, you can use row_number window function to achieve same result
select row_number() over (order by <pk>) rn, name from cities;
If we turn back to SELECT #row_number, name FROM cities;, you are not icrementing #row_number which in turns shows always same value which is assigned value for #row_number
PS: please also note that you are not using order by clause on your query which may lead to inconsistent row numbering.
You need to assign to it in order to add 1 to the value on each row. If you don't do this, you get the same value on every row, which isn't a row number. It will be whatever was left from the last time you assigned the variable, which might be the total number of rows from a previous query that was correctly incrementing.
If you're using MySQL 8.x you can replace this use of session variables with the ROW_NUMBER() function.
Instead of session variable, for MySQL version 8+ you can use ROW_NUMBER() and for below MySQL 8 you can do this
SELECT #row_number := #row_number + 1, name
FROM cities,
(SELECT #row_number:= 0) AS x;

How to easily get row number when using LIMIT in MySQL?

Suppose I have a database table with quite a few rows which I want to display to an user. It would make sense to LIMIT the output, and make pages of rows. In MySQL I would do this:
SELECT * FROM myTable ORDER BY myValue LIMIT 120,10
which will show 10 rows starting from row 120. So MySQL must use, internally, some kind of order, and has numbered the rows accordingly. I would like to display the row number with each row. How do I get access to these numbers, using only MySQL? To be clear, I am looking for something like this:
SELECT *,<LIMIT_ROWNO> FROM myTable ORDER BY myValue LIMIT 120,10
I looked online and in the manual, but I cannot find it. I would prefer something simple, without using variables, or functions. Isn't there a predefined expression for this?
I can solve this problem easily in PHP, but it would be more logical to get the row numbers from MySQL.
You can't do it without using variables, e.g.:
SELECT m.*, #rownum := #rownum + 1 as `num`
FROM myTable m, (SELECT #rownum := 120) a
ORDER BY myValue LIMIT 120,10;
set #rownum=120;
SELECT *,#rownum:=#rownum+1 as rn FROM myTable ORDER BY myValue LIMIT 120,10;
as of final of 2021, why not:
SELECT
t1.*,
COUNT(t1.*) OVER (PARTITION BY RowCounter) as TotalRecords
FROM (
SELECT a, b, c, 1 as RowCounter
FROM MyTable
) t1
LIMIT 120,10
using a subquery with a column marking every row with the same value, will give us the possibility to count all of the same values of the the resulted column with PARTITION BY window function's group

Mysql how to sum column with previous column sum

In XLS I have two columns A and B.
A,B
1,1
2,3
2,5
1,6
5,11
2,13
A column have value, and B column is calculated with formula, (A + (previous row B value))
How can i do this calculation on MYSQL?
I'm trying to join same table twice and i can get previous rows A column next to B.
I can sum them, but how can I sum them with this formula?
XLS formula looks like this:
H20 = H19+G20
This is my SQL created from suggestions.
SELECT
date, time, sum, #b := sum+#b as 'AccSum', count
FROM
(SELECT
t.date, t.time, t.sum, t.count
FROM TMP_DATA_CALC t
ORDER BY t.epoch) as tb
CROSS JOIN
(SELECT #b := 0) AS var
;
SELECT A, #b := A+#b AS B
FROM (SELECT A
FROM YourTable
ORDER BY id) AS t
CROSS JOIN
(SELECT #b := 0) AS var
The user variable #b holds the value of B from the previous row, allowing you to add the current row's A to it.
DEMO
http://sqlfiddle.com/#!2/74488/2/1 shows how to select the data.
SET #runtot:=0;
Select a,b, #runtot:=#runtot+a from b
However there's an underlying problem I can't figure out. Since you don't have a defined order, the SQL could do this ordering in any way, so you may not get the desired results.. without a defined order you results may be unpredictable.
runtot = running total.
In MySQL we don't have any function like partition by which Oracle has. You can use curser to achieve your requirement. Or we can write any function which will get rownumber as input then add these two values then return that to query.
select b from xsl limit rownum-1,1 + select a from xsl limit rownum,1

How to select some rows from MySQL table using theirs numbers?

I have a set of rows (it is actually filtered and ordered mysql table).
I also have a set of integer numbers like (5,9,12,67,2,3) which I want to use as row numbers.
Question: What is better way to fetch rows from this set using these numbers?
Example: I have a set 15 rows. And numbers (1,5,7). How I can get 1st,5th and 7th rows?
Try to use this code
select * from (SELECT *, #rowid:=#rowid+1 as rowid FROM table, (SELECT #rowid:=0) as init WHERE ... order by ...) t where t.rowid in (...set of numbers...)
Sorry I just singed up and don't know the good habit to answer so I'll try this:
If You have auto_increment index use that?
If not you can make arbitrary number like
SELECT #currentRow := #currentRow + 1 AS row_number, *
FROM table, (SELECT #currentRow:=0) as init
WHERE row_number IN 1,5,7
(something is wrong with WHERE)
Yes i got it figured out but #Epsiloncool seemd to do it much faster.
Triple select helps you to get where:
SELECT * FROM
(SELECT *, #currentRow := #currentRow + 1 AS row_number
FROM table t, (SELECT #currentRow := 0) AS init) AS table
WHERE cdees.row_number IN 1,5,7

Row number in mySQL

Is it possible to get the row number in MySQL? Say I have a 'table'
ID tag name
1 A alpha
4 B beta
5 C gamma
8 D ceta
How can I get in MySQL that, for example, 'C' is the 3rd row in that table? Following:
SET #pos=0;
SELECT #pos:=#pos+1,tag FROM table ORDER BY tag ASC;
counts the rows as it should. But (sorry for ignorant code)
SET #pos=0;
SELECT #pos:=#pos+1,tag FROM table where tag='C' ORDER BY tag ASC;
gives 1 row as a result, with pos as 0, as it probably should.
Is there a way to get the 'pos' to be '3' as I need it to be?
(Ordering would be important as well, whether it is relevant to the question or not..)
You can use this
Select rownum from (
SELECT #rownum:=#rownum+1 rownum, t.*FROM (SELECT #rownum:=0) r, table t order by tag) as t1
where tag = 'C'
In case your IDs are strictly increasing with row numbers, you can do
SELECT COUNT(*) FROM tbl WHERE ID <= (SELECT ID FROM tbl WHERE tag = 'C');
I am not sure what you mean by the ordering though.
Side note: Your code
SET #pos=0;
SELECT #pos:=#pos+1,tag FROM tbl where tag='C' ORDER BY tag ASC;
cant work, because here #pos operates on the result set only, which consists of only one record.
Actually, there's one possibility that I didn't consider before:
SELECT count(1) FROM table WHERE tag <= 'C' ORDER BY tag
This seems to do the same thing, and a bit faster, too.. Or am I missing something?