您可以在SQL引用特定数据集中传递参数吗?

发布于 2025-01-24 09:41:10 字数 2007 浏览 3 评论 0原文

我有一个数据库,在数据库中,我想引用特定的数据集。当用户查询时,我希望它仅返回与用户相关的数据,而不是返回与他们无关的类似数据集。我想使用诸如关键字参数之类的内容,并且我知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文