如何在SQL中显示外国钥匙值,而不是其ID-S

发布于 2025-02-11 04:57:15 字数 1886 浏览 1 评论 0原文

我在制作日历时遇到了问题。我有一个名为rezervacije(预订)的表格,带有namestart date结束日期,等。 ;还有recervation_type_fk,将其连接到另一张表格的外键,其中保留类型是用名称,价格等列写下的。

现在我的问题是:

我如何显示保留名称类型而不是ID-S? 我当前的SQL查询是这样的:

select 
    ID_REZ, /*ID of the reservation, number goes up by 1 on every insert automatically generated*/
    START_REZ, /*DATE OF THE START OF THE RESERVATION*/
    END_REZ, /*DATE OF THE END OF THE RESERVATION*/
    DETAILS_REZ, /*VARCHAR2 - comments on the reservation*/
    case 
        when DETAILS_REZ is not null 
        then title || '[' || QUANTITY_REZ || 'x ' || /*HERE I WANT TO DISPLAY THE FK NAME*/ 
             || ' hours on ' || NUMBER_OF_HOURS || ' - ' || DETAILS_REZ || ' ]'
    end as title,
    QUANTITY_REZ, /* NUMBER Quantity of things on the reservation */
    NUMBER_OF_HOURS, /*NUMBER of hours of the reservation calculated automatically*/
    case
        when REZ_TYPE_FK = 8
        then  'apex-cal-red' /*THIS FK is a number 
Identity: always, in increments of 1 that connects to another table that has a name price etc and is saved in my RESERVATION table as a foreign key and I don't want to display it as a number but as a name of the RESERVATION TYPE*/
        when REZ_TYPE_FK = 9 then  'apex-cal-orange'
        when REZ_TYPE_FK = 10  then  'apex-cal-blue'
        when REZ_TYPE_FK = 11 then 'apex-cal-green'
        when REZ_TYPE_FK = 12 then 'apex-cal-lime'
    end as CSS_REZ   /*Varchar2 that paints my calendar entries to a certain color */
from
    REZERVACIJE
where 
    (nvl(:REZ_TYPE_OBJ,'0') = '0' or REZ_TYPE_FK = :REZ_TTYPE_OBJ)  /* This is for a LOV selection where i can filter what RESERVATION TYPE i want to see*/
order by
    START_REZ

因此,我只想在标题中显示预订类型的名称。我知道这是一个新秀问题,可能很简单,但是请耐心,因为我才刚刚开始。

I encountered a problem while making a calendar. I have a table named REZERVACIJE (reservations) with columns like name, start date, end date, etc.; and also a reservation_type_FK, a foreign key that connects it to another table where reservation types are written down with columns like name, price, etc.

Now my question is:

How do I show the name of the reservation types instead of the ID-s?
my current SQL query is like this:

select 
    ID_REZ, /*ID of the reservation, number goes up by 1 on every insert automatically generated*/
    START_REZ, /*DATE OF THE START OF THE RESERVATION*/
    END_REZ, /*DATE OF THE END OF THE RESERVATION*/
    DETAILS_REZ, /*VARCHAR2 - comments on the reservation*/
    case 
        when DETAILS_REZ is not null 
        then title || '[' || QUANTITY_REZ || 'x ' || /*HERE I WANT TO DISPLAY THE FK NAME*/ 
             || ' hours on ' || NUMBER_OF_HOURS || ' - ' || DETAILS_REZ || ' ]'
    end as title,
    QUANTITY_REZ, /* NUMBER Quantity of things on the reservation */
    NUMBER_OF_HOURS, /*NUMBER of hours of the reservation calculated automatically*/
    case
        when REZ_TYPE_FK = 8
        then  'apex-cal-red' /*THIS FK is a number 
Identity: always, in increments of 1 that connects to another table that has a name price etc and is saved in my RESERVATION table as a foreign key and I don't want to display it as a number but as a name of the RESERVATION TYPE*/
        when REZ_TYPE_FK = 9 then  'apex-cal-orange'
        when REZ_TYPE_FK = 10  then  'apex-cal-blue'
        when REZ_TYPE_FK = 11 then 'apex-cal-green'
        when REZ_TYPE_FK = 12 then 'apex-cal-lime'
    end as CSS_REZ   /*Varchar2 that paints my calendar entries to a certain color */
from
    REZERVACIJE
where 
    (nvl(:REZ_TYPE_OBJ,'0') = '0' or REZ_TYPE_FK = :REZ_TTYPE_OBJ)  /* This is for a LOV selection where i can filter what RESERVATION TYPE i want to see*/
order by
    START_REZ

So I just want to show in the title the name of the RESERVATION TYPE from the other table. I know this is such a rookie question and is probably very simple but please have patience because I just started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2025-02-18 04:57:15

带有以下假设:

  1. “另一个表”被命名为reservation_types带有主键reservation_type_id以及需要显示的列为reservient_type_type_name
  2. 列列rezervacije.rez_type_fk始终具有一个值。

选择将是:

