您可以在SQL引用特定数据集中传递参数吗?
我有一个数据库,在数据库中,我想引用特定的数据集。当用户查询时,我希望它仅返回与用户相关的数据,而不是返回与他们无关的类似数据集。我想使用诸如关键字参数之类的内容,并且我知道SQL具有其中
子句,但它是有限的,并且不能使一个人返回一组特定的数据。
self._headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:78.0) Gecko/20100101 Firefox/78.0', 'Accept-Language': 'en-US,en;q=0.5'}
上面的示例引用了一个特定的实例,这就是我在数据库中要做的。
from cmath import e
from http.client import OK
from logging import exception
from socket import if_nameindex
import psycopg
import datetime
from config import *
class profiles:
def commercial_profile(self):
conn = None
try:
with psycopg.connect(
dbname = dbname,
user = user,
password = password,
host = host,
port = port
) as conn:
with conn.cursor() as cur:
total_streaming_plays = '''WITH temp AS (
SELECT artist, plays FROM machine_spotify
UNION ALL
(SELECT artist, plays FROM machine_tidal)
UNION ALL
(SELECT artist, plays FROM machine_soundcloud)
UNION ALL
(SELECT artist, plays FROM machine_youtube)
UNION ALL
(SELECT artist, plays FROM machine_youtube_music)
) SELECT SUM(plays) AS total_plays
FROM temp;'''
cur.execute(total_streaming_plays)
for records in cur:
print(records[0] * 2)
conn.commit()
except Exception as e:
print(e)
finally:
if conn is not None:
conn.close()
在我的上述查询中,它是选择艺术家,从Machine_spotify
中播放,我想让选择Artist = {username},从Machine_spotify
在数据库中引用用户名的数据。 <代码> 条款将有效,但这将是限制的。
I have a database, and in the database I want to reference a specific data set. When a user queries it I want it to only return data related to the user, as opposed to returning similar data sets not related to them. I want to use something like a keyword argument, and I know sql has a where
clause but it is limited and doesn't enable one to return a specific set of data.
self._headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:78.0) Gecko/20100101 Firefox/78.0', 'Accept-Language': 'en-US,en;q=0.5'}
The example above references a specific instance, and that's what I want to do within my database.
from cmath import e
from http.client import OK
from logging import exception
from socket import if_nameindex
import psycopg
import datetime
from config import *
class profiles:
def commercial_profile(self):
conn = None
try:
with psycopg.connect(
dbname = dbname,
user = user,
password = password,
host = host,
port = port
) as conn:
with conn.cursor() as cur:
total_streaming_plays = '''WITH temp AS (
SELECT artist, plays FROM machine_spotify
UNION ALL
(SELECT artist, plays FROM machine_tidal)
UNION ALL
(SELECT artist, plays FROM machine_soundcloud)
UNION ALL
(SELECT artist, plays FROM machine_youtube)
UNION ALL
(SELECT artist, plays FROM machine_youtube_music)
) SELECT SUM(plays) AS total_plays
FROM temp;'''
cur.execute(total_streaming_plays)
for records in cur:
print(records[0] * 2)
conn.commit()
except Exception as e:
print(e)
finally:
if conn is not None:
conn.close()
In my above query where it is SELECT artist, plays FROM machine_spotify
, I would like to have SELECT artist={username}, plays FROM machine_spotify
referencing username's data within the database. A where
clause would work but it would be limiting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论