I am retrieving data from table but instead of all row, I want 20 rows at a time for pagination. For this I use limit keyword which work perfectly in Mysql but not in Oracle.
Code:
"select "+ "C.CONTRACTOR_ID,C.CONTRACTOR_NAME,nvl(C.CONTACT_PERSON_1,'-'),nvl(C.CONTACT_PERSON_2,'-'),C.REGISTRATION_NO,CRA.DESCRIPTION REG_AUTH_NAME,"+
"to_char(C.VALID_FROM,'dd/mm/yyyy'),to_char(C.VALID_TO,'dd/mm/yyyy'),CC.DESCRIPTION CONTRACTOR_CLASS,C.INCORP_PLACE,"+
"IT.DESCRIPTION INCORP_TYPE,nvl(to_char(C.DATE_OF_INCORP,'dd/mm/yyyy'),'-') DATE_OF_INCORP,C.ADDRESS1,nvl(C.ADDRESS2,'-'),nvl(C.EMAIL_ID,'-'),"+
"nvl(C.WEBSITE_URL,'-'),nvl(C.PHONE_NO,'-'),nvl(C.FAX_NO,'-'),nvl(C.MOBILE_NO,'-'),C.BANK_NAME,C.BANK_BRANCH,C.ACCOUNT_NO,C.IFSC_CODE," +
"C.PAN_NO,nvl(C.TIN_NO,'-'),nvl(C.CST_NO,'-') "+
"from "+
"CONTRACTOR C "+
"inner join CONTRACTOR_REG_AUTH CRA on CRA.REG_AUTH_ID=C.REG_AUTH_ID "+
"inner join CONTRACTOR_CLASS CC on CC.CLASS_ID=C.CONTRACTOR_CLASS_ID "+
"inner join INCORPORATION_TYPE IT on IT.INCORP_TYPE=C.INCORP_TYPE "+
"limit " + offset + ", " + noOfRecords ";
Here no order by keyword is there. I am retrieving data from different table and then display only 20 rows at a time.
In Oracle, you can use the special rownum variable. This example behaves like limit FirstRow, NrOfRows:
select *
from (
select *
, rownum as rn
from YourTable
order by
id
) as SubQueryAlias
where FirstRow <= rn
and rn < FirstRow + NrOfRows
An optimized version of this query from AskTom, linked from this SO question:
select *
from (
select /*+ FIRST_ROWS(n) */
rownum as rn
, *
from (
select *
from YourTable
order by
id
) as SubQueryAlias1
where rownum <= FirstRow + NrOfRows
) as SubQueryAlias2
where rn >= FirstRow
Related
I have this:
SELECT * FROM `npt_articles` WHERE (date_discovered >= 1464534366 AND
date_discovered <= 1464534366 AND
npt_site_id = 4 AND
(#total_social := (comments_html + fb_total + google_plus_one + pinterest + linked_in ) + 0.0) >
865.0 AND
http_status_code = 200) ORDER BY #total_social DESC;
It works but no where near as intended.
total_social is the aggregate value of other named fields of the same row (record, object, etc.) which is then compared against a number which is injected into the query string. I want to also use this aggregate in a virtual column to then be used to order the results.
I tried this but I'm not sure if this is the way to go:
SELECT *, #total_social := (comments_html + fb_total + google_plus_one + pinterest + linked_in ) + 0.0 FROM `npt_articles`
WHERE (date_discovered >= 1464534366 AND
npt_site_id = 4 AND
#total_social >
865.0 AND
http_status_code = 200) ORDER BY #total_social DESC;
I think what I want is something like this:
SELECT *, ((comments_html + fb_total + google_plus_one + pinterest + linked_in ) + 0.0) as total_social FROM `npt_articles` WHERE (date_discovered >= 1464534366 AND
npt_site_id = 4 AND
total_social >
865.0 AND
http_status_code = 200) ORDER BY total_social DESC;
The only issue is that MySQL Workbench says total_social doesn't exist.
I've tried using a CTE like this:
WITH inner_table AS (
SELECT
((comments_html + fb_total + google_plus_one + pinterest + linked_in) + 0.0) AS total_social
FROM
`npt_articles`
)
select * FROM inner_table
WHERE
(date_discovered >= 1464534366
AND npt_site_id = 4
AND total_social > 865.0
AND http_status_code = 200)
ORDER BY total_social DESC;
like in "Referring to a Column Alias in a WHERE Clause" but MySQL Workbench won't accept it because it doesn't like the WITH at that position, which I know now isn't supported by MySQL.
Right, so the solution was to use the HAVING clause which I found from "How to use a temp column in the where clause", which I think is basicly a WHERE clause that happens after a SELECT.
Now my SQL looks like this:
SELECT *,
(comments_html + fb_total + google_plus_one + pinterest + linked_in) + 0.0 AS total_social FROM `npt_articles` WHERE (
date_discovered >= 1464534366
AND
npt_site_id = 4
AND
http_status_code = 200
)
HAVING total_social > 865.0 ORDER BY total_social DESC;
Now it does the query, does the select, calculates the total_social column and then does a second query using total_social and then orders by total_social in descending order.
I have this kind of query. I am using limit, but this query gives me 20 results. Can anybody tell me why
SELECT
*,
`tablename`.`bookmark_id` as `bookmark_id`,
`tablename`.`bookmark_date` as `bookmark_date`
FROM ( (" + sql1 + ")
UNION ALL (" + sql2 + ")
UNION ALL (" + sql3 + ") ) AS tablename
WHERE `bookmark_id`
NOT IN
(SELECT `table1`.`bookmark_id`
FROM (
(SELECT `user_bookmarks`.`bookmark_id`
FROM `user_bookmarks`
WHERE `user_bookmarks`.`bookmark_id` = `bookmark_id`
AND `user_id` = 26)
UNION
(SELECT `bookmark_id`
FROM `user_deleted_bookmarks`
WHERE `user_id` = ?)
) AS `table1`)
GROUP BY bookmark_id
ORDER BY `bookmark_date`
DESC limit 17, 20
Thanks
From the SELECT docs
With one argument, the value specifies the number of rows to return from the beginning of the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
I think you want LIMIT 16, 4
When you use limit, second argument is number of results to return.
See : http://dev.mysql.com/doc/refman/5.0/en/select.html
In your case you want :
SELECT .... LIMIT 16,4
You will get 4 rows : 17, 18, 19 and 20.
SELECT *,`tablename`.`bookmark_id` as `bookmark_id`,`tablename`.`bookmark_date` as `bookmark_date` FROM ( (" + sql1 + ") union all (" + sql2 + ") union all (" + sql3 + ") ) AS tablename WHERE `bookmark_id` NOT IN (SELECT `table1`.`bookmark_id` FROM ((SELECT `user_bookmarks`.`bookmark_id` FROM `user_bookmarks` WHERE `user_bookmarks`.`bookmark_id` = `bookmark_id` AND `user_id` = 26) UNION (SELECT `bookmark_id` FROM `user_deleted_bookmarks` WHERE `user_id` = ?)) AS `table1`) GROUP BY bookmark_id ORDER BY `bookmark_date`
DESC limit 16, 4;
for getting result 17,18,19,20
it's count start from 17th position and give next 4 values
I have MySQL procedure which is collecting data from 3 tables:
dossier
courrier
courrier_concerne_dossier
Simply I just can't use select * from... I need every dossier_str separated (one dossier_str in each column and all in one big result set).
(SELECT dossier_oid from data.courrier_concerne_dossier where courrier_oid = param_oid LIMIT 1) as 'dossier1_oid',
(SELECT concat(a.prefixe_numero, "-", cast(a.numero as char), " - ",DATE_FORMAT(a.date_ouverture, '%e/%c/%Y'), " - ", b.nom, " - ", a.intitule)
FROM data.dossier as a
JOIN data.client as b on
a.client_oid=b.oid
WHERE a.oid = dossier1_oid) as 'dossier1_str',
(SELECT dossier_oid from data.courrier_concerne_dossier where courrier_oid = param_oid LIMIT 1,1) as 'dossier2_oid',
(SELECT concat(a.prefixe_numero, "-", cast(a.numero as char), " - ",DATE_FORMAT(a.date_ouverture, '%e/%c/%Y'), " - ", b.nom, " - ", a.intitule)
FROM data.dossier as a
JOIN data.client as b on
a.client_oid=b.oid
WHERE a.oid = dossier2_oid) as 'dossier2_str',
(SELECT dossier_oid from data.courrier_concerne_dossier where courrier_oid = param_oid LIMIT 2,1) as 'dossier3_oid',
(SELECT concat(a.prefixe_numero, "-", cast(a.numero as char), " - ",DATE_FORMAT(a.date_ouverture, '%e/%c/%Y'), " - ", b.nom, " - ", a.intitule)
FROM data.dossier as a
JOIN data.client as b on
a.client_oid=b.oid
WHERE a.oid = dossier3_oid) as 'dossier3_str',
In this query I want a total of x number of records returned. within that query I have several sub-queries where I can't be sure if they'll return the max number of records. if one result is less than it's max limit I want to populate the remaining slots with the next query and so on. I can't do math inside a limit clause so I'm still trying to figure out how to do it. here is what I would do if math was available inside the limit clause.
select *
from
(
(select * from profile where size='local' order by rand() limit 7) as local
join
(select * from profile where size='regional' order by rand() limit (13-count(local.id)) as regional
join
(select * from profile where size='national' order by rand() limit (19-(count(local.id)+count(regional.id))) as national
join
(select * from profile where size='international' order by rand() limit (25-(count(local.id)+count(regional.id)+count(national.id)))) as international
)
I might have done this a needlessly complicated way, but it does seem to work:-
SELECT id, size
FROM
(
SELECT id, size,
#SeqLocal:=IF(size="local", IF(#SeqLocal <= 7, #SeqLocal + 1, #SeqLocal), #SeqLocal) AS SeqLocal,
#SeqRegional:=IF(size="regional", IF(#SeqLocal + #SeqRegional <= 14, #SeqRegional + 1, #SeqRegional), #SeqRegional) AS SeqRegional,
#SeqNational:=IF(size="national", IF(#SeqLocal + #SeqRegional + #SeqNational <= 21 , #SeqNational + 1, #SeqNational), #SeqNational) AS SeqNational,
#SeqInternational:=IF(size="international", IF(#SeqLocal + #SeqRegional + #SeqNational + #SeqInternational <= 28, #SeqInternational + 1, #SeqInternational), #SeqInternational) AS SeqInternational
FROM
(
select *
from profile
where size IN ("local", "regional", "national", "international")
order by FIELD(size, "local", "regional", "national", "international"), rand()
) Sub1
CROSS JOIN (SELECT #SeqLocal:=0, #SeqRegional:=0, #SeqNational:=0, #SeqInternational:=0) Sub2
) Sub3
WHERE (size = "local" AND SeqLocal != #SeqLocal)
OR (size = "regional" AND SeqRegional != #SeqRegional)
OR (size = "national" AND SeqNational != #SeqNational )
OR (size = "international" AND SeqInternational != #SeqInternational)
Sqlfiddle here:-
http://www.sqlfiddle.com/#!2/cc3b884/14
What would be the equivalent Oracle(11g) code for performing following operation:
For MySQL
ps = con.prepareStatement("select SQL_CALC_FOUND_ROWS from student_details where UPPER(name) like UPPER(?) limit " + offset + ", " + noOfRecords);
and rs = ps.executeQuery("SELECT FOUND_ROWS()");
Try it like this:
SELECT *
FROM (select t.*, rownum rn, count(*) over() as SQL_CALC_FOUND_ROWS
from student_details t
where UPPER(name) like UPPER(?))
WHERE rn <= offset
The value returned in "SQL_CALC_FOUND_ROWS" will be the number of records that would have been selected if there wasn't the WHERE rn <= offset clause
The only two RDBMS-specific things I see in your statement are "upper()" and "limit".
Here's a link for using Oracle "rownum" as a workaround for "limit":
https://forums.oracle.com/forums/thread.jspa?threadID=415724.
"Upper()" should work fine as-is in Oracle.