select 
    R.ID_REZ, /*ID of the reservation, number goes up by 1 on every insert automatically generated*/
    R.START_REZ, /*DATE OF THE START OF THE RESERVATION*/
    R.END_REZ, /*DATE OF THE END OF THE RESERVATION*/
    R.DETAILS_REZ, /*VARCHAR2 - comments on the reservation*/
    case 
        when R.DETAILS_REZ is not null 
        then R.title || '[' || R.QUANTITY_REZ || 'x ' || T.RESERVATION_TYPE_NAME
             || ' hours on ' || R.NUMBER_OF_HOURS || ' - ' || R.DETAILS_REZ || ' ]'
    end as title,
    R.QUANTITY_REZ, /* NUMBER Quantity of things on the reservation */
    R.NUMBER_OF_HOURS, /*NUMBER of hours of the reservation calculated automatically*/
    case
        when R.REZ_TYPE_FK = 8
        then  'apex-cal-red' /*THIS FK is a number 
Identity: always, in increments of 1 that connects to another table that has a name price etc and is saved in my RESERVATION table as a foreign key and I don't want to display it as a number but as a name of the RESERVATION TYPE*/
        when R.REZ_TYPE_FK = 9 then  'apex-cal-orange'
        when R.REZ_TYPE_FK = 10  then  'apex-cal-blue'
        when R.REZ_TYPE_FK = 11 then 'apex-cal-green'
        when R.REZ_TYPE_FK = 12 then 'apex-cal-lime'
    end as R.CSS_REZ   /*Varchar2 that paints my calendar entries to a certain color */
from
    REZERVACIJE R
    JOIN RESERVATION_TYPES T ON R.REZ_TYPE_FK = T.RESERVATION_TYPE_ID
where 
    (nvl(:REZ_TYPE_OBJ,'0') = '0' or R.REZ_TYPE_FK = :REZ_TTYPE_OBJ)  /* This is for a LOV selection where i can filter what RESERVATION TYPE i want to see*/
order by
    R.START_REZ

用实际的表/列名称替换假设(1)中的值。如果假设(2)不正确,则您将无法获得具有r.rez_type_fk的null值的记录的行。将更改为 如果要所有记录,请左Out Out Join 。

With the following assumptions:

  1. "The other table" is named RESERVATION_TYPES with primary key RESERVATION_TYPE_ID and the column that needs to be displayed is RESERVATION_TYPE_NAME
  2. The column REZERVACIJE.REZ_TYPE_FK always has a value.

The select would be:

select 
    R.ID_REZ, /*ID of the reservation, number goes up by 1 on every insert automatically generated*/
    R.START_REZ, /*DATE OF THE START OF THE RESERVATION*/
    R.END_REZ, /*DATE OF THE END OF THE RESERVATION*/
    R.DETAILS_REZ, /*VARCHAR2 - comments on the reservation*/
    case 
        when R.DETAILS_REZ is not null 
        then R.title || '[' || R.QUANTITY_REZ || 'x ' || T.RESERVATION_TYPE_NAME
             || ' hours on ' || R.NUMBER_OF_HOURS || ' - ' || R.DETAILS_REZ || ' ]'
    end as title,
    R.QUANTITY_REZ, /* NUMBER Quantity of things on the reservation */
    R.NUMBER_OF_HOURS, /*NUMBER of hours of the reservation calculated automatically*/
    case
        when R.REZ_TYPE_FK = 8
        then  'apex-cal-red' /*THIS FK is a number 
Identity: always, in increments of 1 that connects to another table that has a name price etc and is saved in my RESERVATION table as a foreign key and I don't want to display it as a number but as a name of the RESERVATION TYPE*/
        when R.REZ_TYPE_FK = 9 then  'apex-cal-orange'
        when R.REZ_TYPE_FK = 10  then  'apex-cal-blue'
        when R.REZ_TYPE_FK = 11 then 'apex-cal-green'
        when R.REZ_TYPE_FK = 12 then 'apex-cal-lime'
    end as R.CSS_REZ   /*Varchar2 that paints my calendar entries to a certain color */
from
    REZERVACIJE R
    JOIN RESERVATION_TYPES T ON R.REZ_TYPE_FK = T.RESERVATION_TYPE_ID
where 
    (nvl(:REZ_TYPE_OBJ,'0') = '0' or R.REZ_TYPE_FK = :REZ_TTYPE_OBJ)  /* This is for a LOV selection where i can filter what RESERVATION TYPE i want to see*/
order by
    R.START_REZ

Replace the values in assumption (1) with the actual table/column names. If assumption (2) is incorrect you will not get a row for records that have a NULL value for R.REZ_TYPE_FK. Change JOIN to LEFT OUTER JOIN if you want all the records.

别低头,皇冠会掉 2025-02-18 04:57:15

这是加入最简单的用例

select
    r.*,
    rt.rez_type_name
from
    reservation r
    join
    reservation_type rt on rt.res_type_id = r.res_type_id

This is the most simple use case for JOIN

select
    r.*,
    rt.rez_type_name
from
    reservation r
    join
    reservation_type rt on rt.res_type_id = r.res_type_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文