How to use Sql query for generating S.no from 1. I can't get the answer correctly
SELECT (#cnt := #cnt + 1) AS 'S.No',`barcode`,
(SELECT #cnt := 0) AS dummy
FROM wp_weblib_outitems
Try this:
SELECT #s:=#s+1 AS 'S.No',`barcode`
FROM wp_weblib_outitems,
(SELECT #s:= 0) AS s;
This is my query. i just want S.No as asc . How can i write this query as ascending order
SELECT DISTINCT (subject),a.title, a.callnumber,
a.author, a.recv_date,a.pubdate ,b.book_access_number,(#cnt := #cnt + 1) AS 'S.No'
from wp_weblib_collection a
inner join wp_weblib_book_log_reports b on a.barcode = b.book_access_number
$student
CROSS JOIN (SELECT #cnt := 0) AS dummy
GROUP BY subject asc
You can try with ROW_NUMBER() as given below.......
SELECT ROW_NUMBER() OVER (ORDER BY [Column1]) AS 'NO',[Column1],[column2],.... FROM tblUser;
Related
I have two sql queries. But in Java I can't set variables.
I tried to summarize it to one query. But that not works, because the sql syntax is wrong.
SET #rn = 0;
SELECT *
FROM (SELECT t.id, #rn := #rn + 1 AS rank
FROM stats t
ORDER BY t.points DESC) t2
WHERE t2.id = ?;
If only the SET part is the problem you can do
SELECT t.id, #rn := #rn + 1 AS rank
FROM stats t
CROSS JOIN ( SELECT #rn := 0 ) as parameters
ORDER BY t.points DESC
You can also check this tutorial http://www.mysqltutorial.org/mysql-row_number/
You need to do a join like this :
SELECT stats.id, #rn := #rn+1 AS rank
FROM stats, (SELECT #rn:=0) a
WHERE stats.id = ?
If you have mysql 8.0 you can use: ROW_NUMBER() or RANK() :
SELECT
id,
ROW_NUMBER() OVER w AS 'row_number',
RANK() OVER w AS 'rank',
FROM stats
WHERE stats.id = ?
WINDOW w AS (ORDER BY points);
I am trying to get the rank of specified record, and I have some success with this code:
SELECT `rank`
FROM
(
select #rownum:=#rownum+1 `rank`, p.*
from TableName p, (SELECT #rownum:=0) r
order by point DESC
) s
WHERE names = 'house'
(See the schema here.)
This SQL query works, but if I want to get the result according to city_id and name, I must use two where clauses, and then the code doesn't work.
I want to get rank of house only for its city. How can I do this?
I want to get rank of "house" only for its city
You can do this by introducing another variable to keep track of the city:
SELECT `rank`
FROM (select p.*,
(#rn := if(#c = city, #rn + 1,
if(#c := city, 1, 1)
)
) as rank
from TableName p CROSS JOIN
(SELECT #rn := 0, #c := -1) params
order by city_id, point DESC
) s
WHERE names = 'house' ;
You can also use your original query, with a tweak, if you know that "house" only appears once in the data:
SELECT `rank`
FROM (select p.*, (#rn := #rn + 1) as rank
from TableName p CROSS JOIN
(SELECT #rn := 0) params
where city_id = (select city_id from TableName t2 where t2.names = 'house')
order by point DESC
) s
WHERE names = 'house' ;
I have the table with data:
And for this table I need to create pegination by productId column. I know about LIMIT N,M, but it works with rows and not with groups. For examle for my table with pegination = 2 I expect to retrieve all 9 records with productId = 1 and 2 (the number of groups is 2).
So how to create pegination by numbers of groups ?
I will be very thankfull for answers with example.
One way to do pagination by groups is to assign a product sequence to the query. Using variables, this requires a subquery:
select t.*
from (select t.*,
(#rn := if(#p = productid, #rn + 1,
if(#rn := productid, 1, 1)
)
) as rn
from table t cross join
(select #rn := 0, #p := -1) vars
order by t.productid
) t
where rn between X and Y;
With an index on t(productid), you can also do this with a subquery. The condition can then go in a having clause:
select t.*,
(select count(distinct productid)
from t t2
where t2.productid <= t.productid)
) as pno
from t
having pno between X and Y;
Try this:
select * from
(select * from <your table> where <your condition> group by <with your group>)
LIMIT number;
I have table with id (store user id) and score in different match. I want what is the position of a user.
So for i try this sql fiddle;
in this I am getting all the row but I need only user having id 3 and it position in the table.
like this:
Score Postion
26 3
Even i try to do like this but no success
MySql: Find row number of specific record
With MySQL, how can I generate a column containing the record index in a table?
I got the answer: http://sqlfiddle.com/#!2/b787a/2
select * from (
select T.*,(#rownum := #rownum + 1) as rownum from (
select sum(score) as S,id from mytable group by id order by S desc ) as T
JOIN (SELECT #rownum := 0) r
) as w where id = 3
Updated sqlfiddle and above query. Now it is working perfectly.
I think this should do the trick:
SELECT totalScore, rownum FROM (
SELECT id,sum(score) AS totalScore,(#rownum := #rownum + 1) AS rownum
FROM mytable
JOIN (SELECT #rownum := 0) r
group by id) result
WHERE result.ID = 3;
just add a where clause
select x.id,x.sum,x.rownum
from(
select id,sum(score) as sum,(#rownum := #rownum + 1) as rownum
from mytable
JOIN (SELECT #rownum := 0) r
group by id
) x
where id =3
I want to use the "order by rand alternative" query (bottom) to get a random set of results but I want to get them from within the results a query such as:
SELECT t2.id FROM index_table t1 JOIN data_table t2 ON t1.id= t2.index_id
And I need to limit the number of random results I'd get back.
I can't quite get my head around the syntax I'd need to use, any help greatly appreciated.
thanks
"order by rand alternative" query:
How can i optimize MySQL's ORDER BY RAND() function?
SELECT *
FROM (
SELECT #cnt := COUNT(*) + 1,
#lim := 10
FROM t_random
) vars
STRAIGHT_JOIN
(
SELECT r.*,
#lim := #lim - 1
FROM t_random r
WHERE (#cnt := #cnt - 1)
AND RAND(20090301) < #lim / #cnt
) i
SELECT
t2.id
FROM
index_table t1
JOIN data_table t2
ON t1.id= t2.index_id
ORDER BY
RAND()
LIMIT 5
or whatever the maximum number of entries you want via the limit
Would something like this work?
SELECT *
FROM (
SELECT #cnt := COUNT(*) + 1,
#lim := 10
FROM FROM index_table t1 JOIN data_table t2 ON t1.id= t2.index_id
) vars
STRAIGHT_JOIN
(
SELECT t2.id,
#lim := #lim - 1
FROM index_table t1 JOIN data_table t2 ON t1.id= t2.index_id
WHERE (#cnt := #cnt - 1)
AND RAND() < #lim / #cnt
) i
I removed the parameter from RAND otherwise my output was always the same.