如何在不使用子查询的情况下对一组ID值进行过滤

时间:2018-10-15 02:01:42

标签: database oracle

我想知道是否有人可以帮助我,所以我需要show this在Oracle中。

为此,我使用以下选择:

curl 'https://xyq.cbg.163.com/equip?s=352&eid=201809300000113-352-DCEBW3UFAURC&view_loc=equip_list' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://xyq.cbg.163.com/cgi-bin/login.py?next_url=%2Fequip%3Fs%3D352%26eid%3D201809300000113-352-DCEBW3UFAURC%26view_loc%3Dequip_list&server_id=352&act=do_anon_auth' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,und;q=0.7' -H 'Cookie: vjuids=5568da96a.1645fa80ea3.0.9bce666ccd158; vjlast=1530613207.1530613207.30; _ntes_nnid=6225af57f6bccb13029779bc5f612dc1,1530613206698; _ntes_nuid=6225af57f6bccb13029779bc5f612dc1; P_INFO=jiangwei1995910@163.com|1538213634|0|mail163|11&10|bej&1538141485&xyq#bej&null#10#0#0|&0|xyq&cbg|jiangwei1995910@163.com; nts_mail_user=jiangwei1995910@163.com:-1:1; mail_psc_fingerprint=bfb14236794fea1c71128deb46355f02; usertrack=CrH3/luvRwWoRWDVAx0mAg==; area_id=52; __session__=1; fingerprint=1264686547; cur_servername=%25E5%25BE%25B7%25E9%2598%25B3%25E6%2596%2587%25E5%25BA%2599; sid=smTmSs6c2gPxqlDfsmXuUsNNJPcba_CNNrv7uvbJ; last_login_serverid=352; wallet_data=%7B%22is_locked%22%3A%20false%2C%20%22checking_balance%22%3A%200%2C%20%22balance%22%3A%200%2C%20%22free_balance%22%3A%200%7D; latest_views=159_2928104-275_1957182-167_1244727-123_2368954-167_1178024-167_1232781-167_1244661-167_1243011-167_1245563-123_2327135-221_1481157-829_181405-173_3263018-352_2811234' --compressed

但是问题是我不能使用子选择,而我需要使用函数或过程,而我想到了这个函数:

import requests
res=requests.get("https://xyq.cbg.163.com/equip?s=352&eid=201809300000113-352-DCEBW3UFAURC&view_loc=equip_list")
headers={
'Host': 'xyq.cbg.163.com',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Referer': 'https://xyq.cbg.163.com/cgi-bin/login.py?next_url=%2Fequip%3Fs%3D352%26eid%3D201809300000113-352-DCEBW3UFAURC%26view_loc%3Dequip_list&server_id=352&act=do_anon_auth',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,und;q=0.7',
'Cookie': 'vjuids=5568da96a.1645fa80ea3.0.9bce666ccd158; vjlast=1530613207.1530613207.30; _ntes_nnid=6225af57f6bccb13029779bc5f612dc1,1530613206698; _ntes_nuid=6225af57f6bccb13029779bc5f612dc1; P_INFO=jiangwei1995910@163.com|1538213634|0|mail163|11&10|bej&1538141485&xyq#bej&null#10#0#0|&0|xyq&cbg|jiangwei1995910@163.com; nts_mail_user=jiangwei1995910@163.com:-1:1; mail_psc_fingerprint=bfb14236794fea1c71128deb46355f02; usertrack=CrH3/luvRwWoRWDVAx0mAg==; area_id=52; __session__=1; fingerprint=1264686547; cur_servername=%25E5%25BE%25B7%25E9%2598%25B3%25E6%2596%2587%25E5%25BA%2599; sid=smTmSs6c2gPxqlDfsmXuUsNNJPcba_CNNrv7uvbJ; last_login_serverid=352; wallet_data=%7B%22is_locked%22%3A%20false%2C%20%22checking_balance%22%3A%200%2C%20%22balance%22%3A%200%2C%20%22free_balance%22%3A%200%7D; latest_views=159_2928104-275_1957182-167_1244727-123_2368954-167_1178024-167_1232781-167_1244661-167_1243011-167_1245563-123_2327135-221_1481157-829_181405-173_3263018-352_2811234'
}
res.encoding='gb2312'
print(res.text)

这是一个仅用于返回最大值的函数,但是我无法返回游标。我不知道您是否能理解我,谢谢您的时间。

1 个答案:

答案 0 :(得分:0)

否,您无法返回游标并无法按照您想要的方式在SQL语句WHERE子句中使用该游标。

您可以编写一个函数以TABLE OF NUMBER的形式返回要查找的id值,但是您仍然需要使用子查询来访问主查询中的结果。即

WHERE idMedicament IN ( SELECT * FROM TABLE(my_custom_function()) )

因此,我认为用于返回要过滤的idMedicamento的值的函数不能很好地满足您的“无子查询”限制。


备用方法#1

顺便说一句,在Oracle 12c上,您可以编写不带子查询且不具有以下功能的查询:

SELECT m.idMedicamento, m.nombre, m.precio,
case when rownum() over ( order by idMedicamento asc ) <= 3
       or rownum() over ( order by idMedicamento desc ) <= 3 THEN 'Y' ELSE null END include_flag
FROM medicamento m
ORDER BY include_flag nulls last, m.precio DESC
FETCH FIRST 6 ROWS ONLY;

替代方法2

我认为您不能使用子查询,因为您正在使用为您构建SQL的工具或框架。在这种情况下,也许您也不能使用窗口函数(即rownum() over (...))。您可以通过创建视图然后针对该视图编写查询来解决此问题。

赞:

CREATE OR REPLACE VIEW medicamento_v AS
SELECT m.idMedicamento, m.nombre, m.precio,
rownum() over ( order by idMedicamento asc) asc_order,
rownum() over ( order by idMedicamento desc) desc_order
FROM medicamento m;

SELECT m.idMedicamento, m.nombre, m.precio
FROM   medicameno_v m
WHERE ( asc_order <= 3 OR desc_order <= 3 )
ORDER BY m.precio DESC;