I have a query like
Select *
From y
WHERE y.z = (
SELECT a, (adding rownumber here)
FROM b
)
I want to add a clause where it only selects every second row. To do this I need to add row_number() to the subquery, and have a clause where rownumber % 2 = 0.
My question is, am I able to add rownumber to the select of the subquery and somehow hide it so it doesn't affect the query
Rownumbering in MySQL is a notorious pain in the neck.
You can number your rows in MySQL like this.
SELECT (#rownum := #rownum+1) rownum, b.*
FROM b
JOIN (SELECT #rownum := 0) init
ORDER BY b.whatever
Don't forget the ORDER BY clause here. Without explicit ordering the query engine is free to randomize the order of rows it returns.
Then, you can use that mess as a subquery and do things with the rownum.
SELECT *
FROM (
SELECT (#rownum := #rownum+1) rownum, b.*
FROM b
JOIN (SELECT #rownum := 0) init
ORDER BY b.whatever
) table_with_rownum
WHERE rownum % 2 = 0
If you don't want to show the rownumbers, change your SELECT from SELECT * to SELECT col, col, col and leave out rownum.
Related
Is it possible to create a counter in mysql/mariadb in one single SELECT-statement. I've tried the following but it returns only the value 1 in the first column:
SELECT #rownr := IF(ISNULL(#rownr),0,#rownr)+1 AS rowNumber, * FROM table_x LIMIT 0,10
If I run the statement more often in the same mysql-instance it starts counting from the last number. So the second time it starts at 2, the third time at 12. This means that the variable is created but seems to be only available for modification when it was instantiated before the SQL statement was issued.
It is possible, but a bit tricky. First, you need to declare the variable outside of the select clause (in a separate set assignment, or in a derived table). Also, it is safer to sort the rows in a subquery first, and then compute the variable.
I would recommend:
set #rn := 0;
select t.*, #rn := #rn + 1 rowNumber
from (select t.* from mytable t order by id limit 10) t
Note that I added an order by clause to the inner query, otherwise it is undefined in which sequence rows will be ordered (I assumed id).
Alternatively, you can declare the variable in a derived table:
select t.*, #rn := #rn + 1 rowNumber
from (select t.* from mytable t order by id limit 10) t
cross join (select #rn := 0) x
Finally: if you are running MySQL 8.0, just use row_number():
select t.*, row_number() over(order by id) rn
from mytable t
order by id
limit 10;
You don't have an order by, so the ordering is indeterminate. But you can initialize the parameter in the statement itself:
SELECT #rownr := (#rownr + 1) AS rowNumber, x.*
FROM table_x x.CROSS JOIN
(SELECT #rownr := 0) params
LIMIT 0, 10;
If you want a particular ordering, you should use an order by in a subquery.
Also note that starting in MySQL 8, variable assignments in SELECT are deprecated. You should be using window functions (row_number()) in more recent versions.
Let's say there are millions of records in my_table.
Here is my query to extract rows with a specific name from list:
SELECT * FROM my_table WHERE Name IN ('name1','name2','name3','name4')
How do I limit the returned result per name1, name2, etc?
The following query would limit the whole result (to 100).
SELECT * FROM my_table WHERE Name IN ('name1','name2','name3','name4') LIMIT 100
I need to limit to 100 for each name.
This is a bit of a pain in MySQL, but the best method is probably variables:
select t.*
from (select t.*,
(#rn := if(#n = name, #rn + 1,
if(#n := name, 1, 1)
)
) as rn
from my_table t cross join
(select #n := '', #rn := 0) params
order by name
) t
where rn <= 100;
If you want to limit this to a subset of the names, then add the where clause to the subquery.
Note: If you want to pick certain rows -- such as the oldest or newest or biggest or tallest -- just add a second key to the order by in the subquery.
Try
SELECT * FROM my_table WHERE Name IN ('name1','name2','name3','name4') FETCH FIRST 100 ROWS ONLY
SELECT * FROM ( SELECT #row := #row +1 AS rownum, [column name] FROM ( SELECT * FROM [table name] ) WHERE rownum % 5 = 1
This does indeed return every 5th row, but in ascending order. What I want is that it first gets all the data, put them in descending order and THEN apply the filter.
If you filter it first and then put it in descending order, it will not start with the latest data added (4/5th of the time).
I would like to know how one should do this.
Thanks in advance
Edit: For people with the same problem, this is what I used:
SELECT * FROM
(SELECT rank, id, Temperature FROM
(SELECT *, #rownum := #rownum + 1 AS rank FROM temperature_room1,
(SELECT #rownum := 0) r) AS T ORDER BY id DESC) AS J WHERE rank % 5 = 1
Select everything from:
Select rank, id and Temperature from:
Select everything and rownumber as rank from the table as t ordered by ID in descending order
Finally, only output the row numbers which can be divided by 5 and the remainder is 1
Don't quote me on this, I'm a big noob regarding SQL stuff. It works for me, so I'm happy.
seems like you just need an order by dec on the desired column in one of the three queries. I think the second one as order by applies to the select at the same level. ans since you want your rownum ordered desc... seems like that's the place...
SELECT *
FROM ( SELECT #row := #row +1 AS rownum, [column name]
FROM ( SELECT * FROM [table name] )
ORDER BY [column name] desc
)
WHERE rownum % 5 = 1
I have a table with columns like this:
id | timestamp | ...
and I am looking for rows where the timestamp decreased since the previous row.
I tried a statement like this:
SELECT count(a.id)
FROM tbl AS a INNER JOIN tbl AS b ON a.id+1=b.id
WHERE a.timestamp<b.timestamp;
but it appears not to have worked. I get zero results even though I expect some. Any suggestions what is wrong?
I would also appreciate any ideas on a better way to write this query.
I am using MySQL.
You can get the previous value using a correlated subquery, and then use that for the comparison:
select t.*
from (select t.*,
(select t2.timestamp from tbl t2 where t2.id < t.id order by t2.id desc limit 1
) as prevts
from tbl t
) t
where timestamp < prevts;
The problem with your query is probably that the ids have gaps in them.
EDIT:
You can do this with variables. The challenge is getting the variable comparison and assignment in a single expression. This is needed because MySQL does not guarantee the order of evaluation of expressions in a select statement.
The following assigns a value to IsDecreasing and assigns the values:
select t.*
from (select t.*,
if(#prev > timestamp, if(#prev := timestamp, 1, 1),
if(#prev := timestamp, 0, 0)
) IsDecreasing
from tbl t cross join
(select #prev := -1) vars
order by id
) t
where IsDecreasing = 1;
This should be faster than the previous method -- probably even when you have the right index.
First I want to make clear this is not a simple LIMIT x,y question. I want to know if it is possible to do a query like the following peuso query.
SELECT *, OFFSET_OF_ROW()
FROM `table`
WHERE `some_column` = someValue
ORDER BY `some_other_column`;
the pseudo function OFFSET_OF_ROW() should give the number of rows which would come before the selected row (+1) if there was no condition `some_column = someValue`
This isn't particularly efficient, but it will do what you want:
select #rownum := 0;
select * from (
select #rownum := #rownum + 1, id, some_column, sortcol
from `table`
order by `sortcol`
) all_rows
where `some_column` = someValue;